Using Variables from Another Script

Discussion in 'Perl Misc' started by John Smith, Apr 3, 2006.

  1. John Smith

    John Smith Guest

    We have global_vars.pl which defines:

    $foo = "/abc/def/hij/";


    We then have temp.pl which uses $foo as follows:

    #!/usr/bin/perl -w
    require "./mxrt_vars.pl";
    use strict;
    print "$foo\n";

    ../temp.pl
    Global symbol "$foo" requires explicit package name at ./temp.pl line
    10.
    Execution of ./temp.pl aborted due to compilation errors.



    If I try declaring $foo in temp.pl as follows:

    #!/usr/bin/perl -w
    require "./mxrt_vars.pl";
    use strict;

    my $foo;
    print "$foo\n";

    ./temp.pl
    Use of uninitialized value at ./temp.pl line 10.

    I have also tried changing $foo to my $foo in global_vars.pl; I still
    got an error.
    What is the correct way to use $foo, which is included in
    global_vars.pl?
     
    John Smith, Apr 3, 2006
    #1
    1. Advertising

  2. John Smith

    Paul Lalli Guest

    John Smith wrote:
    > We have global_vars.pl which defines:
    >
    > $foo = "/abc/def/hij/";
    >
    >
    > We then have temp.pl which uses $foo as follows:
    >
    > #!/usr/bin/perl -w
    > require "./mxrt_vars.pl";
    > use strict;
    > print "$foo\n";
    >
    > ./temp.pl
    > Global symbol "$foo" requires explicit package name at ./temp.pl line
    > 10.
    > Execution of ./temp.pl aborted due to compilation errors.


    Either add:
    our $foo;
    or fully qualify it:
    print "$main::foo\n";

    > If I try declaring $foo in temp.pl as follows:
    >
    > #!/usr/bin/perl -w
    > require "./mxrt_vars.pl";
    > use strict;
    >
    > my $foo;
    > print "$foo\n";


    $foo is now a lexical variable that has absolutely *nothing* do with
    the global $foo that you decalred in mxrt_vars.pl

    > ./temp.pl
    > Use of uninitialized value at ./temp.pl line 10.
    >
    > I have also tried changing $foo to my $foo in global_vars.pl; I still
    > got an error.


    By definition, 'my' variables are not global.

    > What is the correct way to use $foo, which is included in
    > global_vars.pl?


    In general, create a module rather than a simple 'required' file that
    creates a bunch of global variables. I recommend creating a module
    that creates one global hash that contains all your values, and export
    that hash using Exporter

    package MyConfig;
    use strict;
    use warnings;
    use base 'Exporter';
    our @EXPORT = qw/%CFG/;
    our %CFG;
    $CFG{foo} = 'bar';
    $CFG{user} = 'mritty';
    # etc;

    1;

    ----------------------

    #!/usr/bin/perl
    use strict;
    use warnings;
    use MyConfig;

    print $CFG{foo};

    __END__


    Paul Lalli
     
    Paul Lalli, Apr 3, 2006
    #2
    1. Advertising

  3. John Smith

    Guest Guest

    John Smith <> wrote:
    : We have global_vars.pl which defines:

    : $foo = "/abc/def/hij/";


    : We then have temp.pl which uses $foo as follows:

    : #!/usr/bin/perl -w
    : require "./mxrt_vars.pl";

    Your script requires mxrt_vars.pl but never sees global_vars.pl. Do you
    hide code from us? No wonder $foo isn't declared anywhere.

    : #!/usr/bin/perl -w
    : require "./mxrt_vars.pl";
    : use strict;

    : my $foo;
    : print "$foo\n";

    : ./temp.pl
    : Use of uninitialized value at ./temp.pl line 10.

    Again, you do not reveal the whole story. I do not see ten (10) lines here.

    : I have also tried changing $foo to my $foo in global_vars.pl; I still
    : got an error.
    : What is the correct way to use $foo, which is included in
    : global_vars.pl?

    Tell your script which file name you really mean, and follow the require/
    use advice in the other posting.

    Regards, Oliver.

    --
    Dr. Oliver Corff e-mail: -berlin.de
     
    Guest, Apr 3, 2006
    #3
  4. John Smith wrote:
    > We have global_vars.pl which defines:
    >
    > $foo = "/abc/def/hij/";
    >
    >
    > We then have temp.pl which uses $foo as follows:
    >
    > #!/usr/bin/perl -w


    use strict; use warnings;

    our $foo; # pre-declare if global_vars.pl uses package main

    > require "./mxrt_vars.pl";

    ^^^^^^^^^^^^^^^^
    Did you mean "global_vars.pl" ?

    ( Maybe you need both require's too...? :
    require "./mxrt_vars.pl";
    require"/path/to/global_vars.pl"; )

    If the global_vars.pl declares a separate package, you
    could alternatively just package qualify $foo:

    print "$some_global_pkg_name::foo\n";


    hth,
    --
    Charles DeRykus
     
    Charles DeRykus, Apr 3, 2006
    #4
  5. John Smith

    John Smith Guest

    Paul Lalli wrote:

    > In general, create a module rather than a simple 'required' file that
    > creates a bunch of global variables. I recommend creating a module
    > that creates one global hash that contains all your values, and export
    > that hash using Exporter
    >
    > package MyConfig;
    > use strict;
    > use warnings;
    > use base 'Exporter';
    > our @EXPORT = qw/%CFG/;
    > our %CFG;
    > $CFG{foo} = 'bar';
    > $CFG{user} = 'mritty';
    > # etc;
    >
    > 1;
    >
    > ----------------------
    >
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > use MyConfig;
    >
    > print $CFG{foo};


    I tried putting the global variables in a hash in global_hash.pl as
    follows:
    #!/usr/bin/perl -w

    package MyConfig;
    use strict;

    use base 'Exporter';
    our @EXPORT = qw/%CFG/;
    our %CFG;
    $CFG{foo} = 'bar';
    $CFG{user} = 'mritty';

    1;

    then in temp.pl:
    #!/usr/bin/perl -w

    use strict;
    use MyConfig;

    print $CFG{foo};


    Running temp.pl gives the following error:
    .../temp.pl
    Can't locate MyConfig.pm in @INC (@INC contains:
    /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503
    /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .)
    at ./temp.pl line 4.
    BEGIN failed--compilation aborted at ./temp.pl line 4.


    How do we place the module MyConfig.pm in the @INC path?

    P.S. Please assume my system admin is not upgrading to a newer version
    of perl as such a upgrade will affect a lot of users and current perl
    scripts.
     
    John Smith, Apr 4, 2006
    #5
  6. "John Smith" <> wrote in news:1144160016.555604.27680
    @t31g2000cwb.googlegroups.com:

    > Paul Lalli wrote:
    >
    >> In general, create a module rather than a simple 'required' file that
    >> creates a bunch of global variables. I recommend creating a module
    >> that creates one global hash that contains all your values, and
    >> export that hash using Exporter
    >>
    >> package MyConfig;


    ....

    > How do we place the module MyConfig.pm in the @INC path?
    >


    If MyConfig.pm is in the same directory as your script,

    perldoc FindBin

    BTW, I would recommend a naming scheme based on the name of the script.
    If your script is called foo.pl, I would put the config either in
    FooConfig.pm or Foo/Config.pm in the script directory.

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 4, 2006
    #6
  7. John Smith

    Paul Lalli Guest

    John Smith wrote:
    > Paul Lalli wrote:
    > I tried putting the global variables in a hash in global_hash.pl as

    ^^^^^^^^^^^^^^
    > follows:
    > #!/usr/bin/perl -w
    >
    > package MyConfig;


    The module's name is MyConfig. Therefore, the file's name must be
    MyConfig.pm

    Also, get rid of that shebang. That does not belong in a module.

    > use strict;
    >
    > use base 'Exporter';
    > our @EXPORT = qw/%CFG/;
    > our %CFG;
    > $CFG{foo} = 'bar';
    > $CFG{user} = 'mritty';
    >
    > 1;
    >
    > then in temp.pl:
    > #!/usr/bin/perl -w
    >
    > use strict;
    > use MyConfig;
    >
    > print $CFG{foo};
    >
    >
    > Running temp.pl gives the following error:
    > ../temp.pl
    > Can't locate MyConfig.pm in @INC (@INC contains:
    > /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503
    > /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .)
    > at ./temp.pl line 4.
    > BEGIN failed--compilation aborted at ./temp.pl line 4.
    >
    >
    > How do we place the module MyConfig.pm in the @INC path?


    Well first, you create a file named MyConfig.pm....

    > P.S. Please assume my system admin is not upgrading to a newer version
    > of perl as such a upgrade will affect a lot of users and current perl
    > scripts.


    This has nothing to do with your issue, but is a really poor excuse for
    using a piece of software that is 15 years out of date.

    Install a new version of perl somewhere other than /usr/bin

    Paul Lalli
     
    Paul Lalli, Apr 4, 2006
    #7
  8. "Paul Lalli" <> wrote in news:1144160647.820426.308490
    @i39g2000cwa.googlegroups.com:

    > John Smith wrote:
    >> Paul Lalli wrote:
    >> I tried putting the global variables in a hash in global_hash.pl as

    > ^^^^^^^^^^^^^^


    Good catch. I missed that.

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 4, 2006
    #8
  9. John Smith

    John Smith Guest

    Paul Lalli wrote:
    > The module's name is MyConfig. Therefore, the file's name must be
    > MyConfig.pm
    >
    > Also, get rid of that shebang. That does not belong in a module.


    MyConfig.pm now reads:
    package MyConfig;
    use strict;

    use base 'Exporter';
    our @EXPORT = qw/%CFG/;
    our %CFG;
    $CFG{foo} = 'bar';
    $CFG{user} = 'mritty';

    1;

    (I cannot use warnings without shebang since I have an old version of
    perl)

    perl -c MyConfig.pm gives the following errors:
    Bareword "our" not allowed while "strict subs" in use at MyConfig.pm
    line 5.
    Array found where operator expected at MyConfig.pm line 5, at end of
    line
    (Do you need to predeclare our?)
    syntax error at MyConfig.pm line 5, near "our @EXPORT "
    Global symbol "@EXPORT" requires explicit package name at MyConfig.pm
    line 5.
    Bareword "our" not allowed while "strict subs" in use at MyConfig.pm
    line 6.
    Bareword "CFG" not allowed while "strict subs" in use at MyConfig.pm
    line 6.
    Operator or semicolon missing before %CFG at MyConfig.pm line 6.
    Ambiguous use of % resolved as operator % at MyConfig.pm line 6.
    Global symbol "%CFG" requires explicit package name at MyConfig.pm line
    7.
    MyConfig.pm had compilation errors.

    How come MyConfig.pm thinks "our" is a bareword (rather than keyword)?
    How to resolve the compilation errors?
     
    John Smith, Apr 4, 2006
    #9
  10. John Smith

    J. Gleixner Guest

    John Smith wrote:

    > (I cannot use warnings without shebang since I have an old version of
    > perl)


    > How come MyConfig.pm thinks "our" is a bareword (rather than keyword)?
    > How to resolve the compilation errors?


    Because, as you stated, you have an old version of perl. One that
    doesn't support 'our'. You can either upgrade or 'use vars'.

    use vars qw(%CFG);
    $CFG{foo} = 'bar';


    For more information see: perldoc vars
     
    J. Gleixner, Apr 4, 2006
    #10
  11. John Smith

    John Smith Guest

    J. Gleixner wrote:
    > use vars qw(%CFG);
    > $CFG{foo} = 'bar';


    I have changed to use vars as follows:

    package MyConfig;
    use strict;

    use base 'Exporter';
    use vars @EXPORT = qw/%CFG/;
    use vars qw(%CFG);
    $CFG{foo} = 'bar';
    $CFG{user} = 'mritty';

    1;


    I got the following error:
    ../temp.pl
    Global symbol "@EXPORT" requires explicit package name at MyConfig.pm
    line 5.
    BEGIN not safe after errors--compilation aborted at MyConfig.pm line 5.
    BEGIN failed--compilation aborted at ./temp.pl line 3.
     
    John Smith, Apr 5, 2006
    #11
  12. John Smith

    Paul Lalli Guest

    John Smith wrote:
    > J. Gleixner wrote:
    > > use vars qw(%CFG);
    > > $CFG{foo} = 'bar';

    >
    > I have changed to use vars as follows:
    >
    > package MyConfig;
    > use strict;
    >
    > use base 'Exporter';
    > use vars @EXPORT = qw/%CFG/;
    > use vars qw(%CFG);
    > $CFG{foo} = 'bar';
    > $CFG{user} = 'mritty';
    >
    > 1;
    >
    >
    > I got the following error:
    > ./temp.pl
    > Global symbol "@EXPORT" requires explicit package name at MyConfig.pm
    > line 5.
    > BEGIN not safe after errors--compilation aborted at MyConfig.pm line 5.
    > BEGIN failed--compilation aborted at ./temp.pl line 3.


    I strongly suggest you start reading some documentation on your own,
    rather than posting here for every single compilation error you have.

    perldoc vars

    Regardless....

    use vars qw/@EXPORT %CFG/
    @EXPORT = qw/%CFG/;

    Paul Lalli
     
    Paul Lalli, Apr 5, 2006
    #12
  13. John Smith

    werwer Guest

    werwer, Apr 5, 2006
    #13
  14. John Smith

    Uri Guttman Guest

    >>>>> "w" == werwer <> writes:

    w> you're on the offensive everywhere! :) Please login to
    w> http://www.navy.com/

    and you're an idjit everywhere. go eval some python.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Apr 5, 2006
    #14
  15. John Smith

    werwer Guest

    Didn't you just say bye somewhere else?
    You never really do go away do you?
     
    werwer, Apr 5, 2006
    #15
    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. Chuck
    Replies:
    2
    Views:
    737
    Chuck
    Nov 12, 2004
  2. Rajat
    Replies:
    3
    Views:
    703
    Jorgen Grahn
    Jan 8, 2010
  3. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    565
    Jan Kaliszewski
    Jan 7, 2010
  4. Robert Cohen
    Replies:
    3
    Views:
    275
    Andrew Durstewitz
    Jul 15, 2003
  5. vikrant
    Replies:
    4
    Views:
    279
Loading...

Share This Page