Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Variable scope in ternary ?: operator not propagating?

by JayBonci (Curate)
on Mar 11, 2003 at 20:54 UTC ( #242155=perlquestion: print w/replies, xml ) Need Help??

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

I understand what's wrong here, I think, but I don't really know /why/. Simply:
my $foo= (my $bar = localtime)?("$bar hello"):("FAILED!");
Assuming you had some kind of code that you were checking for existance before formatting on one line, you get this:
Global symbol "$bar" requires explicit package name at ./ line + (whatever)
Why doesn't $bar propagate down to it's child conditions? This is bewildering because this works as expected:
print do { if(my $bar = localtime){ "$bar hello"}else{ "FAILED!" } };
I realize that the above construct probably isn't the internal representation of the ternary operator, but it is the logical expansion. If anyone has any additional info on what the perl internal structure on this is, I'd be happy to find out. Thanks


Replies are listed 'Best First'.
Re: Variable scope in ternary ?: operator not propagating?
by Abigail-II (Bishop) on Mar 11, 2003 at 21:03 UTC
    Because a variable only gets into scope *AFTER* the statement it's defined. That allows one to write:
    my $var = $var; # Start with the $var from the enclosing scope. local $_ = $_;
    So, in your example $bar comes into existance in the statement after the ?:.


Re: Variable scope in ternary ?: operator not propagating?
by Zaxo (Archbishop) on Mar 11, 2003 at 21:03 UTC

    A my declaration does not take effect until the end of a statement - a semicolon or end of block (which won't do you much good, since the var will go out of scope immediately). You'll need to split that into two statements:

    my $bar = localtime; my $foo= ($bar)?("$bar hello"):("FAILED!");

    After Compline,

Re: Variable scope in ternary ?: operator not propagating?
by TimToady (Parson) on Mar 12, 2003 at 16:18 UTC
    In Perl 6 it will work as you expect, because the rule has been changed to introduce lexicals as soon as the name is seen, rather than waiting for the end of the current statement.

      Then how do you achieve the current effect of

      my $x=42; print "\$x before block: $x\n"; { my $x=$x; $x++; print "\$x inside block: $x\n"; } print "\$x after block: $x\n";
      ? (Should give 42, 43, 42.)

      (Update: added missing $x++;, thanks, BrowserUk.) Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

        I suspect it will be by way of one of the new pseudopackages - as an educated guess, since all blocks are really closures in Perl 6, I'd say CALLER::. So my $x = $CALLER::x; will probably do that.

        Or I may be entirely wrong. :-)

        Makeshifts last the longest.

Re: Variable scope in ternary ?: operator not propagating? (do)
by tye (Sage) on Mar 11, 2003 at 23:40 UTC

    If you never use $bar again, then I might do:

    my $foo= do { my $bar= localtime; $bar ? "$bar hello" : "FAILED!"; };

                    - tye
Re: Variable scope in ternary ?: operator not propagating?
by clairudjinn (Beadle) on Mar 11, 2003 at 23:00 UTC
    $foo = localtime() . " hello" || "FAILED!";

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2023-02-04 03:15 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (30 votes). Check out past polls.