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

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
vars.pm 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 "vars.pm"; 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

Hi,
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

Comment on hash pm
Select or Download Code
Re: hash pm
by fullermd (Curate) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://874398]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-04-19 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls