http://qs1969.pair.com?node_id=578736


in reply to Re^2: The difference between my and local
in thread The difference between my and local

I am getting following error, when I use strict and use $tt instead of $a My Code:-
use strict; my $tt = 3.14159; { local $tt = 3; print "In block, \$tt = $tt\n"; print "In block, \$::tt = $::tt\n"; } print "Outside block, \$tt = $tt\n"; print "Outside block, \$::tt = $::tt\n";
My Error:-
Can't localize lexical variable $tt at basic.pl line 4.

Replies are listed 'Best First'.
Re^4: The difference between my and local
by davorg (Chancellor) on Oct 17, 2006 at 11:54 UTC

    You'll probably get more attention if you post a new question, rather than attempting to resurrect such an old discussion.

    The error message you get seems pretty clear to me. You are attempting to use "local" on a lexical variable - and "local" can only be used on package variables.

    If you see an error message that you don't understand then it's a good idea to add "use diagnostics" to your code in order to see an expanded description of the error. In this case it says:

    You used local on a variable name that was previously declared as a lexical variable using "my". This is not allowed. If you want to localize a package variable of the same name, qualify it with the package name.

    But you need to ask yourself why you're doing it like this. What are you hoping to achieve by localising the variable here? I think that it's probably clearer if you create a new variable, which will be removed at the end of the block.

    use strict; my $tt = 3.14159; { my $inner_tt = 3; print "In block, \$tt = $tt\n"; print "In block, \$inner_tt = $inner_tt\n"; } print "Outside block, \$tt = $tt\n"; print "Outside block, \$inner_tt = $inner_tt\n";

    Your code is further confused by the references to $::tt. Those are references to a package variable called $tt. And you never declare a package variable of that name.

    You seem a bit confused about package and lexical variables. I recommend that you take the time to read Dominus' excellent article Coping with Scoping.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re^4: The difference between my and local
by shmem (Chancellor) on Oct 17, 2006 at 11:40 UTC

    Lexical (my) variables cannot be localized, as the error tells you. Use my for that :-P

    use strict; my $tt = 3.14159; { my $tt = 3; print "In block, \$tt = $tt\n"; print "In block, \$::tt = $::tt\n"; } print "Outside block, \$tt = $tt\n"; print "Outside block, \$::tt = $::tt\n";
    local is for globals. But there's a way to get lexical globs, I wonder whether these can be localized... ;-)
    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re^4: The difference between my and local
by ikegami (Patriarch) on Oct 17, 2006 at 15:11 UTC

    As you've already been told (by Perl and by fellow monks), you can't use local on a lexical (my) variable.

    Fellow monks have shown how you can achieve your goal using my instead of local. If you had to use local, then you need to make a package variable by that name first.

    use strict; my $tt = 3.14159; { our $tt; local $tt = 3; print "In block, \$tt = $tt\n"; print "In block, \$::tt = $::tt\n"; } print "Outside block, \$tt = $tt\n"; print "Outside block, \$::tt = $::tt\n";

    You should never have to use this, though. Always use my if possible.

Re^4: The difference between my and local
by Nazeerahamed (Initiate) on Oct 06, 2012 at 16:15 UTC
    You can only localize a "package global variable" but not a lexical variable i.e., declared using "my". A package global can be created using "use vars qw($pkg_gbl_bar1 $pkg_gbl_bar2)". Please change your code as below
    use strict;
    use vars qw($tt);
    $tt = 3.14159;
    {
    local $tt = 3;
    print "In block, \$tt = $tt\n";
    print "In block, \$::tt = $::tt\n";
    }
    print "Outside block, \$tt = $tt\n";
    print "Outside block, \$::tt = $::tt\n";

    Hope this helps you. If I am wrong, please let me know.
Re^4: The difference between my and local
by Anonymous Monk on Apr 15, 2018 at 04:32 UTC
    in original code, there's no "my" for the "global" variable :)