Perl's lack of 'in' keyword
Iain Barnett
iainspeed at gmail.com
Thu Oct 9 13:30:58 BST 2008
On 9 Oct 2008, at 12:40 pm, Jonathan Stowe wrote:
> 2008/10/9 Iain Barnett <iainspeed at gmail.com>:
>>
>> On 9 Oct 2008, at 11:51 am, Andy Armstrong wrote:
>>
>>> On 9 Oct 2008, at 01:39, Iain Barnett wrote:
>>>>
>>>> if $a ~~ $b #this could mean several things
>>>
>>> A smart match between two scalars. Clear.
>>>
>>>> if $a in $b #it is clear what this means
>>>
>>> One scalar is in another? WTF?
>>
>> What does smart match do if you pass it an array/hash/object ref?
>> If it
>> matches against the scalar part then it's not a very smart match,
>> IMO.
>>
>
> Right, it was however unclear from your example what the contents of
> the two variables were.
>
> It will of course dereference a hash or array ref if appropriate. I'm
> not quite sure what you are proposing it should do when presented with
> a blessed reference.
Well, my point was that "in" describes to the reader exactly what the
writer wanted to do whereas using smart match can only be determined
by the computer at run time or by me with a lot of scrolling and
scanning (and probably headscratching). It's ambiguousity versus
clarity :-)
It also (quite nicely) shows how outdated the need for $ or @ is,
since every type can be held in a scalar (and I quite frequently end
up changing everything to references anyway) - why not just kill off
the funny characters? Unless of course, you think that @b tells the
reader more about the program, which then supports the need for an
"in" as well as ~~.
Personally, I'd prefer to check the type against either it's
signature (roll on Perl6) or where it was defined (or even better, in
my IDE with little or no effort on my part, but that's another no no
in the perl world)
I've no idea what smart match should do when confronted with an
object - check against each value in it's hash? It's the smart one,
not me! :_)
Iain
More information about the london.pm
mailing list