What version of Perl are you using?
The
sort { no warnings 'numeric'; $a <=> $b; }
trick works for your particular data under Perl version 5.30.3, but
does not work under version 5.8.9.
Win8 Strawberry 5.30.3.1 (64) Wed 12/02/2020 16:16:13
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings -l
my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi
+nk');
my @sorted = sort { no warnings 'numeric'; $a <=> $b } @list;
print for @sorted;
^Z
1,cat
001,elk
2,dog
11,eel
13,mink
22,mouse
Win8 Strawberry 5.8.9.5 (32) Wed 12/02/2020 16:18:25
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings -l
my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi
+nk');
# my @sorted = sort { no warnings 'numeric'; $a <=> $b } @list;
my @sorted = sort { no warnings; $a <=> $b; } @list;
"no" not allowed in expression at - line 3, at end of line
BEGIN not safe after errors--compilation aborted at - line 3.
I haven't done the work to figure out the version number at which
no starts to work in a sort subroutine block.
Interestingly (?), no is supported in a sub
subroutine block.
Win8 Strawberry 5.8.9.5 (32) Wed 12/02/2020 16:19:55
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings -l
my @list = ('1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,min
+k');
my @sorted = sort numeric_ascending @list;
print for @sorted;
sub numeric_ascending { no warnings 'numeric'; $a <=> $b; }
^Z
1,cat
001,elk
2,dog
11,eel
13,mink
22,mouse
Update: ... and also works in a do { ... }; block:
Win8 Strawberry 5.8.9.5 (32) Wed 12/02/2020 18:17:07
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings -l
my $sum = do {
'98xyzzy' + '763foo';
};
print $sum;
$sum = do {
no warnings 'numeric';
'123abc' + '45defg';
};
print $sum;
^Z
Argument "763foo" isn't numeric in addition (+) at - line 2.
Argument "98xyzzy" isn't numeric in addition (+) at - line 2.
861
168
(Same results under version 5.30.3.)
Give a man a fish: <%-{-{-{-<
|