accessing main:: vars in required file

S

Stephen O'D

Guys,

I am trying to create a program that requires a file, and the required
file needs to be able to see variables from the main program (perl
5.6.1):

eg

my %config;
%config = load_config();
# now I have a bunch of name value pairs

require "myotherfile.inc";

foreach my $step (@steps) {
# do something
}

myotherfile.inc includes something like the following:

@steps = ( "$config{KEY}/some/path/etc");

1;

When I run this %config is always undefined in the required file. I
even attempted putting $config in a package and exporting it, and
accessing it from %package::config but it is still undef.

I am obviously missing something about how this works - can someone
please shed some light on it for me? The steps array always contains
data and is accessible in the main program, but it just cannot see the
config hash I populated before requiring it ...

Thanks,

Stephen.
 
B

Brian McCauley

Guys,

I am trying to create a program that requires a file, and the required
file needs to be able to see variables from the main program (perl
5.6.1):

my %config;

my() declares a variable as _lexically_ scoped. That means it's only
visible in code that is _lexically_ part of the remainder of enclosing
block, it's not visible to code elsewhere that's called via subroutine
calls or do/require. (A lexical variable _is_ (sometimes) visible to
code that's eval()ed even though you'd think it shouldn't be).

See "coping with scoping" or the FAQ "What's the difference between
dynamic and lexical (static) scoping? Between local() and my()?"
 
T

Ted Zlatanov

I am trying to create a program that requires a file, and the required
file needs to be able to see variables from the main program (perl
5.6.1):

eg

my %config;
%config = load_config();
# now I have a bunch of name value pairs

require "myotherfile.inc";

foreach my $step (@steps) {
# do something
}

myotherfile.inc includes something like the following:

@steps = ( "$config{KEY}/some/path/etc");

1;

When I run this %config is always undefined in the required file. I
even attempted putting $config in a package and exporting it, and
accessing it from %package::config but it is still undef.

I am obviously missing something about how this works - can someone
please shed some light on it for me? The steps array always contains
data and is accessible in the main program, but it just cannot see the
config hash I populated before requiring it ...

As much as I dislike the approach you are taking, you want to simply
say

do "myotherfile.inc";

Please look at AppConfig or similar modules to manage your
configuration properly.

"perldoc -f do" will tell you more about how this works.

Ted
 
M

Mumia W. (reading news)

Guys,

I am trying to create a program that requires a file, and the required
file needs to be able to see variables from the main program (perl
5.6.1):

eg

my %config;
%config = load_config();
# now I have a bunch of name value pairs

.... which are restricted to the current module. You would have to
declare %config as an "our" variable for it to be visible in other modules.

require "myotherfile.inc";

foreach my $step (@steps) {
# do something
}

myotherfile.inc includes something like the following:

@steps = ( "$config{KEY}/some/path/etc");

1;

When I run this %config is always undefined in the required file. I
even attempted putting $config in a package and exporting it, and
accessing it from %package::config but it is still undef.

I am obviously missing something about how this works - can someone
please shed some light on it for me? The steps array always contains
data and is accessible in the main program, but it just cannot see the
config hash I populated before requiring it ...

Thanks,

Stephen.

Read "perldoc -f our"
 
S

Stephen O'D

visible in code that is _lexically_ part of the remainder of enclosing
block, it's not visible to code elsewhere that's called via subroutine
calls or do/require. (A lexical variable _is_ (sometimes) visible to
code that's eval()ed even though you'd think it shouldn't be).

See "coping with scoping" or the FAQ "What's the difference between
dynamic and lexical (static) scoping? Between local() and my()?"

Of course - I finally figured out that declaring the variables as our()
get me what I needed. I thought (wrongly) that if I required a file
that wasnt a module that it would be in the same lexial scope.

I know what I am doing is really 'good practice' - i am trying to
achive something similar to MJD's dispatch tables described in Higher
Order Perl - i think I need to go back and re-read it ...

Thanks.
 
T

Tad McClellan

Stephen O'D said:
I thought (wrongly) that if I required a file
that wasnt a module that it would be in the same lexial scope.


That wrong thought can easily be avoided by remembering that

lexical variable are never visible across file boundaries.

I know what I am doing is really 'good practice' - i am trying to
achive something similar to MJD's dispatch tables described in Higher
Order Perl - i think I need to go back and re-read it ...


Perhaps you should read this too:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top