Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Using variables in require file... not possible?

by Rubber Cthulhu (Acolyte)
on Oct 16, 2011 at 08:56 UTC ( #931746=note: print w/replies, xml ) Need Help??


in reply to Using variables in require file... not possible?

Using global variables is really a bad practice. But if you realy want to make the program with this aproach, you should know some tricks. When you are using 'use strict' pragma you must declare your global variables using explicit package prefix. Though you don't declare any package in your main module, it has name 'main' defined implicitly. Hence you need to declare your 'foo' global as
use strict; $main::foo = 'bar'; # that's ok
instead
use strict; $foo = 'bar'; # error!!! it doesn't work with strict pragma!
So when you want to use global $foo (defined in main script) in other modules you need to use it's full name - $main::foo. Simple example:

1. Script main.pl
#!/usr/bin/perl use strict; use pkg1; $main::foo = 'bar'; print pkg1_get_foo()."\n";
2. Module pkg1.pm
use strict; sub pkg1_get_foo { return "From pkg1: $main::foo"; } 1;

Replies are listed 'Best First'.
Re^2: Using variables in require file... not possible?
by anneli (Pilgrim) on Oct 16, 2011 at 10:03 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://931746]
help
Chatterbox?
[Discipulus]: beer finished in Strasbourg? ;=)
[Corion]: Discipulus: Yeah, or it got too warm and they hope for colder beer here ;)

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-07-23 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (459 votes). Check out past polls.

    Notices?