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

I am using perl 5.005_02 and 'use strict'.

     local(%querystring) = split /&=/, $ENV{QUERY_STRING};

produces the following error message:

     Global symbol "%querystring" requires explicit package name.

If I change 'local' to 'my' everything works great. If I specify '%main::querystring' everything also works great.

My question is, why does 'local' have a different syntax than 'my'? Is this a bug?

If so, should I bother to report it, since 5.005_02 is no longer current?

Who does one report perl bugs to anyway?

Replies are listed 'Best First'.
Re: 'use strict' Rejecting Local Hash
by chromatic (Archbishop) on Dec 28, 2000 at 04:52 UTC
    local doesn't actually create a local variable. That's what my does.

    At least, that's what perlsub says.

    Here's what I tried:

    #!/usr/bin/perl -w use strict; # local %hash; # gives error my %hash; # no error { local %hash; # no error, if %hash previously declared with my # local %hash; # error if %hash previously undeclared }
    local and my are completely different. What local does is, within its scope, save the previous value of a defined variable, restoring it when you leave the enclosing scope. Dominus has a nice article on the uses of local, and I'll see if I can find it before someone else posts a pointer.

    Update: chipmunk and tilly are both right. I should have put use vars ( %hash ); instead of my. But the main point still stands.

          local %hash;    # no error, if %hash previously declared with my Actually, if %hash has previously been declared with my, that will generate an error: Can't localize lexical variable %hash ... Lexical variables cannot be localized. local() doesn't have a lexical effect, so it wouldn't really make sense if they could.
        Actually lexical variables can be localized.
        use strict; my %hash = qw(hello world); print "$hash{hello}\n"; { local $hash{hello} = "WORLD"; print "$hash{hello}\n"; } print "$hash{hello}\n";
        There is no technical reason why this isn't doable for the original hash as well. The only reason why it cannot currently be done is that it is considered a likely cause of confusion.
Re: 'use strict' Rejecting Local Hash
by ichimunki (Priest) on Dec 28, 2000 at 04:55 UTC
    If you are attempting to implement my code from this node, PLEASE DON'T.

    Even if you arent, and you are being paid to do this, you owe it to your employers to take the five minutes to read perlman::CGI to learn how to extract $ENV in CGI without a lot of hand-coded parsing. You will save yourself (or whoever maintains this next) a lot of future headaches and worries.

    As to your specific question, I suggest re-reading perlfunc::my and perlfunc::local. And here is an offsite link to a great article called, "Coping with Scoping" that has a great walkthrough on this topic.

    Added: If you aren't being paid to do this, then you owe it to yourself to get in the habit of using modules. I wish I had done so a LOT sooner. The CGI module in particular is very good for a gradual introduction to working with larger modules, since it is not an all or nothing type of module. You can easily start with query string parsing and then work up from there.
(Ovid) Re: 'use strict' Rejecting Local Hash
by Ovid (Cardinal) on Dec 28, 2000 at 04:48 UTC
    It looks like you're trying to do CGI form processing. If so, use CGI. It's much simpler.

    The reason why you don't get an error with %main::querystring is because strict does not require you to declare fully qualified package variables.

    You can report perl bugs by typing "perlbug" at the command line, but you shouldn't do so unless you can guarantee that it's a bug. Seldom are bugs really bugs in Perl. If you think it's a bug, reduce it down to the absolutely smallest piece of code that replicates the bug. Most of the time, you'll find that the bug is really in your code rather than in Perl.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re (tilly) 1: 'use strict' Rejecting Local Hash
by tilly (Archbishop) on Dec 28, 2000 at 04:58 UTC
    What does strict say? Oh, it says that local won't pass. And local tells you rather bluntly that you probably should be using my.

    Of course the site documentation is out of date, you can get documentation that is current for your site with:

    perldoc strict perldoc -f local perldoc -f my
    As for the reason for the behaviour, it is because local came first and has to be supported for backwards compatibility, and my is more recent. So we cannot change what local does and are limited to scattering around various hints (say in what strict does, in what the documentation says, etc) that my is what people are looking for.

    Good monk Dominus wrote a very good article explaining all of this called Coping with Scoping. I highly recommend it.

Re: 'use strict' Rejecting Local Hash
by merlyn (Sage) on Dec 28, 2000 at 05:44 UTC
Re: 'use strict' Rejecting Local Hash
by sierrathedog04 (Hermit) on Dec 28, 2000 at 06:17 UTC
    Thanks everyone for the fast and on-point responses to my question. Not only do I now understand that 'my' is almost always preferable to 'local', but some kind souls suggested that I start using Lincoln Stein's CGI module. It will indeed save me a lot of time.