Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Help with passing through a package...

by basicdez (Pilgrim)
on Jan 31, 2002 at 22:15 UTC ( [id://142526]=perlquestion: print w/replies, xml ) Need Help??

basicdez has asked for the wisdom of the Perl Monks concerning the following question:

I have this module saved and want to pass information between these, why am I being so unsuccesful?

I call this one X2PCONF.pm

package X2PCONF; my $Num_OF_Bank_INFO = 2;
and want to call the information into this one main.pl
#!/usr/local/bin/perl-Tw use X2PCONF; print "Number $X2PCONF::Num_OF_Bank_INFO\n";
What I expect is to print out Number 2, but what I am getting is Number and a blank. Can anyone tell me why this is happening?

peace, LOVE and ((code))

basicdez

Replies are listed 'Best First'.
Re: Help with passing through a package...
by wog (Curate) on Jan 31, 2002 at 22:21 UTC
    Variables declared with my are local to the block or file which they are declared within. To make a variable accessible from the outside, you can declare with use vars, our (5.6+ only), or just use its full name instead of declaring it (e.g. $X2PCONF::...). (Of course, you don't even need to declare it or use its full name if you don't use strict, but please don't do that.)
      I am using the full name, do you know why it is not passing through? I am very stumped with this one. I am trying to declare one table to use throughout a larger set of programs, it is not only about passing through one variable, this is merely the tip of the iceberg. please help me if you can.
        I meant use that using fullname in XPCONF2.pm (and not my or anything else) was one option; I wasn't (purposely) suggesting that you'd need to change what's in your main script. What you are doing now in the .pm file is referring to a seperate variable declared with my when you do the assignment.
Re: Help with passing through a package...
by blakem (Monsignor) on Jan 31, 2002 at 22:24 UTC
    Lexical variables ( i.e. ones declared with my ) can't be used outside their scope. Therefore, the quickest way to get this code to run is to make your variable global (i.e. a "package variable") using either our or use vars.

    However, globals should avoided when possible, so this might not be the best long term approach. With a little more about what you're trying to do, its probably possible get rid of the globals all together.

    -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found