in reply to Python 'is' command

My 'is' function is overkill for my test case, but it demonstrates the functionality you describe.
use strict; use warnings; use Test::Simple tests => 2; sub is { use Scalar::Util qw(refaddr); return (refaddr $_[0] == refaddr $_[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), 'Same' ); ok( !is($test_ref_2, $x_ref), 'Different' );
Bill

Replies are listed 'Best First'.
Re^2: Python 'is' command
by AnomalousMonk (Archbishop) on Aug 14, 2019 at 18:51 UTC
    ... '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:  <%-{-{-{-<

      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:  <%-{-{-{-<

      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

      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).