in reply to Variable scoping problem
I would like the output to be as follows: What am I doing wrong?
Why would you like the output to be as follows? What are you actually trying to accomplish?
Is this a test/quiz question?
Or
What book are you learning from?
Have you read require?
This is how you should write that program
my $database = LoadDatabase('a.pl'); print "$database\n"; $database = LoadDatabase('b.pl'); print "$database\n"; $database = LoadDatabase('a.pl'); print "$database\n"; exit( 0 ); sub LoadDatabase { .... }
Devel::Trace will trace your program exactly
>> c.pl:1: require "a.pl"; >> a.pl:1: use strict; ... >> a.pl:2: use warnings; ... >> c.pl:2: print "$database\n"; >> c.pl:3: require "b.pl"; >> b.pl:1: use strict; ... >> b.pl:2: use warnings; ... >> c.pl:4: print "$database\n"; >> c.pl:5: require "a.pl"; >> c.pl:6: print "$database\n";
As you can see the last require does't execute a.pl because its already loaded
|
|---|