in reply to Re: Python 'is' command
in thread Python 'is' command

... 'is' ... is overkill ...

I strongly agree! What's the point of bringing Scalar::Util::refaddr() et al to the party when  == != (and all the other numeric comparators, in the unlikely event they would be of any use) seem to manage just fine:

c:\@Work\Perl\monks>perl use strict; use warnings; use Test::Simple tests => 4; 1..4 sub is { $_[0] == $_[1] } my $x = 3; my $y = 3; my $x_ref = \$x; my $test_ref_1 = \$x; my $test_ref_2 = \$y; ok( is($test_ref_1, $x_ref), 'is Same' ); ok( !is($test_ref_2, $x_ref), 'is Different' ); ok( $test_ref_1 == $x_ref, '==' ); ok( $test_ref_2 != $x_ref, '!=' ); __END__ ok 1 - is Same ok 2 - is Different ok 3 - == ok 4 - !=


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Python 'is' command
by haukex (Archbishop) on Aug 14, 2019 at 19:33 UTC
    What's the point of bringing Scalar::Util::refaddr() et al to the party when == != (and all the other numeric comparators ... seem to manage just fine

    Because it's the only solution in the whole thread that still works even in the presence of operator overloading? ;-)

    use warnings; use strict; package Foo { my $x; use overload '<=>'=>sub{1}, 'cmp'=>sub{1}, '0+'=>sub{++$x}; } my $x = bless {}, 'Foo'; my $y = $x; print $x==$y ? "True\n" : "False\n"; # False use Scalar::Util qw/refaddr/; sub is { return refaddr $_[0] == refaddr $_[1] } print is($x,$y) ? "True\n" : "False\n"; # True

      Hmm... Hadn't thought about that.


      Give a man a fish:  <%-{-{-{-<

        Some of my first thoughts whenever it's about breaking some expected behavior are usually: prototypes, tied variables, and operator overloading :-)

Re^3: Python 'is' command
by BillKSmith (Monsignor) on Aug 14, 2019 at 20:22 UTC
    Your comment clarifies exactly what I meant. However, the documentation of the refaddr function in Scalar::Util assures us that our operatores are comparing addresses. The user documentation for references (perlref) makes no such claim.
    Bill
Re^3: Python 'is' command
by bliako (Abbot) on Aug 14, 2019 at 21:17 UTC

    you assume references. I don't know what python's is() does but I would expect any Perl is() to handle both is($x,$y) and is(\$x, \$y).