importing a hash from package

Discussion in 'Perl Misc' started by Jeff Thies, Sep 15, 2003.

  1. Jeff Thies

    Jeff Thies Guest

    I'm sure this is easy, but I'm not finding it...

    I have a package with a heash in it:

    package VARIABLES;

    my %HASH;
    $HASH{a}='a';

    $some_scalar='some_scalar'

    ....

    I want to refer to that hash elsewhere.

    I know I can do this:

    my $local_some_scalar=VARIABLE::$some_scalar;

    But, how do I get at HASH? Do I have to write a reference to it in
    package VARIABLES?

    Cheers,
    Jeff
     
    Jeff Thies, Sep 15, 2003
    #1
    1. Advertising

  2. Jeff Thies <> wrote:
    > package VARIABLES;
    >
    > my %HASH;
    > $HASH{a}='a';
    >
    > $some_scalar='some_scalar'
    >
    > ...
    >
    > I want to refer to that hash elsewhere.
    >
    > I know I can do this:
    >
    > my $local_some_scalar=VARIABLE::$some_scalar;

    ^^^^^^^^^^^^^^^^^^^^^^

    That's not quite right.

    > But, how do I get at HASH? Do I have to write a reference to it
    > in package VARIABLES?


    The difference is between lexical (my) variables, which you can't
    use outside the scope where they were declared, and package variables,
    which are global.

    You can use our() to declare package variables.

    our %HASH; # %VARIABLES::HASH

    Is that what you were after?

    --
    Steve
     
    Steve Grazzini, Sep 15, 2003
    #2
    1. Advertising

  3. Jeff Thies

    Jeff Thies Guest

    Steve Grazzini wrote:
    >
    > Jeff Thies <> wrote:
    > > package VARIABLES;
    > >
    > > my %HASH;
    > > $HASH{a}='a';
    > >
    > > $some_scalar='some_scalar'
    > >
    > > ...
    > >
    > > I want to refer to that hash elsewhere.
    > >
    > > I know I can do this:
    > >
    > > my $local_some_scalar=VARIABLE::$some_scalar;

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


    oops!

    >
    > That's not quite right.
    >
    > > But, how do I get at HASH? Do I have to write a reference to it
    > > in package VARIABLES?

    >
    > The difference is between lexical (my) variables, which you can't
    > use outside the scope where they were declared, and package variables,
    > which are global.
    >
    > You can use our() to declare package variables.
    >
    > our %HASH; # %VARIABLES::HASH
    >
    > Is that what you were after?


    Yes, thanks!

    Jeff
    >
    > --
    > Steve
     
    Jeff Thies, Sep 15, 2003
    #3
  4. Jeff Thies <> wrote:
    > Steve Grazzini wrote:


    >> The difference is between lexical (my) variables, which you can't
    >> use outside the scope where they were declared, and package variables,
    >> which are global.
    >>
    >> You can use our() to declare package variables.
    >>
    >> our %HASH; # %VARIABLES::HASH
    >>
    >> Is that what you were after?

    >
    > Yes,



    Then see also:

    "Coping with Scoping":

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 15, 2003
    #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. mhk
    Replies:
    1
    Views:
    625
    Chris Smith
    Nov 28, 2003
  2. Dave
    Replies:
    2
    Views:
    482
  3. plb
    Replies:
    2
    Views:
    363
  4. rp
    Replies:
    1
    Views:
    557
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    644
    David A. Black
    Jul 2, 2008
Loading...

Share This Page