Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

"my" declaration problem

by Hosen1989 (Scribe)
on Apr 25, 2017 at 11:17 UTC ( [id://1188833]=perlquestion: print w/replies, xml ) Need Help??

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

Dear ALL,

I was debug some old dirty script of mine (which without strict nor warnings ^_^), and faced this issue:

As you can see in case(1), variable $me had been declared more than once, and to output of this script is: []

#----------[ CASE(1) ]----------# my $v_place = 'home'; my $me = 'moving'; my $me = 'at home' if($v_place =~ m/^home/); my $me = 'at work' if($v_place =~ m/^work/); print "[$me]"; __END__ output: [] #-------------------------------#

But after correct the declaration problem as in case(2), we got the correct output: [at home].

#----------[ CASE(2) ]----------# my $v_place = 'home'; my $me = 'moving'; $me = 'at home' if($v_place =~ m/^home/); $me = 'at work' if($v_place =~ m/^work/); print "[$me]"; __END__ output: [at home] #-------------------------------#

Now,can any monk explain me just what happen here?

BR

Hosen

Replies are listed 'Best First'.
Re: "my" declaration problem
by Eily (Monsignor) on Apr 25, 2017 at 11:56 UTC

    The note on statement modifiers in the doc states:

    NOTE: The behaviour of a my, state, or our modified with a statement modifier conditional or loop construct (for example, my $x if ... ) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
    So even if there only was the last of the three declarations, it would still be wrong.

Re: "my" declaration problem
by davies (Prior) on Apr 25, 2017 at 11:29 UTC

    Warnings would tell you that your repeated "my" masked a previous declaration. When you use "my", a new variable is created. If several of the same name are created in the same scope, only the last to be declared is accessible. Therefore, $me is "at work" if $v_place is "work" (simplified, I know), which it isn't. Therefore $me exists but has no value, which is what you are seeing.

    Regards,

    John Davies

Re: "my" declaration problem
by talexb (Chancellor) on Apr 25, 2017 at 14:32 UTC

    As has already been pointed out, just get into the habit of always using strict and warnings on all your code. It lets Perl remind you about anything that's a bit dodgy.

    Second, it's a bad idea to mix a declaration with anything more than a straight initialization. To me, that has a bad 'code smell'. And Perl also thinks it's a bad idea.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: "my" declaration problem
by karlgoethebier (Abbot) on Apr 26, 2017 at 11:53 UTC
    "...explain..."
    #!/usr/bin/env perl use strict; use warnings; use feature qw(say); my $immortal = q(Connor MacLeod); my $immortal = q(The Kurgan); say qq("There can be only one"); __END__ karls-mac-mini:monks karl$ ./my.pl "my" variable $immortal masks earlier declaration in same scope at ./m +y.pl line 8. "There can be only one"

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    Furthermore I consider that Donald Trump must be impeached as soon as possible

Re: "my" declaration problem
by Anonymous Monk on Apr 25, 2017 at 11:29 UTC
    Turn on warnings and read the warning generated
Re: "my" declaration problem
by LanX (Saint) on Apr 25, 2017 at 17:02 UTC
    > Now,can any monk explain me just what happen here?

    because ...

    > without strict nor warnings ^_^

    Or differently phrased:

    Repeated declarations of the same variable in the same scope make no sense.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Repeated declarations of the same variable in the same scope make no sense.
      In bad style and obfuscated, sure, but is it nonsensical? Redeclaration and reassignment are not the same when destructors or side-effects are involved. I can imagine this kind of situation arising with "templated" code where sections are copy-paste or somesuch. Job security FTW!

        OK ... Please provide an example where the code is not broken.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1188833]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found