in reply to u+ fails to force scalar context to empty list assignment: +( () = ... )

to prove tybaldt's point that Unary "+" has no effect whatsoever

#!/usr/bin/perl -w use warnings; use strict; # -------------------- # force context # -------------------- sub VOID (&) { print "VOID:\t"; $_[0]->(); return; } sub LIST (&) { print "LIST:\t"; () = $_[0]->(); return; } sub SCALAR (&) { print "SCALAR:\t"; scalar $_[0]->(); return; } # -------------------- # tests # -------------------- sub ctxt { unless (defined wantarray) { print "is void\n"; } elsif (wantarray) { print "is list\n"; } else { print "is scalar\n"; } } SCALAR { ctxt }; SCALAR { +(ctxt) }; # u+ no effect LIST { ctxt }; LIST { +(ctxt) }; # u+ no effect VOID { ctxt }; VOID { +(ctxt) };; # u+ no effect # BUT LIST { 0+(ctxt) }; # forcing scalar context

SCALAR: is scalar SCALAR: is scalar LIST: is list LIST: is list VOID: is void VOID: is void LIST: is scalar

(code stolen from older post)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: u+ fails to force scalar context to empty list assignment: +( () = ... )
by ikegami (Patriarch) on Mar 12, 2019 at 00:19 UTC

    Shorter alternative:

    >perl -MO=Concise,-exec -e"$a = +f()" 2>&1 | find "entersub" 5 <1> entersub[t3] sKS/TARG >perl -MO=Concise,-exec -e"@a = +f()" 2>&1 | find "entersub" 6 <1> entersub[t4] lKS/TARG >perl -MO=Concise,-exec -e"+f()" 2>&1 | find "entersub" 5 <1> entersub[t2] vKS/TARG

    The first letter of the "word" after the op name (entersub) is the context in which it's evaluated.

      Interesting!

      > Shorter alternative:

      Well ...

      the sub ctxt() is just using the well documented wantarray the rest is only cosmetics to provide readable output.

      If I wanted to golf, I could also do:

      >perl -E"sub f{say qw/s l v/[wantarray//'2']};+f;$a=+f;@a=+f" v s l

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        So your alternatives to $a = +f() are magical code and incomprehensible code?