From: jl_post on
> In article <a4526931-df5f-4acd-9b48-66e5143f9...(a)i31g2000yqm.googlegroups..com>,
> jl_p...(a)hotmail.com <jl_p...(a)hotmail.com> wrote:
>
> >   use Scalar::Util qw(looks_like_number);
> >   if ( @a ~~ sub { looks_like_number($_[0]) } )  # prints "false"
> >   {
> >      print "true";
> >   }
> >   else
> >   {
> >      print "false"
> >   }
>
> >the smart matching operator will return a false value.  In this case,
> >the smart matching operator behaves like an "all()" function,

On Jul 23, 3:55 pm, pac...(a)kosh.dhis.org (Alan Curry) replied:
>
> No it doesn't.
>
> Try it with @a=(1,2,3); it's still false.
>
> The coderef is only called once, with \@a as the argument. You passed an
> arrayref to looks_like_number, which is going to be false no matter what.


Hmmm... contrary to what you're saying, I tried it with @a=(1,2,3);
and it's evaluating to true for me. Here is the sample script I used
to test it with:


#!/usr/bin/perl
use strict;
use warnings;
my @a = (1, 2, 3);
use Scalar::Util qw(looks_like_number);
if ( @a ~~ sub { looks_like_number($_[0]) } ) # prints "true"
{
print "true";
}
else
{
print "false"
}
__END__


In fact, if I change the sub { } to be:

sub { print "$_[0]\n" }

then I see:

1
2
3
true

(The "true" is there because print() is returning a true value.) So I
have to disagree with the statement that the coderef is only called
once, with \@a as its argument.


> You're right about one thing at least: it's not easy to predict what ~~ will
> do based on the documentation.

Yeah... evidently I'm not the only one who's confused about aspects
of the '~~' operator. That's too bad -- it seems like it has a lot of
potential.

Cheers,

-- Jean-Luc
From: Alan Curry on
In article <22ba29dc-83fd-4619-b250-c1297f4d2308(a)m18g2000vbg.googlegroups.com>,
jl_post(a)hotmail.com <jl_post(a)hotmail.com> wrote:
>
> Hmmm... contrary to what you're saying, I tried it with @a=(1,2,3);
>and it's evaluating to true for me. Here is the sample script I used
>to test it with:
>

I'll run it exactly as presented...

>
>#!/usr/bin/perl
>use strict;
>use warnings;
>my @a = (1, 2, 3);
>use Scalar::Util qw(looks_like_number);
>if ( @a ~~ sub { looks_like_number($_[0]) } ) # prints "true"
>{
> print "true";
>}
>else
>{
> print "false"
>}
>__END__

....and the answer is "false". Meanwhile, the altered version:

>
>
> In fact, if I change the sub { } to be:
>
> sub { print "$_[0]\n" }
>
>then I see:
>
>1
>2
>3
>true

gives me "ARRAY(0x10030008)" and "true".

>
>(The "true" is there because print() is returning a true value.) So I
>have to disagree with the statement that the coderef is only called
>once, with \@a as its argument.

It must be version-dependent. My perl is the one currentl found in the last
stable Debian release:

This is perl, v5.10.0 built for powerpc-linux-gnu-thread-multi

--
Alan Curry
From: Ben Morrow on

Quoth pacman(a)kosh.dhis.org (Alan Curry):
[smartmatch]
>
> It must be version-dependent. My perl is the one currentl found in the last
> stable Debian release:
>
> This is perl, v5.10.0 built for powerpc-linux-gnu-thread-multi

Smartmatch semantics changed in several small but important ways between
5.10.0 and 5.10.1. I would highly recommend avoiding smartmatch on
5.10.0 for this reason. (I believe the intention is not to change them
again.)

Ben

From: Klaus on
On 24 juil, 08:38, Ilya Zakharevich <nospam-ab...(a)ilyaz.org> wrote:
> On 2010-07-23, jl_p...(a)hotmail.com <jl_p...(a)hotmail.com> wrote:
>
> >    I find this a bit counter-intuitive.
>
> There is nothing "a bit counter-intuitive" about the smart matching
> operation.  It is just *absolutely useless*, since there is no
> humanly-possible way to predict what it would do.

I perfectly agree.

Another feature in smart matching that is counter-intuitive/useless
where there is no humanly possible way to predict what it would do is:

the rule that if the lefthand side of a smartmatch is a number and the
righthand side is a string that *looks like a number*, then that
string is treated like a number.

First of all, it is impossible in Perl 5 (due to dualvars) to see
whether or not a variable contains a number or not.

Secondly, the rule whether or not a string looks like a number is not
straight forward:

**********************************************
use strict;
use warnings;
use 5.010;

