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

Hey all, I'm a total newbie to Perl, and have above average programming experience. The Problem:I was trying to figgure out what /exactly/ the use strict; line does and why Komodo doesn't like its existence in the following program pasted below. The Error Message: "Global symbol "$name" requires explicit package name." Program:
#!/usr/bin/perl -w use strict; print "Hello, I am Komodo what is your name? "; $name = <STDIN>; chomp $name; print "Hello, $name!\n";
The man pages were good in telling me that use strict; was the "safest" way to run code but didn't explain why I was getting "Global symbol "$name" requires explicit package name." Thanks -Pete

20050112 Edit by castaway: Changed title from 'Total Noob....'

Replies are listed 'Best First'.
Re: Explanation of 'use strict;'
by friedo (Prior) on Jan 11, 2005 at 21:25 UTC
    ( Accidentally posted this in the dupe thread. Copied it here. )

    You're getting that error because you're defining a package-global variable ($name) which is not allowed under strict. (Well, it's allowed, but it requires an explicit package name, which probably does not surprise you.)

    The way to do what you want is to create a lexical variable with my.

    #!/usr/bin/perl -w use strict; print "Hello, I am Komodo what is your name? "; my $name = <STDIN>; chomp $name; print "Hello, $name!\n";

    For more, see Coping with Scoping. Required reading for all n00bz.

Re: Explanation of 'use strict;'
by kutsu (Priest) on Jan 11, 2005 at 21:28 UTC

    Strict is one of the hurdles everyone must, or at least should, face at some time. Take a look at Use strict warnings and diagnostics or die, it should help you find your answer

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Re: Explanation of 'use strict;'
by radiantmatrix (Parson) on Jan 11, 2005 at 21:45 UTC

    friedo is right on the money. Komodo is complaining becasue as you code, Komodo attempts to check your syntax in the same way that the Perl interpreter would if you ran the code at that moment.

    As for exactly what use strict does, read the docs. But, in general, strict disallows many things that are considered sloppy or frequently lead to bugs. In your case, $name has no explicit scope; without strict, Perl just assumes it is a package variable, which may or may not be what you wanted. Because it is ambiguous, strict is generating an error and asking you to either explicitly use the package name or otherwise explicitly scope the variable.

    radiantmatrix
    require General::Disclaimer;
    s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}

what does 'global variable requires package name' mean (was: Re: Total Noob....)
by Joost (Canon) on Jan 11, 2005 at 22:13 UTC
      Tiny hint, but, give your post a better title next time, like "what does 'global variable requires package name' mean"?


      ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
      =~y~b-v~a-z~s; print
Re: Explanation of 'use strict;'
by Anonymous Monk on Jan 12, 2005 at 11:00 UTC
    replace "$name = <STDIN>" with "my $name = <STDIN>". This way you tell perl you know the variable didn't exist before that line. If not, "use strict" will warn you to prevent you from trying to access an unknown variable.
Re: Explanation of 'use strict;'
by Tanktalus (Canon) on Jan 11, 2005 at 21:25 UTC

    Hello! Welcome. May I suggest also learning how to use PM to your best advantage? For example, enclose your code with <code> and </code> as such:

    #!/usr/bin/perl -w use strict; print "Hello, I am Komodo what is your name? "; $name = <STDIN>; chomp $name; print "Hello, $name!\n";
    This makes it much easier for the monks to read. Now that we've done that, I can point you to strict and then to my. Strict basically enforces some guidelines which manage to catch common errors, such as typos. In your example, it wonders if "$name" is valid because you did not pre-declare it. By using "my", you can declare this is a new variable, and strict is satisfied:
    #!/usr/bin/perl -w use strict; print "Hello, I am Komodo what is your name? "; my $name = <STDIN>; chomp $name; print "Hello, $name!\n";
    And now strict is much happier.

Re: Explanation of 'use strict;'
by Eimi Metamorphoumai (Deacon) on Jan 11, 2005 at 21:25 UTC
    perldoc strict has all the gory details, but the short answer is that one of the things "use strict;" does it prevent you from using variable names you didn't declare. The benefit is that if you assign to "$name" and then later read from "$naem", perl will catch the typo. The downside is that you need to declare the variables you use. In your case, the simplest thing to do is replace the line "$name = <STDIN>;" with the line "my $name = <STDIN>;" and you're good.
A reply falls below the community's threshold of quality. You may see it by logging in.