Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: my least favorite perl feature

by Aristotle (Chancellor)
on Feb 06, 2003 at 12:27 UTC ( #233118=note: print w/replies, xml ) Need Help??

in reply to my least favorite perl feature

It's pretty simple. You use eq for all cases, except for using == when you need a numeric comparison. That's pretty much it, for 99.99% of the time. How hard can that be?

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: my least favorite perl feature
by adrianh (Chancellor) on Feb 06, 2003 at 14:06 UTC

    Not hard obviously ;-) However, I do miss the ability to say succinctly whether two things are the same "object" or not - something like Common Lisp's eq, Pop-11's == or python's is.

    I admit Perl's eq works for this case 99% of the time, I don't like using it for identity checks because:

    • It's not explicit enough. I want to say "these two things are the same" not "these two things expressed as strings are the same"
    • Stringification has got to be more expensive than a direct comparison internally.
    • It can fail when you have overloading of eq or "". I don't like writing code that can fail, even if it is only a few edge cases :-)

    It's easy to write your own obviously - this does the job:

    #! /usr/bin/perl use strict; use warnings; use Scalar::Util qw(refaddr); sub same { ref($_[0]) && ref($_[1]) && refaddr($_[0]) == refaddr($_[1]) || refaddr(\$_[0]) == refaddr(\$_[1]); }; use Test::More 'no_plan'; our ($x, $y, $z) = (1,1); *z = *x; ok same($x, $x), 'x is x'; ok same(\$x, \$x), 'ref x is ref x'; ok !same($x, $y), 'x not y'; ok same($x, $z), 'x is z';

    But, in my opinion, something this basic should be in the base language... but I guess that's what perl6 is for :-)

        Overloading == is the problem with that I'm afraid :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://233118]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2021-02-24 21:18 GMT
Find Nodes?
    Voting Booth?

    No recent polls found