requires explicit package name

Discussion in 'Perl Misc' started by sam, Jan 6, 2005.

  1. sam

    sam Guest

    Hi,

    I don;t know what is wrong with the following simple perl code:

    #!/usr/bin/perl

    use strict;
    use warnings;

    local %hash1 = ('sales_subtotal'=>100);
    local %hash2 = ('sales_subtotal'=>150);


    When I execute the above code, I got the following error:
    Global symbol "%hash1" requires explicit package name at hashs.pl line 6.
    Global symbol "%hash2" requires explicit package name at hashs.pl line 7.


    Thanks
    Sam
    sam, Jan 6, 2005
    #1
    1. Advertising

  2. sam wrote:

    > local %hash1 = ('sales_subtotal'=>100);
    > local %hash2 = ('sales_subtotal'=>150);


    Have a look at "perldoc -f local", especially the first paragraph.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Jan 6, 2005
    #2
    1. Advertising

  3. sam

    sam Guest

    Bernard El-Hagin wrote:

    > sam <> wrote:
    >
    >
    >>Hi,
    >>
    >>I don;t know what is wrong with the following simple perl code:
    >>
    >>#!/usr/bin/perl
    >>
    >>use strict;
    >>use warnings;
    >>
    >>local %hash1 = ('sales_subtotal'=>100);
    >>local %hash2 = ('sales_subtotal'=>150);

    >
    >
    >
    > Why do you think you need local() there?
    >
    >
    >
    >>When I execute the above code, I got the following error:
    >>Global symbol "%hash1" requires explicit package name at hashs.pl
    >>line 6. Global symbol "%hash2" requires explicit package name at
    >>hashs.pl line 7.

    >
    >
    >
    > Does this help (from perldoc perlsub)
    >
    >
    > ==============
    > A `local' just gives temporary values to global (meaning package)
    > variables. It does *not* create a local variable. This is known as
    > dynamic scoping.
    > ==============
    >

    Yup. thanks
    >
    > ?
    >
    >
    sam, Jan 6, 2005
    #3
  4. sam

    Mothra Guest

    sam <> wrote in news:crj0rc$15p0$:

    > use strict;
    > use warnings;
    >
    > local %hash1 = ('sales_subtotal'=>100);
    > local %hash2 = ('sales_subtotal'=>150);
    >
    >
    > When I execute the above code, I got the following error:
    > Global symbol "%hash1" requires explicit package name at hashs.pl line 6.
    > Global symbol "%hash2" requires explicit package name at hashs.pl line 7.
    >

    use 'my' to scope a variable:

    my %hash1 = ('sales_subtotal'=>100);
    my %hash2 = ('sales_subtotal'=>150);
    Mothra, Jan 12, 2005
    #4
  5. sam <> wrote:

    > I don;t know what is wrong with the following simple perl code:



    See the documentation for the strict pragma. You are
    running afoul of the "strict vars" part:

    perldoc strict


    > use strict;



    Here you promise to declare all of your variables, or to use the
    full package name of the variable...


    > local %hash1 = ('sales_subtotal'=>100);



    .... and here you use a variable doing neither.

    You have broken your promise, perl refuses to run your program.


    Why are you using package variables instead of lexical variables?

    If you can use lexical variables, then you declare them like this:

    my %hash1 = ('sales_subtotal'=>100);

    If you really do need package variables (unlikely) then you declare
    them like this:

    our %hash1 = ('sales_subtotal'=>100);

    or, you use its full name:

    %main::hash1 = ('sales_subtotal'=>100);


    > When I execute the above code, I got the following error:
    > Global symbol "%hash1" requires explicit package name at hashs.pl line 6.



    All of the messages that perl might issue are documented in

    perldoc perldiag

    You should look up messages there as a *first* step in troubleshooting:

    =item Global symbol "%s" requires explicit package name

    (F) You've said "use strict vars", which indicates that all variables
    must either be lexically scoped (using "my"), declared beforehand using
    "our", or explicitly qualified to say which package the global variable
    is in (using "::").


    If you don't know what this "package" vs. "lexical" thing is,
    then please see:

    "Coping with Scoping":

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 12, 2005
    #5
    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. xxx
    Replies:
    0
    Views:
    809
  2. Replies:
    1
    Views:
    482
    Fredrik Lundh
    Jul 5, 2006
  3. snowdog
    Replies:
    2
    Views:
    364
    Joe Smith
    Nov 7, 2004
  4. Replies:
    5
    Views:
    993
    Sherm Pendley
    Jan 28, 2005
  5. mike
    Replies:
    3
    Views:
    655
    Dr.Ruud
    Apr 30, 2008
Loading...

Share This Page