develooper Front page | perl.perl5.porters | Postings from September 2009

when ( foo() ) { ... }

Thread Next
From:
Yuval Kogman
Date:
September 23, 2009 15:53
Subject:
when ( foo() ) { ... }
Message ID:
a891e1bd0909231552j152d9237sfdbbe6e543772ccf@mail.gmail.com
op.c's looks_like_bool has a comment:

    /* Does this look like a boolean operation? For these purposes
       a boolean operation is:
         - a subroutine call [*]
...
       [*] possibly surprising
    */

That's quite an understatement. It guess makes sense if the subroutine
is invoking on the topic (and could make sense for subs with the
special $_ prototype where that's implicit), but other than that I
honestly can't think of a case where it makes more sense.

The downside is that it makes using subroutines that return objects
which overload ~~ impossible, which would have been very useful e.g.
with Moose's type constraints:

    use MooseX::Types qw(Str HashRef);

    given ( $thing ) {
        when ( HashRef ) { ... }
        when ( Str ) { ... }
        default { die "dunno what $_ is" }
    }

and similar constructs.

In this case the exports return the type constraint object whose ~~
would be overloaded to apply the type constraint check to the
argument.

Is this prioritization of boolean returning subroutines really
intentional? I don't see why someone would topicalize if they are
going to ignore the value, so i'm really confused...

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About