smart matching question

D

david

Here showed is a piece from debugger
there are three parts

1)
DB<10> x 'a' ~~('a','b')
0 ''

2)
DB<11> x 'a' ~~['a','b']
0 1

3)
DB<12> @a = ('a','b')

DB<13> x 'a' ~~ @a
0 1

My question is: what is the difference between the first and the third
case.
I think i miss a bit the point what is the difference betwwen @a and
().

Can someone help me to understand ?

Thanks,
David
 
A

A. Sinan Unur

Here showed is a piece from debugger
there are three parts

1)
DB<10> x 'a' ~~('a','b')
0 ''

Note that you should get a warning for useless use of constant in void
context here.

What is happening is simple. ('a', 'b') on the RHS is not a list. It is
'a' *comma* 'b'. Therefore, the value of ('a', 'b') is 'b'. 'b' does not
match 'a'. The result is false.

See http://japhy.perlmonk.org/articles/pm/2000-02.html#1.3

Also:

C:\DOCUME~1\asu1\LOCALS~1\Temp> perl -MO=Deparse t.pl
Useless use of a constant in void context at t.pl line 8.
use warnings;
use strict 'refs';
BEGIN {
$^H{'feature_say'} = q(1);
$^H{'feature_state'} = q(1);
$^H{'feature_switch'} = q(1);
}
say 'yes' if ('???', 'b') ~~ 'a';
t.pl syntax OK

Note the '???' where 'a' used to be. That indicates that the constant
'a' has been optimized away.
3)
DB<12> @a = ('a','b')

DB<13> x 'a' ~~ @a
0 1

My question is: what is the difference between the first and the third
case.
I think i miss a bit the point what is the difference betwwen @a and
().

If you look at perldoc perlsyn ('Smart matching in detail'):

Array Any array contains string grep $_ eq $b, @$a

I don't know where this is explicitly stated, but the arguments to ~~
are evaluated in scalar context. Arrays, hashes, code blocks etc are
converted to references.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top