in reply to What about if (my $var = foo()) { ... }

if (my $result = some_func()) { # do something with $result }

I use this construct all the time, and am not aware of any problems with it. The conditional is testing the return value of the assignment, which is true if some_func() returns something true. If it's false, then $result gets whatever false value was returned and the block does not execute (or the compiler may optimize away the assignment completely -- I don't know how that works.)

Off hand, I can't think of any reason why this would not also be allowed in Perl 6.

Replies are listed 'Best First'.
Re^2: What about if (my $var = foo()) { ... }
by Limbic~Region (Chancellor) on Dec 29, 2007 at 16:03 UTC
    friedo,
    Off hand, I can't think of any reason why this would not also be allowed in Perl 6.

    In perl 5, we write:

    for my $name (keys %people) { # do something with $name }

    The $name lexical is implicitly scoped to the for block. This is not the case in perl 6. The way perl 6 solves this is by introducing parameters to the block so you achieve the same thing in a cleaner way (from a design perspective).

    Cheers - L~R

      Eh? Going away? What's the corresponding Perl 6 construct and how/why is it considered to be "cleaner"?
        esper,
        See Re: What about if (my $var = foo()) { ... } for the Perl 6 construct.

        Regarding an explanation as to why formal parameters to a block are cleaner, it is similar to subs with and without signatures. If that still doesn't make any sense, I will try again when I am well rested.

        Cheers - L~R