say " 1.) 0 ~~ 0 ==> does ".( 0 ~~ 0 ? '' : 'not ')."look
like number";
say " 2.) 0 ~~ '' ==> does ".( 0 ~~ '' ? '' : 'not ')."look
like number";
say " 3.) 0 ~~ ' ' ==> does ".( 0 ~~ ' ' ? '' : 'not ')."look
like number";
say " 4.) 0 ~~ '+' ==> does ".( 0 ~~ '+' ? '' : 'not ')."look
like number";
say " 5.) 0 ~~ '-' ==> does ".( 0 ~~ '-' ? '' : 'not ')."look
like number";
say " 6.) 0 ~~ '.' ==> does ".( 0 ~~ '.' ? '' : 'not ')."look
like number";
say " 7.) 0 ~~ '0.' ==> does ".( 0 ~~ '0.' ? '' : 'not ')."look
like number";
say " 8.) 0 ~~ '0.0' ==> does ".( 0 ~~ '0.0' ? '' : 'not ')."look
like number";
say " 9.) 0 ~~ 0.0 ==> does ".( 0 ~~ 0.0 ? '' : 'not ')."look
like number";
say " 10.) 0 ~~ '0+' ==> does ".( 0 ~~ '0+' ? '' : 'not ')."look
like number";
say " 11.) 0 ~~ '+0' ==> does ".( 0 ~~ '+0' ? '' : 'not ')."look
like number";
say " 12.) 0 ~~ '0-' ==> does ".( 0 ~~ '0-' ? '' : 'not ')."look
like number";
say " 13.) 0 ~~ '-0' ==> does ".( 0 ~~ '-0' ? '' : 'not ')."look
like number";
say " 14.) 0 ~~ '0E' ==> does ".( 0 ~~ '0E' ? '' : 'not ')."look
like number";
say " 15.) 0 ~~ 'E0' ==> does ".( 0 ~~ 'E0' ? '' : 'not ')."look
like number";
say " 16.) 0 ~~ '0E0' ==> does ".( 0 ~~ '0E0' ? '' : 'not ')."look
like number";
say " 17.) 0 ~~ ' 0E0' ==> does ".( 0 ~~ ' 0E0' ? '' : 'not ')."look
like number";
say " 18.) 0 ~~ '0E0 ' ==> does ".( 0 ~~ '0E0 ' ? '' : 'not ')."look
like number";
say " 19.) 0 ~~ '0 ' ==> does ".( 0 ~~ '0 ' ? '' : 'not ')."look
like number";
say " 20.) 0 ~~ ' 0' ==> does ".( 0 ~~ ' 0' ? '' : 'not ')."look
like number";
say " 21.) 0 ~~ ' 0 ' ==> does ".( 0 ~~ ' 0 ' ? '' : 'not ')."look
like number";
say " 22.) 1 ~~ 1 ==> does ".( 1 ~~ 1 ? '' : 'not ')."look
like number";
say " 23.) 1 ~~ '' ==> does ".( 1 ~~ '' ? '' : 'not ')."look
like number";
say " 24.) 1 ~~ ' ' ==> does ".( 1 ~~ ' ' ? '' : 'not ')."look
like number";
say " 25.) 1 ~~ '+' ==> does ".( 1 ~~ '+' ? '' : 'not ')."look
like number";
say " 26.) 1 ~~ '-' ==> does ".( 1 ~~ '-' ? '' : 'not ')."look
like number";
say " 27.) 1 ~~ '.' ==> does ".( 1 ~~ '.' ? '' : 'not ')."look
like number";
say " 28.) 1 ~~ '1.' ==> does ".( 1 ~~ '1.' ? '' : 'not ')."look
like number";
say " 29.) 1 ~~ '1.0' ==> does ".( 1 ~~ '1.0' ? '' : 'not ')."look
like number";
say " 30.) 1 ~~ 1.0 ==> does ".( 1 ~~ 1.0 ? '' : 'not ')."look
like number";
say " 31.) 1 ~~ '1+' ==> does ".( 1 ~~ '1+' ? '' : 'not ')."look
like number";
say " 32.) 1 ~~ '+1' ==> does ".( 1 ~~ '+1' ? '' : 'not ')."look
like number";
say " 33.) 1 ~~ '1-' ==> does ".( 1 ~~ '1-' ? '' : 'not ')."look
like number";
say " 34.) -1 ~~ '-1' ==> does ".(-1 ~~ '-1' ? '' : 'not ')."look
like number";
say " 35.) 1 ~~ '1E' ==> does ".( 1 ~~ '1E' ? '' : 'not ')."look
like number";
say " 36.) 1 ~~ 'E0' ==> does ".( 1 ~~ 'E0' ? '' : 'not ')."look
like number";
say " 37.) 1 ~~ '1E0' ==> does ".( 1 ~~ '1E0' ? '' : 'not ')."look
like number";
say " 38.) 1 ~~ ' 1E0' ==> does ".( 1 ~~ ' 1E0' ? '' : 'not ')."look
like number";
say " 39.) 1 ~~ '1E0 ' ==> does ".( 1 ~~ '1E0 ' ? '' : 'not ')."look
like number";
say " 40.) 1 ~~ '1 ' ==> does ".( 1 ~~ '1 ' ? '' : 'not ')."look
like number";
say " 41.) 1 ~~ ' 1' ==> does ".( 1 ~~ ' 1' ? '' : 'not ')."look
like number";
say " 42.) 1 ~~ ' 1 ' ==> does ".( 1 ~~ ' 1 ' ? '' : 'not ')."look
like number";
**********************************************

