Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: The difference between my and local

by nashr (Novice)
on Dec 20, 2004 at 18:47 UTC ( #416272=note: print w/replies, xml ) Need Help??

in reply to The difference between my and local

This brings me to the problem I've been having. I have a "my" variable that I want to update within an if..elsif, but it does not get updated.
my $a = "TEST'; if ($x == 1) { $a = "one"; } elsif ($x == 2) { $a = "TWO"; } else { $a = "EMPTY"; }
However, $a is not getting updated. I'm obviously overlooking something in either declaring the variable to be global, or some way to reference the variable directly. I just don't quite know where to look. Anyone???

Replies are listed 'Best First'.
Re^2: The difference between my and local
by steves (Curate) on Dec 20, 2004 at 19:42 UTC

    That code doesn't compile. TEST starts with double quotes and ends with a single quote. If I fix that, this works as expected for me. Here's a version you can run with your $x argument given on the command line:

    use strict; my $x = shift(@ARGV); my $a = "TEST"; if ($x == 1) { $a = "one"; } elsif ($x == 2) { $a = "TWO"; } else { $a = "EMPTY"; } print "a=$a\n";

      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 line 4.

        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.


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

        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}

        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.

        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.
        in original code, there's no "my" for the "global" variable :)
Re^2: The difference between my and local
by Anonymous Monk on Feb 05, 2014 at 04:14 UTC
    The first line contains an error. TEST is enclosed in a double and a single quotation mark.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://416272]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2021-10-20 11:20 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (81 votes). Check out past polls.