how to access a (global) variable across files

Discussion in 'Perl Misc' started by Gunnar Hjalmarsson, Nov 6, 2003.

  1. Newbie wrote:
    > 3> When I declare $ini as out (global) in A.pl and try accessing it
    > in B.pm as:
    > my $iniFile = Config::IniFiles->val(file =>$ini)
    > It says: Global symbol "$ini" requires explicit package name at
    > B.pm


    Try:

    my $iniFile = Config::IniFiles->val(file =>$main::ini)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 6, 2003
    #1
    1. Advertising

  2. Gunnar Hjalmarsson

    Newbie Guest

    Hi

    Scenario is like this:
    1>I have a file A.pl. It makes a call to the new() functions of other
    2 files B.pm and C.pm.
    2> I am accepting a cfg.ini filename as parameter while running A.pl
    and storing it in a variable $ini, which I want to access to B.pm and
    C.pm.
    3> When I declare $ini as out (global) in A.pl and try accessing it in
    B.pm as:
    my $iniFile = Config::IniFiles->val(file =>$ini)
    It says: Global symbol "$ini" requires explicit package name at B.pm

    Please tell me how to get the solution for this. P.S.: I dont want to
    pass this variable as argument to new().

    Pl. reply ASAP.
    thanks
    Newbie, Nov 6, 2003
    #2
    1. Advertising

  3. Gunnar Hjalmarsson

    Ben Morrow Guest

    Gunnar Hjalmarsson <> wrote:
    > Newbie wrote:
    > > 3> When I declare $ini as out (global) in A.pl and try accessing it
    > > in B.pm as:
    > > my $iniFile = Config::IniFiles->val(file =>$ini)
    > > It says: Global symbol "$ini" requires explicit package name at
    > > B.pm

    >
    > Try:
    >
    > my $iniFile = Config::IniFiles->val(file =>$main::ini)


    While this is the correct answer to the question the OP asked :), it
    would almost certainly be better to either create the variable in the
    B namespace, ie. in A.pl use
    $B::ini = "foo";
    rather than simply
    our $ini = "foo";
    and then declare it as 'our $ini' in B.pm; or to put
    my $ini;
    sub set_ini {
    $ini = shift;
    }
    in B.pm and then call 'B::set_ini("foo")' in A.pl. It's cleaner, and
    makes your module more likely to be reusable.

    Ben

    --
    Musica Dei donum optimi, trahit homines, trahit deos. |
    Musica truces molit animos, tristesque mentes erigit. |
    Musica vel ipsas arbores et horridas movet feras. |
    Ben Morrow, Nov 6, 2003
    #3
  4. Ben Morrow wrote:
    > Gunnar Hjalmarsson <> wrote:
    >> Newbie wrote:
    >>> 3> When I declare $ini as out (global) in A.pl and try
    >>> accessing it in B.pm as:
    >>> my $iniFile = Config::IniFiles->val(file =>$ini)
    >>> It says: Global symbol "$ini" requires explicit package name at
    >>> B.pm

    >>
    >> Try:
    >>
    >> my $iniFile = Config::IniFiles->val(file =>$main::ini)

    >
    > While this is the correct answer to the question the OP asked :),
    > it would almost certainly be better to either create the variable
    > in the B namespace, ie. in A.pl use
    > $B::ini = "foo";
    > rather than simply
    > our $ini = "foo";
    > and then declare it as 'our $ini' in B.pm; or to put
    > my $ini;
    > sub set_ini {
    > $ini = shift;
    > }
    > in B.pm and then call 'B::set_ini("foo")' in A.pl. It's cleaner,
    > and makes your module more likely to be reusable.


    No objection. Global variables in package main is normally not advisable.

    Maybe the very first advise to OP should be to study the relevant
    docs, such as:

    http://www.perldoc.com/perl5.8.0/pod/perlmod.html

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 6, 2003
    #4
  5. Gunnar Hjalmarsson

    Newbie Guest

    thanks 4 ur replies... but thats not working... something trivial is
    going wrong somewhere

    i m doing it from scratch. m creating 2 files A.pl:
    #!/user/local/bin/perl

    use strict;
    use B;

    my $varA = "cfgA";
    $B::varG = "cfgG";

    print "Inside PL file....varA = $varA\n";
    print "Inside PL file....varG = $B::varG\n";

    &B::test ();

    and B.pm:

    package B;

    local $varG;
    @EXPORT = qw (&test $varG);

    sub test () {
    print "a: $main::varA\n";
    print "varG = $varG\n";
    }

    1;

    running A.pl, it gives:
    Undefined subroutine &B::test called at A.pl line 12.

    P.S.: I have set the env var PERL5LIB=mydir
    Please reply
    Newbie, Nov 7, 2003
    #5
  6. Gunnar Hjalmarsson

    Anno Siegel Guest

    Newbie <> wrote in comp.lang.perl.misc:
    > thanks 4 ur replies... but thats not working... something trivial is


    Oh please... use complete English words. This kind of baby talk has
    been out since the last 300 Baud modems disappeared from the market.

    > going wrong somewhere
    >
    > i m doing it from scratch. m creating 2 files A.pl:
    > #!/user/local/bin/perl

    ^
    You mean "/usr/...".

    >
    > use strict;
    > use B;


    Don't call your module "B". More generally, don't call your module
    like an existing module. The Perl standard distribution has a module
    B.pm, and that is loaded, not yours.


    > my $varA = "cfgA";
    > $B::varG = "cfgG";
    >
    > print "Inside PL file....varA = $varA\n";
    > print "Inside PL file....varG = $B::varG\n";
    >
    > &B::test ();
    >
    > and B.pm:
    >
    > package B;
    >
    > local $varG;
    > @EXPORT = qw (&test $varG);


    If you want to export, you must load the Exporter module. "require Exporter"
    is missing. You also need to let your module inherit the import() routine
    from Exporter. "BEGIN { our @ISA = qw( Exporter) }" is missing. See
    "perldoc Exporter". You should always look at the documentation of things
    if something isn't working as you expect.

    The failure to use Exporter correctly is irrelevant here, because you
    never call the exported version of test(), you are calling it fully
    qualified.

    > sub test () {
    > print "a: $main::varA\n";
    > print "varG = $varG\n";
    > }
    >
    > 1;
    >
    > running A.pl, it gives:
    > Undefined subroutine &B::test called at A.pl line 12.


    Because the B.pm you actually loaded doesn't define a test() subroutine.

    > P.S.: I have set the env var PERL5LIB=mydir


    Why would that matter? Is B.pm in mydir?

    Anno
    Anno Siegel, Nov 7, 2003
    #6
  7. Also sprach Anno Siegel:

    > Newbie <> wrote in comp.lang.perl.misc:


    >> my $varA = "cfgA";
    >> $B::varG = "cfgG";
    >>
    >> print "Inside PL file....varA = $varA\n";
    >> print "Inside PL file....varG = $B::varG\n";
    >>
    >> &B::test ();
    >>
    >> and B.pm:
    >>
    >> package B;
    >>
    >> local $varG;
    >> @EXPORT = qw (&test $varG);

    >
    > If you want to export, you must load the Exporter module. "require Exporter"
    > is missing. You also need to let your module inherit the import() routine
    > from Exporter. "BEGIN { our @ISA = qw( Exporter) }" is missing. See
    > "perldoc Exporter". You should always look at the documentation of things
    > if something isn't working as you expect.


    Maybe we can come to a conclusion in this group not to use this
    cumbersome idiom of subclassing Exporter by manually pushing onto
    @ISA. We recently had a thread ("what is @$?") on this where no one
    quite could explain why it is exemplified that way throughout the Perl
    documentation. So unless someone can point out any potential (or
    non-potential) flaws of

    use base qw(Exporter);

    we should recommend this notion. After all, this is what the base pragma
    was initially made for.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Nov 7, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. luc wastiaux

    global variable across object files

    luc wastiaux, May 24, 2004, in forum: C Programming
    Replies:
    5
    Views:
    697
    Dan Pop
    May 25, 2004
  2. chandan kumar

    global variable across modules

    chandan kumar, Sep 11, 2013, in forum: Python
    Replies:
    0
    Views:
    117
    chandan kumar
    Sep 11, 2013
  3. Chris Angelico

    Re: global variable across modules

    Chris Angelico, Sep 11, 2013, in forum: Python
    Replies:
    0
    Views:
    99
    Chris Angelico
    Sep 11, 2013
  4. Dave Angel

    Re: global variable across modules

    Dave Angel, Sep 11, 2013, in forum: Python
    Replies:
    0
    Views:
    115
    Dave Angel
    Sep 11, 2013
  5. John Pote

    Re: global variable across modules

    John Pote, Sep 11, 2013, in forum: Python
    Replies:
    0
    Views:
    119
    John Pote
    Sep 11, 2013
Loading...

Share This Page