the result is:
**********************************************
1.) 0 ~~ 0 ==> does look like number
2.) 0 ~~ '' ==> does not look like number
3.) 0 ~~ ' ' ==> does not look like number
4.) 0 ~~ '+' ==> does not look like number
5.) 0 ~~ '-' ==> does not look like number
6.) 0 ~~ '.' ==> does not look like number
7.) 0 ~~ '0.' ==> does look like number
8.) 0 ~~ '0.0' ==> does look like number
9.) 0 ~~ 0.0 ==> does look like number
10.) 0 ~~ '0+' ==> does not look like number
11.) 0 ~~ '+0' ==> does look like number
12.) 0 ~~ '0-' ==> does not look like number
13.) 0 ~~ '-0' ==> does look like number
14.) 0 ~~ '0E' ==> does not look like number
15.) 0 ~~ 'E0' ==> does not look like number
16.) 0 ~~ '0E0' ==> does look like number
17.) 0 ~~ ' 0E0' ==> does look like number
18.) 0 ~~ '0E0 ' ==> does look like number
19.) 0 ~~ '0 ' ==> does look like number
20.) 0 ~~ ' 0' ==> does look like number
21.) 0 ~~ ' 0 ' ==> does look like number
22.) 1 ~~ 1 ==> does look like number
23.) 1 ~~ '' ==> does not look like number
24.) 1 ~~ ' ' ==> does not look like number
25.) 1 ~~ '+' ==> does not look like number
26.) 1 ~~ '-' ==> does not look like number
27.) 1 ~~ '.' ==> does not look like number
28.) 1 ~~ '1.' ==> does look like number
29.) 1 ~~ '1.0' ==> does look like number
30.) 1 ~~ 1.0 ==> does look like number
31.) 1 ~~ '1+' ==> does not look like number
32.) 1 ~~ '+1' ==> does look like number
33.) 1 ~~ '1-' ==> does not look like number
34.) -1 ~~ '-1' ==> does look like number
35.) 1 ~~ '1E' ==> does not look like number
36.) 1 ~~ 'E0' ==> does not look like number
37.) 1 ~~ '1E0' ==> does look like number
38.) 1 ~~ ' 1E0' ==> does look like number
39.) 1 ~~ '1E0 ' ==> does look like number
40.) 1 ~~ '1 ' ==> does look like number
41.) 1 ~~ ' 1' ==> does look like number
42.) 1 ~~ ' 1 ' ==> does look like number
**********************************************

--
Klaus
From: Peter J. Holzer on
On 2010-07-28 07:03, Klaus <klaus03(a)gmail.com> wrote:
> On 24 juil, 08:38, Ilya Zakharevich <nospam-ab...(a)ilyaz.org> wrote:
>> On 2010-07-23, jl_p...(a)hotmail.com <jl_p...(a)hotmail.com> wrote:
>>
>> > � �I find this a bit counter-intuitive.
>>
>> There is nothing "a bit counter-intuitive" about the smart matching
>> operation. �It is just *absolutely useless*, since there is no
>> humanly-possible way to predict what it would do.
>
> I perfectly agree.

I haven't tried to use it in anger yet (as mentioned in another posting,
I have still too many machines running 5.8.x), but from reading the docs
I tend to agree: It's way too complicated and I don't think I can
remember that stuff. It is of course possible that those rules exactly
match my intuition, but somehow I doubt it.


> Another feature in smart matching that is counter-intuitive/useless
> where there is no humanly possible way to predict what it would do is:
>
> the rule that if the lefthand side of a smartmatch is a number and the
> righthand side is a string that *looks like a number*, then that
> string is treated like a number.
>
> First of all, it is impossible in Perl 5 (due to dualvars) to see
> whether or not a variable contains a number or not.

Right.


> Secondly, the rule whether or not a string looks like a number is not
> straight forward:

What's not straightforward about that? Except maybe for leading and
trailing whitespace the results are what I expected.

hp