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
Steffen Mueller
January 26, 2011 04:54
Re: Using smart matching to find whether an array contains a string
Message ID:
On 01/26/2011 01:36 PM, Aristotle Pagaltzis wrote:
> * Ed Avis<>  [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)
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,

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About