Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Not sure how to handle this scope

by Anonymous Monk
on Aug 19, 2021 at 13:52 UTC ( [id://11135963]=note: print w/replies, xml ) Need Help??


in reply to Not sure how to handle this scope

Braces always create a new scope, and my always creates a lexical variable, which is undef by default -- or at least, should be. In practice the behavior of my $x = 'foo' if $bar is undefined if the condition is false, and is documented as such.

Since the scope of a lexical variable begins at the statement after the statement that defines it, the ternary operator does what you want: my $self = $ARGV[0] eq 'Fido' ? OO->new( $dog ) : $self;. The $self in the assignment statement refers to the variable outside the braces, since the variable inside the braces is not yet in-scope.

Replies are listed 'Best First'.
Re^2: Not sure how to handle this scope
by The Perlman (Scribe) on Aug 19, 2021 at 21:43 UTC
    "Braces always create a new scope"

    Unless they create a $hash ={a=>1} or are used for enclosing ${identifiers} or are used to dereference a $hash->{a} ... or...?

    - Ron

      Quite right. In trying to address the OP's statement that ... it wont create a new scope of $self in the braces ... I overstated the case. Thank you for the correction.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found