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

Re: Using smart matching to find whether an array contains a string

Thread Previous | Thread Next
From:
Steffen Mueller
Date:
January 26, 2011 04:54
Subject:
Re: Using smart matching to find whether an array contains a string
Message ID:
4D401988.4030604@cpan.org
On 01/26/2011 01:36 PM, Aristotle Pagaltzis wrote:
> * Ed Avis<eda@waniasset.com>  [2011-01-26 13:15]:
>> Thanks. In my original code the array is of strings so I could
>> use this. But when I dropped into the command line to test
>> behaviour with some one-liners, I reflexively picked (1, 2, 3)
>> as my example array.
>>
>> This is a slightly unperlish distinction; usually in Perl it
>> doesn't matter whether a scalar holds the number 42 or the
>> string '42' - you can use either of them in arithmetic or
>> string operations with the same semantics and no warnings. This
>> is a contrast to languages like Python where you have a visible
>> difference between numeric and string objects. Smart matching
>> might be the first everyday case where the difference between
>> numbers and strings becomes obvious to the ordinary Perl
>> programmer.
>
> Yes. I consider it to be exposing implementation details of Perl.
>
> The very idea of smart matching fundamentally clashes with Perl 5
> on this point, since it is supposed to look at the type of its
> argument to figure out what kind of comparison operator to use to
> establish boolean truth. Scalars are typeless in intent, though
> not entirely in practice, and smart matching is left to figure
> out a way to pick between two sets of operators. It’s kind of
> insane, since any operation can affect the apparent type of
> a scalar as a side effect. So ultimately the only sane way to use
> smart matching is to use some literal value on the right-hand
> side, never any program input.

I agree fully. I have had to try to codify such distinctions in the past 
when I implemented the method resolution for SOOT. I had to have to 
choose between

methodname(int foo)
and
methodname(float foo)

and worse.

It's fragile. That is to say if it's a general language feature that 
requires making this distinction, it is broken!

Best regards,
Steffen

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