Calling a scalar from another script using "require"

Discussion in 'Perl Misc' started by Regent, Dec 29, 2003.

  1. Regent

    Regent Guest

    Hi, friends,

    Although I read several books before posting this, I still don't know how to declare a scalar in "common.pl" and use it directly in "script.pl":

    common.pl

    $root = "/root";


    script.pl

    require "common.pl"; # this line works
    print "$root"; # Global symbol "$root" requires explicit package name

    PS: I habitually use the -wT switch

    Regent
     
    Regent, Dec 29, 2003
    #1
    1. Advertising

  2. Regent wrote:
    > Although I read several books before posting this,


    Those cannot have been Perl books... ;-)

    > I still don't know how to declare a scalar in "common.pl" and use
    > it directly in "script.pl":
    >
    > common.pl
    >
    > $root = "/root";
    >
    >
    > script.pl
    >
    > require "common.pl"; # this line works
    > print "$root"; # Global symbol "$root" requires
    > # explicit package name
    >
    > PS: I habitually use the -wT switch


    Good. Since you get that error message, you are obviously using
    strictures too, which is also good. :)

    These are two ways to do what you want:

    our $root; # declares $root as a package global
    require "common.pl";
    print $root;

    or

    require "common.pl";
    print $main::root; # explicit package name

    (There is no need to quote the variable in the print statement.)

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

  3. On Mon, 29 Dec 2003 10:26:40 +0800, Regent <> wrote:
    > Hi, friends,
    >
    > Although I read several books before posting this, I still don't know
    > how to declare a scalar in "common.pl" and use it directly in
    > "script.pl":


    Typically require inserts the contents of the required script at the point
    of the require statement. However, I noticed that if I used "my $root",
    it only seemed to be in scope in the required script, and was
    uninitialized in main script.

    > common.pl
    >
    > $root = "/root";
    >
    >
    > script.pl
    >
    > require "common.pl"; # this line works
    > print "$root"; # Global symbol "$root" requires explicit package name
    >
    > PS: I habitually use the -wT switch
    >
    > Regent
    >


    If I just used -w switch (and put a newline in print "$root\n";),
    I got:

    Name "main::root" used only once: possible typo at ./mytest line 3.
    /root

    Inserting a use vars line got rid of the warning:

    use vars ('$root');
    require "common.pl";
    print "$root\n"

    If I used -wT, "." (current dir) was apparently excluded from @INC, so I
    had to use a full path to common.pl (same output).

    Tested in perl v5.6.1 built for i586-linux and v5.8.0 built for
    i586-linux-thread-multi

    --
    David Efflandt - All spam ignored http://www.de-srv.com/
    PS: All mail referencing [1-9]63\.(com|net) is automatically dropped due
    to excessive uncontrolled spam.
     
    David Efflandt, Dec 29, 2003
    #3
  4. David Efflandt <> wrote:

    > However, I noticed that if I used "my $root",
    > it only seemed to be in scope in the required script, and was
    > uninitialized in main script.



    That is what my() is _supposed to do.


    perldoc -f my

    ... local (lexically) to the enclosing block, file ...


    $root will only be in the scope of the "required file" since
    my() variables can never cross file boundaries.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 29, 2003
    #4
  5. Regent

    Regent Guest

    Re[1]: Calling a scalar from another script using "require"

    ---------------------
    Mon, 29 Dec 2003 03:58:37 +0100 £¬Gunnar Hjalmarsson wrote:
    >Those cannot have been Perl books... ;-)


    Well, those were books ABOUT Perl. The Perl books (to which I believe you refer) seem to discuss principles rather than specific problems, but I'm not much more than a newbie :((

    >These are two ways to do what you want:
    >
    > our $root; # declares $root as a package global


    You mean in common.pl? I put this line in common.pl, but it seemed I had also to declare $root in script.pl, otherwise I still got a "Global symbol "$root" requires explicit package name" error. Then I tried "my $root;" in script.pl, but got a "Use of uninitialized value", though I had already expected this error msg. The problem seems to be how do declare $root AGAIN in script.pl, which sounds weird to me :(

    > require "common.pl";
    > print $root;
    >
    >or
    >
    > require "common.pl";
    > print $main::root; # explicit package name


    This worked! Thanks indeed, but what does $main here mean? "::" means the use of modules to me. BTW, $main::root appears to be a somewhat clumsy ;)

    >
    >(There is no need to quote the variable in the print statement.)
    >
    >--
    >Gunnar Hjalmarsson
    >Email: http://www.gunnar.cc/cgi-bin/contact.pl
    >


    Regent
     
    Regent, Dec 30, 2003
    #5
  6. Regent wrote:
    > Gunnar Hjalmarsson wrote:
    >>These are two ways to do what you want:
    >>
    >> our $root; # declares $root as a package global

    >
    > You mean in common.pl?


    No, I mean in script.pl. Both my suggestions were intended for
    script.pl, while keeping common.pl as it is.

    >> require "common.pl";
    >> print $main::root; # explicit package name

    >
    > This worked! Thanks indeed, but what does $main here mean?


    $main::root means the global variable $root in package main.

    > "::" means the use of modules to me.


    When something begins with a '$' character, it's most likely a
    variable. You'd better do some reading:

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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 30, 2003
    #6
  7. Regent

    Regent Guest

    Re[1]: Calling a scalar from another script using "require"

    ------------------------
    Tue, 30 Dec 2003 01:58:53 +0100 £¬Gunnar Hjalmarsson wrote:
    >Regent wrote:
    >> Gunnar Hjalmarsson wrote:
    >>>These are two ways to do what you want:
    >>>
    >>> our $root; # declares $root as a package global

    >>
    >> You mean in common.pl?

    >
    >No, I mean in script.pl. Both my suggestions were intended for
    >script.pl, while keeping common.pl as it is.


    Okay, I see. If I use this method, I must declare all scalars in script.pl like this, right?

    >>> require "common.pl";
    >>> print $main::root; # explicit package name

    >>
    >> This worked! Thanks indeed, but what does $main here mean?

    >
    >$main::root means the global variable $root in package main.
    >
    >> "::" means the use of modules to me.

    >
    >When something begins with a '$' character, it's most likely a
    >variable. You'd better do some reading:


    Yup, I know the '$' well, but never used "::" in this way :p

    > http://www.perldoc.com/perl5.8.0/pod/perlmod.html
    >
    >--
    >Gunnar Hjalmarsson
    >Email: http://www.gunnar.cc/cgi-bin/contact.pl
    >


    ------------------------
    Regent
     
    Regent, Dec 30, 2003
    #7
  8. Regent wrote:
    > Gunnar Hjalmarsson wrote:
    >> Regent wrote:
    >>> Gunnar Hjalmarsson wrote:
    >>>>
    >>>> our $root; # declares $root as a package global
    >>>
    >>> You mean in common.pl?

    >>
    >> No, I mean in script.pl. Both my suggestions were intended for
    >> script.pl, while keeping common.pl as it is.

    >
    > Okay, I see. If I use this method, I must declare all scalars in
    > script.pl like this, right?


    Only those scalars you require from other files that way. Other
    variables should preferrably be declared with my().

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 30, 2003
    #8
  9. Regent

    Regent Guest

    Re[1]: Calling a scalar from another script using "require"

    ------------------------
    Mon, 29 Dec 2003 03:53:17 +0000 (UTC) £¬David Efflandt wrote:
    >If I just used -w switch (and put a newline in print "$root\n";),
    >I got:
    >
    >Name "main::root" used only once: possible typo at ./mytest line 3.
    >/root
    >
    >Inserting a use vars line got rid of the warning:
    >
    >use vars ('$root');
    >require "common.pl";
    >print "$root\n"
    >
    >If I used -wT, "." (current dir) was apparently excluded from @INC, so I
    >had to use a full path to common.pl (same output).


    Yeah, this is a problem. If common.pl isn't in one of the paths already indicated in @INC, I must specify its full path in every script, right?

    >Tested in perl v5.6.1 built for i586-linux and v5.8.0 built for
    >i586-linux-thread-multi
    >
    >--
    >David Efflandt - All spam ignored http://www.de-srv.com/
    >PS: All mail referencing [1-9]63\.(com|net) is automatically dropped due
    >to excessive uncontrolled spam.


    ------------------------
    Regent
     
    Regent, Dec 30, 2003
    #9
  10. Regent

    Jason Hood Guest

    Re: Re[1]: Calling a scalar from another script using "require"

    or you can include it in the @INC path

    use lib('PATH','ANOTHERPATH');

    J

    "Regent" <> wrote in message news:bsqli7$6sh$99.com...
    >
    > ------------------------
    > Mon, 29 Dec 2003 03:53:17 +0000 (UTC) £¬David Efflandt wrote:
    > >If I just used -w switch (and put a newline in print "$root\n";),
    > >I got:
    > >
    > >Name "main::root" used only once: possible typo at ./mytest line 3.
    > >/root
    > >
    > >Inserting a use vars line got rid of the warning:
    > >
    > >use vars ('$root');
    > >require "common.pl";
    > >print "$root\n"
    > >
    > >If I used -wT, "." (current dir) was apparently excluded from @INC, so I
    > >had to use a full path to common.pl (same output).

    >
    > Yeah, this is a problem. If common.pl isn't in one of the paths already indicated in @INC, I must specify its full path in every script, right?
    >
    > >Tested in perl v5.6.1 built for i586-linux and v5.8.0 built for
    > >i586-linux-thread-multi
    > >
    > >--
    > >David Efflandt - All spam ignored http://www.de-srv.com/
    > >PS: All mail referencing [1-9]63\.(com|net) is automatically dropped due
    > >to excessive uncontrolled spam.

    >
    > ------------------------
    > Regent
    >
    >



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.556 / Virus Database: 348 - Release Date: 26/12/2003
     
    Jason Hood, Dec 30, 2003
    #10
    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. Replies:
    1
    Views:
    336
    Chris Rebert
    Feb 18, 2009
  2. Robert Cohen
    Replies:
    3
    Views:
    310
    Andrew Durstewitz
    Jul 15, 2003
  3. Clint Olsen
    Replies:
    6
    Views:
    408
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  4. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    194
    Arndt Jonasson
    Jan 27, 2005
  5. Martin Landa
    Replies:
    6
    Views:
    105
    Cameron Simpson
    Mar 26, 2014
Loading...

Share This Page