nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp ~~
left &
left | ^
left &&
left || //
nonassoc .. ...
right ?:
right = +
r greater than the right
argument.
X<cmp>
Binary "~~" does a smartmatch between its arguments. Smart matching
is described in the next section.
X<~~>
"lt", "le", "ge", "gt" and "cmp" use the collat
tmatch Operator
First available in Perl 5.10.1 (the 5.10.0 version behaved differently),
binary C<~~> does a "smartmatch" between its arguments. This is mostly
used implicitly in the C<when> constru
hin a C<foreach>
loop or a C<given> block, it executes the statement only if the smartmatch
C<< $_ ~~ I<EXPR> >> is true. If the statement executes, it is followed by
a C<next> from inside a C<foreac
XPR) BLOCK elsif (EXPR) BLOCK ...
unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
given (EXPR) BLOCK
LABEL while (EXPR) BLOCK
LABEL while (EXPR) BLOCK continue BLOCK
LABEL
to use the highly experimental C<given>, that could be
written like this:
use v5.10.1;
given ($var) {
when (/^abc/) { $abc = 1 }
when (/^def/) { $def = 1 }
when (/^xyz/) { $xyz = 1 }
def
es.
=head2 Smart match changes
=head3 Changes to type-based dispatch
The smart match operator C<~~> is no longer commutative. The behaviour of
a smart match now depends primarily on the type of its
t like the other code references (even if they
choose to ignore it).
=item *
C<%hash ~~ sub {}> and C<@array ~~ sub {}> now test that the subroutine
returns a true value for each key of the hash (or
are no longer
treated specially when appearing on the left of the C<~~> operator,
but like any vulgar scalar.
=item *
C<undef ~~ %hash> is always false (since C<undef> can't be a key in a
hash). No
in in
subsequent 5.10 releases.
=head3 Changes to type-based dispatch
The smart match operator C<~~> is no longer commutative. The behaviour of
a smart match now depends primarily on the type of its
t like the other code references (even if they
choose to ignore it).
=item *
C<%hash ~~ sub {}> and C<@array ~~ sub {}> now test that the subroutine
returns a true value for each key of the hash (or
are no longer
treated specially when appearing on the left of the C<~~> operator,
but like any vulgar scalar.
=item *
C<undef ~~ %hash> is always false (since C<undef> can't be a key in a
hash). No
# Test cases
$file ~~ m[Pod-Simple/t];
my ($in_pod, $has_encoding, @non_ascii);
FILE: while (my $line = <$fh>) {
chomp $line;
if ($line ~~ /^=[a-z]+/) {
if ($in_pod) {
if ($line ~~ /^=encoding (\S+)/) {
$has_encoding = 1;
last FILE;
} elsif ($line ~~ /[^[:ascii:]]/) {
my $enc
"$encoding->{Name}?" : 'unknown!'),
};
}
}
if ($line ~~ /^=cut/) {
$in_pod = 0;
}
}
if (@non_ascii and not $has_encoding) {
ed.
=head2 Smart match changes
=head3 Changes to type-based dispatch
The smart match operator C<~~> is no longer commutative. The behaviour of
a smart match now depends primarily on the type of its
t like the other code references (even if they
choose to ignore it).
=item *
C<%hash ~~ sub {}> and C<@array ~~ sub {}> now test that the subroutine
returns a true value for each key of the hash (or
are no longer
treated specially when appearing on the left of the C<~~> operator,
but like any vulgar scalar.
=item *
C<undef ~~ %hash> is always false (since C<undef> can't be a key in a
hash). No
item is
contained in an array or a hash:
use 5.010;
if( $item ~~ @array )
{
say "The array contains $item"
}
if( $item ~~ %hash )
{
say "The hash contains $item"
}
With earlier ver
answer
with the least amount of work:
use 5.010;
if( @array1 ~~ @array2 )
{
say "The arrays are the same";
}
if( %hash1 ~~ %hash2 ) # doesn't check values!
{
say "The hash keys are th
ormat, picture line>
X<@> X<^> X<< < >> X<< | >> X<< > >> X<#> X<0> X<.> X<...>
X<@*> X<^*> X<~> X<~~>
@ start of regular field
^ start of special field
< pad character for left jus
ble width field for next line of a multi-line value
~ suppress line with all fields empty
~~ repeat line until all fields are exhausted
Each field in a picture line starts with either "@"
$text = "line 1\nline 2\nline 3";
format STDOUT =
Text: ^*
$text
~~ ^*
$text
.
Output:
Text: line 1
line 2
line
| $Is_NetWare);
if ($p1 ne $p2) {
my($dev1, $ino1, $dev2, $ino2);
($dev1, $ino1) = stat($p1);
($dev2, $ino2) = stat($p2);
($dev1 ~~ $dev2 && $ino1 ~~ $ino2);
}
else {
1;
}
}
1;
#$a = 2; $a->[1] = undef;
$b = thaw freeze $a;
@a = map { ~~ exists $a->[$_] } 0 .. $#$a;
@b = map { ~~ exists $b->[$_] } 0 .. $#$b;
ok 20, "@a" eq "@b";
';
}
else
=> "-X",
dereferencing => '${} @{} %{} &{} *{}',
matching => '~~',
special => 'nomethod fallback =',
);
my %ops_seen;
for $category (keys %ops) {
s => '<>',
filetest => '-X',
dereferencing => '${} @{} %{} &{} *{}',
matching => '~~',
special => 'nomethod fallback ='
Most of the overloadable operators map one-to-one to these
oad was introduced in Perl 5.12.
=item * I<Matching>
The key C<"~~"> allows you to override the smart matching logic used by
the C<~~> operator and the switch construct (C<given>/C<when>). See
L<pe
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$typedesc
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$typedesc
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pty list if the block was exited by C<break>. Thus you
can now write:
my $type = do {
given ($num) {
break when undef;
"integer" when /^[+-]?[0-9]+$/;
"float" when /
e operators is used in
an unparenthesised argument:
< > <= >= lt gt le ge
== != <=> eq ne cmp ~~
&
| ^
&&
|| //
.. ...
?:
= += -= *= etc.
, =>
=head3 Smart-matching against array
resulted in a successful match:
my @a = qw(a y0 z);
my @b = qw(a x0 z);
@a[0 .. $#b] ~~ @b;
This odd behaviour has now been fixed [perl #77468].
=head3 Negation treats strings differen
=over
=item *
C<~~> now correctly handles the precedence of Any~~Object, and is not tricked
by an overloaded object on the left-hand side.
=item *
In Perl 5.14.0, C<$tainted ~~ @array> stopped wo
se { }
** unless (e) { } elsif (e) { } else { }
! ~ \ u+ u- given (e) { when (e) {} default {} }
=~ !~
* / % x NUMBERS vs STRINGS FALSE vs TR
== != eq ne
< > <= >= lt gt le ge < > <= >= lt gt le ge
== != <=> eq ne cmp ~~ <=> cmp
&
| ^ REGEX MODIFIERS REGEX METACHARS
&& /i c
[ 1, 2, 0 ], 0 ];
$e = '"abc" ~~ (%s)';
$subs{'~~'} = $e;
push @tests, [ "abc", $e, '(~~)', '(NM:~~)', [ 1, 1, 0 ], 0 ];
$subs{'-X'} = 'do { my $f = (%s);'
ve caught it
earlier.
=item Repeated format line will never terminate (~~ and @# incompatible)
(F) Your format contains the ~~ repeat-until-blank sequence and a
numeric field that will never go blan
.
=item Smart matching a non-overloaded object breaks encapsulation
(F) You should not use the C<~~> operator on an object that does not
overload it: Perl refuses to use the object's underlying stru
nc may be used together.
=item *
The smartmatch operator (C<~~>) was changed so that the right-hand side
takes precedence during C<Any ~~ Object> operations.
=item *
A bug has been fixed in the ta
concat_amg, /* 0x3f . */
concat_ass_amg, /* 0x40 .= */
smart_amg, /* 0x41 ~~ */
ftest_amg, /* 0x42 -X */
regexp_amg, /* 0x43 qr */
max_amg_code
emented on this Perl version"
# 52 implicit smartmatch in given/when
given ('foo') {
when ('bar') { continue; }
when ($_ ~~ 'quux') { continue; }
default { 0; }
}
####
# 53 conditions in e