develooper Front page | perl.perl5.porters | Postings from August 2012

Re: fixing smartmatch just hard enough (and when, too)

Thread Previous | Thread Next
From:
David Golden
Date:
August 17, 2012 05:51
Subject:
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
CAOeq1c9Ez9KTNAjduyZb+zWpFJaTFxC6dGrQcsZmo0Yy7y=Pag@mail.gmail.com
On Fri, Aug 17, 2012 at 8:20 AM, Ed Avis <eda@waniasset.com> wrote:
>
> Good point.  I was assuming that there is a concept of scalar equality defined
> by the language and accessible to the programmer directly.  But this is not the

Perl gives you numeric and string equality (and allows overriding of
each).  Consider this:

    use v5.10;
    use strict;
    use warnings;
    use bignum;
    use Scalar::Util qw/refaddr/;

    my $x = 42;
    my $y = 42;

    say '$x is object at ' . refaddr($x);
    say '$y is object at ' . refaddr($y);

    # output:
    # $x is object at 40813776
    # $y is object at 40814952

If you tested C<< $x in ( $y ) >>, is that true or false?  $x and $y
are different objects, yet the compare true numerically.  Is $x in the
list or not?

Thus your conception of "in" is visually elegant -- and I like that --
but just isn't specific enough for the dynamic nature of Perl, whereas
junctions force the programmer to clarify what comparison they
actually want for a membership test.

    C<< $x == any( $y ) >>
    C<< refaddr($x) == any( map { refaddr $_ } $y ) >>

-- David

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About