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 :-)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.