in reply to Re^2: Subroutine evaluated as boolean (update: hash-slice)
in thread Subroutine evaluated as boolean

In fact, return @$_{'VALUE','ADDRESS'} has the same problem as my original code. But return @{[@$_{'VALUE','ADDRESS'}]} works great, if it is a little confusing to a beginner like me.

Replies are listed 'Best First'.
Re^4: Subroutine evaluated as boolean (central exit point)
by LanX (Saint) on Oct 13, 2014 at 01:14 UTC
    > return @$_{'VALUE','ADDRESS'} has the same problem as my original code

    yeah sorry, I just tested¹ ...

    ...it's a bit counterintuitive because of the leading @ ... :-/

    > @{[@$_{'VALUE','ADDRESS'}]} works great,

    better don't combine both techniques, readability is more important!

    you could easily do something like

    my @ret=(); for ( @$table ) { next unless $_->{VALUE} eq $wanted; @ret = @$_{VALUE, ADDRESS} } return @ret;

    To get a central exit point.

    update

    > if it is a little confusing to a beginner like me.

    the way context is propagated into subs is always confusing, that's why this was changed in Perl6's design!

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    ¹)

    DB<116> $_={a=>1,b=>2,c=>3,d=>0} => { a => 1, b => 2, c => 3, d => 0 } DB<117> sub tst { @$_{a,d} } DB<118> tst() => (1, 0) DB<119> scalar tst() => 0