Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

My my

by Saxium (Initiate)
on Apr 13, 2015 at 10:00 UTC ( [id://1123258]=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl use strict 'vars'; my $x = 'a'; printme(); my $x = 'b'; printme (); sub printme( ) { print "x: $x\n"; }
outputs
x: x: b

Can some help explain why the first my statement is effectively dropped?

Replies are listed 'Best First'.
Re: My my
by roboticus (Chancellor) on Apr 13, 2015 at 10:33 UTC

    Saxium:

    You're creating *two* variables named $x. (That's what the "...masks earlier declaration..." message that monkey boy pointed out means.) The printme subroutine uses the second one, as it's the one in scope at the point where it's compiled. At the first call, the second $x variable has nothing in it, so you don't get a value in the print statement. *Then* you're assigning 'b' to it, calling printme, at which point it prints "b".

    If you simply remove the 'my' from the second assignment to $x, you'll have a single $x variable, and the program will do what you're expecting.

    Update: Added the sentence in parenthesis.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: My my
by monkey_boy (Priest) on Apr 13, 2015 at 10:15 UTC
    use warnings; "my" variable $x masks earlier declaration in same scope at ./monks.pl + line 7. main::printme() called too early to check prototype at ./monks.pl line + 6. main::printme() called too early to check prototype at ./monks.pl line + 8. Use of uninitialized value $x in concatenation (.) or string at ./monk +s.pl line 11.


    This is not a Signature...
Re: My my
by Athanasius (Archbishop) on Apr 13, 2015 at 13:05 UTC

    Hello Saxium, and welcome to the Monastery!

    The following quote from perlsub may throw a little more light on the answer given by roboticus:

    A my has both a compile-time and a run-time effect. At compile time, the compiler takes notice of it. The principal usefulness of this is to quiet use strict 'vars', but it is also essential for generation of closures as detailed in perlref. Actual initialization is delayed until run time, though, so it gets executed at the appropriate time, such as each time through a loop, for example.

    So the rationale behind the behaviour described by roboticusThe printme subroutine uses the ... [$x] in scope at the point where it's compiled. — is to facilitate closures as a feature of Perl. Here are some references on closures, in case you’re interested:

    Excellent question, by the way!

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: My my
by Laurent_R (Canon) on Apr 13, 2015 at 17:55 UTC
    Yeah, there is really no reason to use a prototype (the empty parentheses in the subroutine definition) here.

    And also it would be better to pass your variable as an argument to the subroutine, rather than using is as a global variable, for example with something like this:

    #!/usr/bin/perl use strict; use warnings; my $x = 'a'; printme($x); $x = 'b'; printme ($x); sub printme { print "x: ", shift, "\n"; }
    which duly prints:
    x: a x: b

    Je suis Charlie.
Re: My my
by AnomalousMonk (Archbishop) on Apr 13, 2015 at 16:02 UTC
Re: My my
by Saxium (Initiate) on Apr 23, 2015 at 09:42 UTC
    Thank-you to all that posted answers!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found