Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

hash pm

by toniax (Scribe)
on Nov 30, 2010 at 00:08 UTC ( #874398=perlquestion: print w/replies, xml ) Need Help??
toniax has asked for the wisdom of the Perl Monks concerning the following question:

Hello again,
I figured out a solution . I found this snippet of code on here and
used part of it for my purpose. What I did is create a file called with this code in it
package A; { my $vars = { main_html_dir => '/home/x/public_html/13X', script_url => 'http://www.13/cgi-bin/13X/NEW/13x.cgi', html_dir => 'http://www.13/13X', script_dir => '/home/x/public_html/cgi-bin/13X/', ext => 'html', }; sub main_html_dir { return $vars->{main_html_dir}; } sub script_url { return $vars->{script_url}; } sub html_dir { return $vars->{html_dir}; } sub script_dir { return $vars->{script_dir}; } sub ext { return $vars->{ext}; } }
I used it like this to test it in one of my scripts.
require ""; open(NEWFILEXQ,">>13x.txt") || die $!; print NEWFILEXQ A->main_html_dir(),; close(NEWFILEXQ);
It works, but I am wondering if this is a good way to avoid using globals

I have 3 cgi scripts that need to share some data
Lets say I created a PM file with a hash in it.
my $vars = { main_html_dir => '/home/x/public_html/13X', script_url => 'http://www.13/cgi-bin/13X/NEW/x.cgi', html_dir => 'http://www.13/13X', script_dir => '/home/x/public_html/cgi-bin/NEW', };
How can I get the values from the hash in the PM file to the cgi script(s) as they require them

Replies are listed 'Best First'.
Re: hash pm
by fullermd (Priest) on Nov 30, 2010 at 03:04 UTC

    You'll need to make it a package variable (our and probably exported, or explicitly call it as Package::varname) instead of a lexical (my).

    $MYconf::vars = { <stuff> };
      Thanks for the info.
      After reading about Perl modules I now realize it is more involved .
      I will just have to read more about it

        The important bit to remember in this case is that "modules" and "packages (namespaces)" and "files" often exist in 1:1:1 ratios, but don't necessarily have to. That's just the common and convenient way.

        What you want is for this variable to be visible from outside this {file,module}. Using my is wrong for that, since my creates a lexical variable, which is limited to the current scope (and the largest scope it can occupy is a file, so it can't be gotten at from outside).

        So it has to be a package variable of one sort or another. It can live in the package of this module, and be exported (via Exporter, or manual trickery if you're insane). It can live in that package and not be exported, and thus accessed only by its fill name $My::Package::vars. Or you can just invent a package namespace for it, and call it $MyConfig::vars everywhere. You may have to read up on how packages work in perl if that doesn't make sense...

        The most obvious (and probably right) Way To Do It in this case is just to make it a package variable in that module's package, declare it in that file with our instead of my, and either use Exporter or refer to it by full name in other files. The various other solutions mentioned above are comparatively less likely to be the best choice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://874398]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-06-19 09:30 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.