importing a hash from package

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default importing a hash from package

    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 Guest

  2. Similar Questions and Discussions

    1. What's up with the mx7 hash ?
      CF Documentation says the Hash function in CFMX7 using the SHA algorithm generates a 28-character character string. Executing this code: ...
    2. hash of hash of array slices
      This works Foreach ( @{$hash{$key1}{$key2}} ) This does note Foreach ( @{($hash{$key1}{$key2})} ) This gives me this error .... Can't...
    3. Sort a hash based on values in the hash stored as arrays of hashes
      Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {...
    4. Hash of Hash
      Greetings, I am attempting to make a hash of hashes or something equivalent but can't seem to get it working properly. Here is what I have so...
    5. Another reference question (hash of hash references)
      beginners, I am trying to build a hash of hash references. My problem is that I need to be able to add a key/value pair to the internal hashes......
  3. #2

    Default Re: importing a hash from package

    Jeff Thies <cyberjeff@sprintmail.com> 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 Guest

  4. #3

    Default Re: importing a hash from package



    Steve Grazzini wrote:
    >
    > Jeff Thies <cyberjeff@sprintmail.com> 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 Guest

  5. #4

    Default Re: importing a hash from package

    Jeff Thies <cyberjeff@sprintmail.com> 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":

    [url]http://perl.plover.com/FAQs/Namespaces.html[/url]


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139