Perl-Sensitive Sunglasses PerlMonks

### Re^2: ~~ and list literal

 on Oct 25, 2012 at 23:46 UTC ( #1000967=note: print w/replies, xml ) Need Help??

in reply to Re: ~~ and list literal
in thread ~~ and list literal

Follow-up question:

Why does "x" ~~ "a", "b", "c" always give 1? Running through Deparse gives: 'x' ~~ 'a', '???', 'c'. What is going on here?

And why does "x" ~~ sub { "a", "b", "c" } also give 1? Smart match is giving me a headache...

Replies are listed 'Best First'.
Re^3: ~~ and list literal
by ikegami (Pope) on Oct 27, 2012 at 00:15 UTC

Why does "x" ~~ "a", "b", "c" always give 1?

It doesn't; it evaluates to "c".

"," is a very low precedence operator. "x" ~~ "a", "b", "c" means ("x" ~~ "a"), "b", "c", which evaluates to "c" in scalar context.

And why does "x" ~~ sub { "a", "b", "c" } also give 1?

It doesn't; it evaluates to "c".

"x" ~~ sub { "a", "b", "c" } is the same as scalar( sub { "a", "b", "c" }->(\$x) ). Since  "a", "b", "c" evaluates to "c" in scalar context, that is what is returned.

Re^3: ~~ and list literal
by Anonymous Monk on Oct 26, 2012 at 03:16 UTC

...

Because of precedence/binding

Consider

```\$ perl -le " \$f = 1,2,3 ; print \$f"
1

\$ perl -MO=Deparse -le " \$f = 1,2,3 ; print \$f"
BEGIN { \$/ = "\n"; \$\ = "\n"; }
\$f = 1, '???', '???';
print \$f;
-e syntax OK

[download]```

Compare it to

```\$ perl -le " \$f = ( 1,2,3 ); print \$f"
3

\$ perl -MO=Deparse -le " \$f = ( 1,2,3 ) ; print \$f"
BEGIN { \$/ = "\n"; \$\ = "\n"; }
\$f = ('???', '???', 3);
print \$f;
-e syntax OK
[download]```

Since smart is binary (like = in the above example), it works on two scalars, left-scalar operator right-scalar

If you have no parentheses, the first scalar is taken, the closest, because assignment operator (=) has higher precedence than comma opeartor (it binds tighter).

If you add parenthesis, then you have the fabled List in Scalar Context, then the comma operator return the last right item (rightest most)

Since you're confused about precedence you're probably also confused about context, so see Tutorials: Context in Perl: Context tutorial, "List" Is a Four-Letter Word, Easy Reference for Contexts

Smart match is giving me a headache...

Stop using it :) worked for me :) Smart-match

Re^3: ~~ and list literal
by mbethke (Hermit) on Oct 26, 2012 at 01:56 UTC

With the ~~ operator having much higher precedence than the comma, this is parsed as a comma-list of a smart-match whose result doesn't matter, more stuff that doesn't matter at all, and "c" which is true.

The second one would pass "x" to the sub which again evaluates a comma-list and throws away everything (including the argument) but "c" which gets returned and is true.

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000967]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-06-24 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (560 votes). Check out past polls.