m_alper_yildiz has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, i'm new PERL learner and i'm stuck.I dont understand what does this " "some variable" requires explicit package name" warning mean?

#!\usr\bin\perl use strict; use warnings; my %ders_kodu = ( 324 => "reaktor", 342 => "isi", ); print $ders_kodu{"324"};

Can someone help me with this ?

Replies are listed 'Best First'.
Re: Explicit package name Warning!!!
by tobyink (Canon) on Jun 04, 2013 at 22:23 UTC

    That's not a warning, but a compilation error. (Warnings print out a complaint, but your script keeps running. Errors stop your script in its tracks.) However, I don't get that error running the code you posted.

    This error occurs if you use strict (in particular the vars component of strict) and attempt to use a variable which is not either (a) declared, or (b) includes a package name.

    $ perl -Mstrict -E'$hello = "Hi"; say $hello' Global symbol "$hello" requires explicit package name at -e line 1. Global symbol "$hello" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

    Solution 1: declare your variable with my or our. (Check the fine documentation for the difference between them.)

    $ perl -Mstrict -E'my $hello = "Hi"; say $hello' Hi $ perl -Mstrict -E'our $hello = "Hi"; say $hello' Hi

    Solution 2: include a package name in the variable's name.

    $ perl -Mstrict -E'$World::hello = "Hi"; say $World::hello' Hi

    Solution 3: don't use strict. (Using strict is generally a good idea, but sometimes you really do know what you're doing, and just want it to stop bothering you.)

    $ perl -Mstrict -E'no strict qw(vars); $hello = "Hi"; say $hello' Hi
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Explicit package name Warning!!!
by davido (Cardinal) on Jun 04, 2013 at 22:15 UTC

    It means that you have a typo in a variable name in the code you're running that isn't there in the code you posted.

    This message shows up because you violated strict 'vars' by using a variable name that hasn't been declared. It could be that you properly declared a variable, but later misspelled it.

    Add "use diagnostics;" toward the top of your script and the error message will become more verbosely explained.


    Dave

      Can you tell me please what should i change or add in this code? Where do i have typo?

        the code you posted has no errors!

        try use diagnostics if you're facing again cryptic error msgs.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        I cannot tell you exactly what you must change, because as I said in my original follow-up to your question, the code you posted doesn't contain the error you're reporting. However, I believe you are seeing an error message. So the code you're running has an error. The code you posted doesn't. You didn't copy and paste your buggy code; you re-typed it here and in so doing, avoided the typo that is causing the error in the code you're running.

        If you want your code to run just fine, copy what you typed here and paste it in place of your original code, because this code runs fine.


        Dave

        Yes. Copy the code you posted in your original question (aka OP, "Original Post", not to be confused... er, well, often confused because ambiguous with "Original Poster") and compare it, letter-by-letter, punct-by-punct; and space-by-space, if necessary, with the code that's giving you the problem. Doing so will reveal your typo.

        Then correct it or paste what you copied over your original code; add use strict; use warnings (not needed with 5.16 and higher update: if the script includes the line use 5.01n; where n is a relevant decimal digit) at the top, just after the hashbang. Voila; all should run as you expected, originally.

        Hint: when posting, copy and paste rather than retype. Doing so will save you the pain of discovering later that you've corrected an error or created a new one by retyping.


        If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: Explicit package name Warning!!!
by kcott (Archbishop) on Jun 05, 2013 at 05:20 UTC

    G'day m_alper_yildiz,

    Welcome to the monastery.

    I note in a number of the replies here, use of the diagnostics pragma is suggested. Instead of editing and rerunning your code, you can look up the verbose diagnostic messages in perldiag.

    There are a very large number of messages in perldiag so you'll probably need to search for the one you want. Be aware that variable parts of the message (e.g. the "some variable" in your post) are usually shown as "%s"; accordingly, just search for a significant, non-variable part (e.g. 'requires explicit package name' finds 'Global symbol "%s" requires explicit package name' but '"$x" requires explicit package name' finds no matches).

    Alternatively, you can feed the entire, unedited message to splain (see perldiag - The splain Program). Here's an example:

    $ splain /Users/ken/perl5/perlbrew/perls/perl-5.18.0t/bin/splain: Reading from +STDIN Global symbol "$x" requires explicit package name Global symbol "$x" requires explicit package name (#1) (F) You've said "use strict" or "use strict vars", which indicates + that all variables must either be lexically scoped (using "my" or +"state"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::"). ^C

    While you are learning Perl, the diagnostics pragma can be useful; however, it is generally not a good idea to retain it in production code. Here's a non-exhaustive list of reasons why not to use this pragma in production code:

    • The message is for the programmer; not the user.
    • Users often tend to overreact when confronted with copious error output.
    • Other important messages may be obscured (a "can't see the woods for the trees" scenario).
    • Log files can become bloated.

    -- Ken