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


Mar 8, 2001, 10:45 AM

Views: 3185
Required help with require...

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");

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

my ($Field, $Table, $Value, $Name, $First, $Selected) = @_;
my $ThisSQL="SELECT $Value, $Name ";
$ThisSQL.="FROM $Table ";
$ThisSQL.="ORDER BY $Name ";
my @Registros;
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";
return $DropMenu;


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

Enthusiast / Moderator

Mar 8, 2001, 11:52 AM

Views: 3182
Re: Required help with require...

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


Mar 8, 2001, 12:43 PM

Views: 3181
Re: Required help with require...

Any other non-obvius way???

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

Maybe I'm too lazy to put another argument.

Enthusiast / Moderator

Mar 8, 2001, 2:41 PM

Views: 3179
Re: Required help with require...

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.

use vars qw( $DataBase ); 

$DataBase = DBI->connect(

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


Mar 8, 2001, 4:29 PM

Views: 3177
Re: Required help with require...

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!!!

Enthusiast / Moderator

Mar 8, 2001, 6:23 PM

Views: 3176
Re: Required help with require...

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:

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

Or you could explicitly state the package name:

$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


Mar 9, 2001, 7:29 AM

Views: 3167
Re: Required help with require...

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.