CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Required help with require...

 



nezumi
Deleted

Mar 8, 2001, 10:45 AM

Post #1 of 7 (1280 views)
Required help with require... Can't Post

Hello, This is my problem:

I made a main program that uses another with requiere. In the main program I declare

#!/usr/bin/perl -w
require "libraries.pl";
use strict;
use DBI;

my $DataBase = DBI->connect("dbi:Pg:dbname=datos", "pepe", "123");

print &DBIDROPMENU("Areas", "areas, "id", "name", "Select One");
exit;


----
The libraries.pl program conteins.
#!/usr/bin/perl -w

sub DBIDROPMENU {
my ($Field, $Table, $Value, $Name, $First, $Selected) = @_;
my $ThisSQL="SELECT $Value, $Name ";
$ThisSQL.="FROM $Table ";
$ThisSQL.="ORDER BY $Name ";
my @Registros;
@Registros=&DBSELECT($ThisSQL);
my $DropMenu="<SELECT NAME=\"$Field\">\n";
if ($First) {
$DropMenu.=" <OPTION VALUE=\"\">$First";
}
for my $J ( 0 .. $#Registros ) {
if ($Selected eq $Registros[$J]{$Value}) {
$DropMenu.=" <OPTION VALUE=\"$Registros[$J]{$Value}\" Selected>$Registros[$J]{$Name}\n";
} else {
$DropMenu.=" <OPTION VALUE=\"$Registros[$J]{$Value}\">$Registros[$J]{$Name}\n";
}
}
$DropMenu.="</SELECT>\n";
return $DropMenu;
}

1;


------
Of course de DBIDROPMENU gives a full SELECT tag with a given table, value, name, etc. The idea is to put this function in a library file so I could use it anywhere.

When I trie to run this the program runs correctly but doesn't return the select tag properly, It only works if I put the function in the same main program...

Obviusly the problem is the that the sub in the other program doesn't sees the variable $DataBase of the main. How can I solve this, when I need the to declare the $DataBase variable in any main program and using it in the same libraries.pl???

Thank you for your help....

PD. Sorry for my english, I have a broken arm




japhy
Enthusiast / Moderator

Mar 8, 2001, 11:52 AM

Post #2 of 7 (1277 views)
Re: Required help with require... [In reply to] Can't Post

The function can't see the variable because the variable was my()ed in a different file. The obvious answer is to send that variable as another argument to the function.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


nezumi
Deleted

Mar 8, 2001, 12:43 PM

Post #3 of 7 (1276 views)
Re: Required help with require... [In reply to] Can't Post

Any other non-obvius way???

I have many other subs that works that way....

Maybe I'm too lazy to put another argument.



japhy
Enthusiast / Moderator

Mar 8, 2001, 2:41 PM

Post #4 of 7 (1274 views)
Re: Required help with require... [In reply to] Can't Post

If you're too lazy, that's too bad. One of the limitations of scope is that a lexically scoped variable in one file cannot be seen in another file -- it must be passed in a function, or held in a global.

That's the other solution. Use a package variable, not a lexical variable.


Code
use vars qw( $DataBase ); 

$DataBase = DBI->connect(
"dbi:Pg:dbname=datos",
"pepe",
"123"
);

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


nezumi
Deleted

Mar 8, 2001, 4:29 PM

Post #5 of 7 (1272 views)
Re: Required help with require... [In reply to] Can't Post

I really don't see why lazziness is too bad, it makes me think harder so I could work less... Cool

Where I put the package variable, in the main program or the library? Where do I put the 6 lines?

Now to think harder... Is there a thierd way? Maybe something like Declare Global $DataBase? Not using -w nor strict? any other??

Let's think more, work less!!!




japhy
Enthusiast / Moderator

Mar 8, 2001, 6:23 PM

Post #6 of 7 (1271 views)
Re: Required help with require... [In reply to] Can't Post

I am not in the mood to entertain any suggestions of not using -w or strict. You are also asking about false laziness (laziness because you're too lazy to think straight).

And if you did some thinking (like you advocate), you would figure out that my 6 lines of code are meant to replace the line of code in the main program you have (the my $DataBase = ... line).

As for declaring globals, that's what I just did. In Perl 5.6.0, instead of doing use vars qw( ... ), you could say:


Code
our $DataBase = DBI->connect(...);

Or you could explicitly state the package name:


Code
$main::DataBase = DBI->connect(...);

Do you want to know what the main problem is with your anti-sending-an-argument approach is? It constricts your code. From now on, you have to name ALL your DBI objects $DataBase if you wish to have this function use them. That could be a problem if you have more than one open at a time. You'd end up doing silly workarounds, just to avoid sending a variable to a function.

Lighten up. Send the scalar as an argument. Some people can think and work at the same time.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


nezumi
Deleted

Mar 9, 2001, 7:29 AM

Post #7 of 7 (1262 views)
Re: Required help with require... [In reply to] Can't Post

I don't like to think straight, I prefer to think diferent...

Anyway, thank you for your help, is obvious that the better way is by argument. By asking more I just learn more, so asking is thinking. So I learn many things thanks to you:

1.- The variables of one file will not been seen by another
2.- Is better to pass variables by function.
3.- Never think about programing without stricts
4.- You can declare globals with use vars qw(..) or stating the package name. But is bad programming.

That's thinking. One simple question, many answers. =)

Thank you, and of course I used arguments from your first answer, I just wanted to know if there where any other posibilities and why not to used them.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives