Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Ambiguous '~~' in 'print scalar ~~list'

by rsFalse (Hermit)
on Mar 12, 2019 at 13:10 UTC ( #1231158=perlquestion: print w/replies, xml ) Need Help??

rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I was playing with '~~' "operator" (perlsecret#Inchworm), which is equivalent to 'scalar()' if parsed as two consecutive unary bit-inverting operators, not as experimental smartmatch op. And there is a case where parser can't understand what do I mean (unary forcing scalar or binary smartmatch):
#!/usr/bin/perl -wl use strict; @_ = qw( a b c ); open my $A, '>', "out.txt"; print $A ~~@_; print $A scalar @_;
STDOUT: empty
STDERR: Smartmatch is experimental at smartmatch_or_scalar.pl line 9.
> cat out.txt
3

In my opinion usual operators (bit-inversion) should be in higher precedence of parsing than experimental operators, but in my code '~~' was interpreted as experimental smartmatch.

Replies are listed 'Best First'.
Re: Ambiguous '~~' in 'print scalar ~~list'
by choroba (Archbishop) on Mar 12, 2019 at 13:54 UTC
    As usually, wrapping the filehandle into curly brackets helps:
    print {$A} ~~@_;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Using curly braces around the filehandle is generally good practice anyway. Makes it clearer that you're printing something to $A instead of printing the contents of $A.

Re: Ambiguous '~~' in 'print scalar ~~list'
by Athanasius (Bishop) on Mar 12, 2019 at 13:43 UTC

    Hello rsFalse,

    Yes, the parser is interpreting ~~ as the smartmatch operator here:

    23:30 >perl -MO=Deparse 1983_SoPW.pl Smartmatch is experimental at 1983_SoPW.pl line 9. BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict; @_ = ('a', 'b', 'c'); open my $A, '>', 'out.txt'; print $A ~~ \@_; print $A scalar @_; 1983_SoPW.pl syntax OK 23:30 >

    But if you really want to parse this construction as the so-called “inchworm operator,” just use parentheses — print $A (~~@_); — to disambiguate:

    23:32 >perl -MO=Deparse 1983_SoPW.pl BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict; @_ = ('a', 'b', 'c'); open my $A, '>', 'out.txt'; print $A ~~@_; print $A scalar @_; 1983_SoPW.pl syntax OK 23:32 >

    As to which interpretation should be given “higher precedence” by the parser: that really depends on the way the two constructs are expected to be used. And in fact the double tilde was never intended to be used as an operator at all. If the smartmatch experiment had been successful (it wasn’t), it would have intentionally introduced a new operator into Perl. So of course the parser is going to treat ~~ as a smartmatch operator wherever that makes syntactic sense — that’s what the experiment was all about.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231158]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2020-01-23 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?