quick scope question

Discussion in 'Perl Misc' started by 1172948251@noid.net, Mar 3, 2007.

  1. Guest

    I want to maintain my $version variable in a single place
    within the script and I want it available in a BEGIN block
    that is a different package from the rest of the script. Is
    this possible? Here's what I mean:

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

    #!/usr/bin/perl

    BEGIN {
    package Some::package;
    my $version = '1';
    print "version is $version\n";
    }

    my $version = $Some::package::version;
    print ("version is $version\n"); # not initialized ???
    , Mar 3, 2007
    #1
    1. Advertising

  2. Mumia W. Guest

    On 03/03/2007 01:03 PM, wrote:
    > I want to maintain my $version variable in a single place
    > within the script and I want it available in a BEGIN block
    > that is a different package from the rest of the script. Is
    > this possible? Here's what I mean:
    >
    > ---------------
    >
    > #!/usr/bin/perl
    >
    > BEGIN {
    > package Some::package;
    > my $version = '1';


    Use "our" instead:

    our $version = '1';

    > print "version is $version\n";
    > }
    >
    > my $version = $Some::package::version;
    > print ("version is $version\n"); # not initialized ???
    >


    When you want a variable to be package scoped, use "our":

    perldoc -f our
    Mumia W., Mar 3, 2007
    #2
    1. Advertising

  3. Guest

    On Mar 3, 12:16 pm, "Mumia W." <paduille.4060.mumia.w
    > wrote:
    > On 03/03/2007 01:03 PM, wrote:
    >
    > > I want to maintain my $version variable in a single place
    > > within the script and I want it available in a BEGIN block
    > > that is a different package from the rest of the script. Is
    > > this possible? Here's what I mean:

    >
    > > ---------------

    >
    > > #!/usr/bin/perl

    >
    > > BEGIN {
    > > package Some::package;
    > > my $version = '1';

    >
    > Use "our" instead:
    >
    > our $version = '1';
    >
    > > print "version is $version\n";
    > > }

    >
    > > my $version = $Some::package::version;
    > > print ("version is $version\n"); # not initialized ???

    >
    > When you want a variable to be package scoped, use "our":
    >
    > perldoc -f our


    Using "our" works perfectly... Thanks!
    , Mar 3, 2007
    #3
  4. <> wrote:
    >
    > I want to maintain my $version variable in a single place
    > within the script and I want it available in a BEGIN block
    > that is a different package from the rest of the script. Is
    > this possible? Here's what I mean:
    >
    > ---------------
    >
    > #!/usr/bin/perl
    >
    > BEGIN {
    > package Some::package;
    > my $version = '1';

    ^^
    ^^

    See:

    "Coping with Scoping":

    http://perl.plover.com/FAQs/Namespaces.html


    > print "version is $version\n";
    > }
    >
    > my $version = $Some::package::version;
    > print ("version is $version\n"); # not initialized ???
    >



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 4, 2007
    #4
    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. Paul Opal
    Replies:
    12
    Views:
    931
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    652
    Patricia Shanahan
    Sep 13, 2005
  3. JKop
    Replies:
    11
    Views:
    856
  4. Steven T. Hatton
    Replies:
    9
    Views:
    459
  5. Xah Lee
    Replies:
    0
    Views:
    2,223
    Xah Lee
    Feb 26, 2009
Loading...

Share This Page