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

yet another way to force scalar context:
use string concatenation,
print "".( () = /;/g ); #prints "3"
unary + doesn't do the job as tybalt89 shows.

why not just?:

print tr/;//;

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:16 UTC

    0+ would make more sense than "". here.

      fair enough, both work.

      As an update, string concatenation can be used in other situations to force scalar context so it is valid to mention that possibility. In this particular case, 0+ is perfect. However if the goal is only to count ";", tr is even better.

        > fair enough, both work.

        Still different, your result is a string.

        Perl works hard to treat all scalar sub-types equally by operators, but I seem to remember edge cases where the internal type mattered and resulted in errors.

        No need to stringify a number.

        And a reader - at least me - would be confused.

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