in reply to Bitten by the || bug

For hack value, here is an expressive version:

sub also_correct { @{ ( map { @$_ ? $_ : [ list1() ] } [ list0() ] )[0] } }

(For some reason @{} executes its expression in void scalar context, which necessitates the ()[0] gymnastics to force the correct context on map, which would otherwise return 1. That cost me some time to track down, because I was testing on the shell in one-liners with no strict. In retrospect that was stupid, considering I was testing code with references.)

This is the list-context equivalent of list0() || list1() and has the same short-circuiting behaviour as ||.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: Bitten by the || bug (@{})
by tye (Sage) on Feb 20, 2006 at 16:53 UTC

    s/void/scalar/, as should be expected since @{} wants a reference to an array, not a list of anything.

    - tye        

      So it is scalar context. (Thanks for the correction on wantarray.)

      By way of an explanation: I misremembered the issue I had in mind. What I was thinking of is that the scalar reference-taking operator \ evaluates its argument in list context. This came up in discussion of the (ab)use of @{[]} or ${\()} constructs to put code inside here-docs.

      Makeshifts last the longest.

      $ perl -le'print @{warn wantarray ? 1 : defined wantarray ? 0 : "undef +"}' undef at -e line 1.

      Nope, void context.

      Makeshifts last the longest.

        You've misused wantarray. It tells the context in which the current subroutine was called, not in what context wantarray was called.

        > perl sub context { warn !defined wantarray ? "void" : wantarray ? "list" : "scalar", $/; } @{context()}; <EOF> scalar

        - tye