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

#!/usr/bin/perl -w use 5.10.1; use strict; use warnings; @wt = (1,2,3,4); print $wt[2];

The above code was used on Windows 7. When I use the dos prompt to type

perl pArray.plx </p> and hit enter, I get the the following error mes +sage: <code> C:\strawberry\perl\programs>perl pArray.plx Global symbol "@wt" requires explicit package name at pArray.plx line +6. Global symbol "@wt" requires explicit package name at pArray.plx line +8. Execution of pArray.plx aborted due to compilation errors.

Replies are listed 'Best First'.
Re: Global Symbol issue while using arrays
by ikegami (Patriarch) on Jun 07, 2011 at 05:58 UTC

    You never declared @wt.

    #!/usr/bin/perl -w use 5.10.1; use strict; use warnings; my @wt = (1,2,3,4); print $wt[2];

    Passing -Mdiagnostics to Perl will explain many of these.

Re: Global Symbol issue while using arrays
by davido (Cardinal) on Jun 07, 2011 at 06:58 UTC

    When you take the time to type (or cut/paste) the phrase, "use strict;", or as someone else pointed out, "use Modern::Perl;", you are asking for Perl to enforce a number of rules that aim to improve your code's quality. The first rule is no symbolic references. The second rule is declare your variables. And the most common way to do that is to declare lexical variables, with "my".

    I would encourage you to not put anything in your scripts that you don't know what function is being served. On the other hand, I would encourage you to put use strict; in every one of your scripts. Now we have an apparent contradiction: Don't include things you don't know what they do, and do use strict always. How to resolve that dichotomy? Read strict, Private Variables via my, and use, in that order. ;)

    And as pointed out earlier, Modern::Perl is superset to strict.

    ....and as someone is sure to point out, rules are sometimes made to be broken.


    Dave

      thanks davido, can u tell me where i can get info on -mdiagnostics (one rely asked me to do that). I googled around and could not find much info.

        Two places: -M is discussed in perlrun, and diagnostics.

        The -M causes Perl to invoke the module named immediately after the M. And diagnostics is a pragma that causes errors and warnings to become a lot more verbose (and presumably more helpful).


        Dave

Re: Global Symbol issue while using arrays
by Khen1950fx (Canon) on Jun 07, 2011 at 06:11 UTC
    Since you are using 5.10.1, you can replace the three lines
    use 5.10.1; use strict; use warnings;
    with Modern::Perl like this:
    #!/usr/bin/perl use Modern::Perl; my(@wt) = (1,2,3,4); print $wt[2], "\n";

      Hi Khen, thanks for the reply. I did what you told me and I got this:

      C:\strawberry\perl\programs>perl pArraym.plx Can't locate Modern/Perl.pm in @INC (@INC contains: C:/strawberry/perl +/lib C:/st rawberry/perl/site/lib C:\strawberry\perl\vendor\lib .) at pArraym.plx + line 4. BEGIN failed--compilation aborted at pArraym.plx line 4.
        diagnostics/splain
        Can't locate Modern/Perl.pm in @INC (@INC contains: C:/strawberry/perl +/lib C:/strawberry/perl/site/lib C:\strawberry\perl\vendor\lib .) a +t pArraym.plx line 4 (#1) (F) You said to do (or require, or use) a file that couldn't be found. Perl looks for the file in all the locations mentioned in @ +INC, unless the file name included the full path to the file. Perhaps +you need to set the PERL5LIB or PERL5OPT environment variable to say w +here the extra library is, or maybe the script needs to add the library + name to @INC. Or maybe you just misspelled the name of the file. See perlfunc/require and lib. BEGIN failed--compilation aborted at pArraym.plx line 4 (#2) (F) An untrapped exception was raised while executing a BEGIN subroutine. Compilation stops immediately and the interpreter is exited.
        Maybe you wish to install Modern::Perl, or ignore Khen1950fx and stick with what you have.