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

I've already had some help from the chatterbox but things are still a bit hazy re: strict In the following all works OK until I switch strict on. I then get Multiple 'errors' along the lines of: Global symbol "$thingy" requires explicit package name at... Could anyone advise - I've included the code below. Thanks.
#!/usr/bin/perl -w use strict; ###@words = ("camel", "llama", "alpaca"); ### ### #or ### #@words = qw(camel llama alpaca); # qw acts to specify "" t +o each variable ### ### Above shows the use of an 'array' - the following describes an 'ha +sh' shown as %a (see 'pinky' for full program) %words = qw ( fred camel barney llama betty alpaca wilma alpaca ); print "What is your name? "; $name = <>; chomp $name; if ($name eq "pete") { print "Hello, $name! \n"; } else { print "Hello $name,\n "; #next section has 2 possible routes $secretword = $words{$name}; if (! defined $secretword) {$secretword = "groucho"; } # or # $secretword = $words{$name} || 'groucho'; # || = OR therf +ore this states # if names exisits then set it if not then all other = gro +ucho. print "What's the secret word ?\n"; $guess = <>; chomp ($guess); while ($guess ne $secretword) { print " Niet - try again, What is the secret word ? \n +"; $guess = <>; chomp ($guess); } }

Replies are listed 'Best First'.
Re: Strict Usage
by dragonchild (Archbishop) on Aug 22, 2001 at 17:12 UTC
    Where you have
    %words = qw ( fred camel barney llama betty alpaca wilma alpaca );
    That should be
    my %words = qw ( fred camel barney llama betty alpaca wilma alpaca );
    my declares a variable to be a lexical, which means "non-global". You have to declare all your variables the first time you use it to be 'strict', so to speak.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

      Lexical does not mean "non-global". As Dominus points out at Re: (s)coping with foreach, it means "pertaining to the text".

      Also note that when trying to import variables from other modules you should declare with the vars pragma rather than with my.

      For more on scoping I recommend Coping with Scoping by Dominus. It predates "our", but then again I don't like our much.

Re: Strict Usage
by Hofmator (Curate) on Aug 22, 2001 at 17:18 UTC

    Use strict is your friend! Once you got to know it ;-)

    You have to declare every variable you use in your script, normally you do this with my. So you'd write, e.g.:

    my %words; %words = (...); # or you can do the initialisation right away my %words = (...); # the same goes for the other variables my $name = <>;
    The other ways to 'declare' variables are with our and local and use vars. dominus has two excellent articles about this topic:

    -- Hofmator

Re: Strict Usage
by Masem (Monsignor) on Aug 22, 2001 at 17:12 UTC
    When you use strict you need to explicity declare all the variables that you use, as is typical of C or other languages. Not only does this improve programming practice, but also helps to catch typos in variable names (eg, using "$think" instead of "$thing"). This is easily done using the my operator. As a starting point, you can either use multiple 'my' statements for each variable, or one large one as shown below.
    my $var1; my $var2; my $var3; # # OR # my ( $var1, $var2, $var3 );
    The "my" function can be used on the left-hand side of an expression as well, but that tends to be an advanced technique to use.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Strict Usage
by Monky Python (Scribe) on Aug 22, 2001 at 17:16 UTC
    prepend "my" to all your variable definitions

    for example:

    my %words = qw ( fred camel barney llama betty alpaca wilma alpaca );

    MP

Re: Strict Usage
by herveus (Prior) on Aug 22, 2001 at 21:06 UTC
    Howdy!

    Note that the error message says that an explicit package name is required...
    One could go to the trouble of saying $main::thingy (even without 'use strict') to mean the thingy in the main symbol table. Sometimes this is real handy when you want to diddle package variables from outside the package. Some packages even document variables for you to use this way (Data::Dumper comes to mind).

    On the other hand, most of the time, you probably wanted to use "my" anyway...

    yours,
    Michael

      Before you think of doing this, please check out 100879 for more info. $main::thingy is extremely fraught with danger. As is, "diddling with package variables". (Any computer programming use of the word 'diddle' should be suspect from the outset!)

      ------
      /me wants to be the brightest bulb in the chandelier!

      Vote paco for President!