/usr/share/perl5/match/smart.pm is in libmatch-simple-perl 0.010-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | package match::smart;
use 5.006001;
use strict;
use warnings;
use B qw();
use Exporter::Tiny;
use List::Util 1.33 qw(any all);
use Scalar::Util qw(blessed looks_like_number refaddr);
BEGIN {
$match::smart::AUTHORITY = 'cpan:TOBYINK';
$match::smart::VERSION = '0.010';
}
our @ISA = qw( Exporter::Tiny );
our @EXPORT = qw( M );
our @EXPORT_OK = qw( match );
sub match
{
no warnings qw(uninitialized numeric);
my ($a, $b, $seen) = @_;
return(!defined $a) if !defined($b);
return !!$b->check($a) if blessed($b) && $b->isa("Type::Tiny");
return !!$b->MATCH($a, 1) if blessed($b) && $b->can("MATCH");
return eval 'no warnings; !!($a~~$b)' if blessed($b) && $] >= 5.010 && do { require overload; overload::Method($b, "~~") };
if (blessed($b) and not $b->isa("Regexp"))
{
require Carp;
Carp::croak("Smart matching a non-overloaded object breaks encapsulation");
}
$seen ||= {};
my $refb = refaddr($b);
return refaddr($a)==$refb if $refb && $seen->{$refb}++;
if (ref($b) eq q(ARRAY))
{
if (ref($a) eq q(ARRAY))
{
return !!0 unless @$a == @$b;
for my $i (0 .. $#$a)
{
return !!0 unless match($a->[$i], $b->[$i], $seen);
}
return !!1;
}
return any { exists $a->{$_} } @$b if ref($a) eq q(HASH);
return any { $_ =~ $a } @$b if ref($a) eq q(Regexp);
return any { !defined($_) } @$b if !defined($a);
return any { match($a, $_) } @$b;
}
if (ref($b) eq q(HASH))
{
return match([sort map "$_", keys %$a], [sort map "$_", keys %$b])
if ref($a) eq q(HASH);
return any { exists $b->{$_} } @$a if ref($a) eq q(ARRAY);
return any { $_ =~ $a } keys %$b if ref($a) eq q(Regexp);
return !!0 if !defined($a);
return exists $b->{$a};
}
if (ref($b) eq q(CODE))
{
return all { !!$b->($_) } @$a if ref($a) eq q(ARRAY);
return all { !!$b->($_) } keys %$a if ref($a) eq q(HASH);
return $b->($a);
}
if (ref($b) eq q(Regexp))
{
return any { $_ =~ $b } @$a if ref($a) eq q(ARRAY);
return any { $_ =~ $b } keys %$a if ref($a) eq q(HASH);
return $a =~ $b;
}
return !!$a->check($b) if blessed($a) && $a->isa("Type::Tiny");
return !!$a->MATCH($b) if blessed($a) && $a->can("MATCH");
return eval 'no warnings; !!($a~~$b)' if blessed($a) && $] >= 5.010 && do { require overload; overload::Method($a, "~~") };
return !defined($b) if !defined($a);
return $a == $b if _is_number($b);
return $a == $b if _is_number($a) && looks_like_number($b);
return $a eq $b;
}
sub _is_number
{
my $value = shift;
return if ref $value;
my $flags = B::svref_2object(\$value)->FLAGS;
$flags & ( B::SVp_IOK | B::SVp_NOK ) and !( $flags & B::SVp_POK );
}
sub _generate_M
{
require Sub::Infix;
&Sub::Infix::infix(\&match);
}
1;
__END__
=pod
=encoding utf-8
=for stopwords smartmatch recurses
=head1 NAME
match::smart - clone of smartmatch operator
=head1 SYNOPSIS
use v5.10;
use match::smart;
if ($this |M| $that)
{
say "$this matches $that";
}
=head1 DESCRIPTION
match::smart provides a match operator C<< |M| >> that acts like more or
less identically to the (as of Perl 5.18) deprecated smart match operator.
If you don't like the crazy L<Sub::Infix> operator, you can alternatively
export a more normal function:
use v5.10;
use match::smart qw(match);
if (match($this, $that))
{
say "$this matches $that";
}
=head2 Differences with ~~
There were major changes to smart match between 5.10.0 and 5.10.1. This
module attempts to emulate the behaviour of the operator in more recent
versions of Perl. In particular, 5.18.0 (minus the warnings). Divergences
not noted below should be considered bugs.
While the real smart match operator implicitly takes references to operands
that are hashes or arrays, match::smart's operator does not.
@foo ~~ %bar # means: \@foo ~~ \%bar
@foo |M| %bar # means: scalar(@foo) |M| scalar(%bar)
If you want the C<< \@foo ~~ \%bar >> behaviour, you need to add the
backslashes yourself:
\@foo |M| \%bar
Similarly:
"foo" ~~ /foo/ # works
"foo" |M| /foo/ # no worky!
"foo" |M| qr/foo/ # do this instead
match::smart treats the C<MATCH> method on blessed objects (if it exists)
like an overloaded C<< ~~ >>. This is for compatibility with L<match::simple>,
and for compatibility with pre-5.10 Perls that don't allow overloading
C<< ~~ >>.
=begin trustme
=item M
=item match
=end trustme
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=match-simple>.
=head1 SEE ALSO
L<match::simple>.
This module uses L<Exporter::Tiny>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014, 2017 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|