in reply to scope of my $x in if statements

Setting aside why you want to do this, how about:

#!/usr/bin/env perl use strict; sub f { return 2; } sub g { my ($y) = shift @_; return $y*2; } if ( my $k = g(my $x = f()) ) { print "If the answer is four I'm happy. Answer == $k\n"; }
That example seems to do all that you want.
update: Test platform was perl v5.8.6


Be Appropriate && Follow Your Curiosity

Replies are listed 'Best First'.
Re^2: scope of my $x in if statements
by ikegami (Patriarch) on May 07, 2009 at 20:49 UTC
    That's not equivalent. It can have a different result when f() returns something false

      Meaning if f() returns false and g(falsehood) returns true.
      Time to go work on my test case skills.


      Be Appropriate && Follow Your Curiosity

        That's only true if g() has no side-effects and no performance penalty.