in reply to Re: Subroutine evaluated as boolean
in thread Subroutine evaluated as boolean

> So the problem is that it's evaluating ADDRESS instead of the list itself?

Yes that's the effect if the scalar comma operator.

Just copy the list to a temporary @array to be returned.

Or return a literal construct like @{[L,I,S,T]}

update

Or best use a hash slice

 return @$_{VALUE,NAME,ADDRESS} if ...

Cheers Rolf

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

Replies are listed 'Best First'.
Re^3: Subroutine evaluated as boolean
by Anonymous Monk on Oct 13, 2014 at 01:07 UTC
    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.
      > 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