develooper Front page | perl.perl5.porters | Postings from January 2008

Re: ~~ changing behavior after using == ?

Thread Previous | Thread Next
From:
brian d foy
Date:
January 4, 2008 21:55
Subject:
Re: ~~ changing behavior after using == ?
Message ID:
040120082355516043%brian.d.foy@gmail.com
In article
<d8a74af10712290118m4441d9dar4a207e1572c6d82c@mail.gmail.com>, Gabor
Szabo <szabgab@gmail.com> wrote:

> use strict;
> use warnings;
> 
> use 5.010;
> 
> my $x = "42x";
> say $x ~~ 42 ? "T" : "F";
> say $x == 42 ? "T" : "F";
> say $x ~~ 42 ? "T" : "F";
> say $x == 42 ? "T" : "F";
> say $x;

Is there any ruling on this? Perhaps something that can go into the
docs (around "Smart matching in detail" in perlsyn)? If the docs say
that the smart match operator has side effects, it's easy to explain
(although confusing to remember perhaps). 

I'm adding smart matching to _Learning Perl, 5th Edition_ and want to
be able to explain why the first one doesn't match but the first say()
line doesn't print anything, but the third one, which is the same code,
doesn't because the (Num ~~ Numish String) has a higher precedence
than (Any ~~ String).

   #!/usr/local/bin/perl5.10.0
   
   use 5.010;
   
   my $m = 123;     # number
   my $n = '123';   # numish string
   my $o = '123.0'; # numish string
   
   say "1. Match for n and o" if $n ~~ $o;
   say "2. Match for n and m" if $n ~~ $m;
   say "1. Match for n and o" if $n ~~ $o;

Are we going to just say that if the smart match operator tries a
numeric comparison on one of it's operands (which we might not
know ahead of time based on the code), it might choose differently for
the smart match later?

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About