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.