[sf-perl] perlstyle
David Christensen
dpchrist at holgerdanske.com
Thu Jul 15 19:01:51 PDT 2021
> On Thu, Jul 15, 2021 at 1:48 PM David Christensen wrote:
>> 2. Under "Along the same lines, just because you CAN omit
>> parentheses ...", what is "num" in the example code?
>>
>> return print reverse sort num values %array;
On 7/15/21 2:33 PM, Miller Hall wrote:
> num in that context is a subroutine meant as a shortcut for numerical
> sorting.
>
> The more full example is the following
>
> #!/usr/bin/env perl
>
> use strict; use warnings;
>
> sub num { $a <=> $b; }
>
> my %array = ( a => 1, b => 2, c => 3 );
>
> sub foo { return print reverse sort num values %array }
>
> foo();
>
>
> You can read about this in the spec for sort
> https://perldoc.perl.org/functions/sort
>
> - Miller
Thank you for the reply. :-)
Looking at the signatures for Perl sort:
sort SUBNAME LIST
sort BLOCK LIST
sort LIST
The way the example is written, "num" would appear to be a Perl built-in
keyword or function, but it is not. This is confusing.
Perhaps perlstyle(1) should use the Perl sort SUBNAME placeholder:
return print reverse sort SUBNAME values %array;
Going further in perlstyle(1), the next line of example code adds
parentheses to the above:
return print(reverse(sort num (values(%array))));
A reader might ask "why is there no opening parenthesis between 'sort'
and 'num'?" and "why is there a space between 'num' and the following
opening parenthesis?".
I thought I understood, but the following console session convinced me
that I do not. Thankfully, I rarely use the 'sort SUBNAME LIST' form,
and must not have attempted parentheses if/when I did so.
Perhaps perlstyle(1) should eliminate the parentheses around LIST:
return print(reverse(sort SUBNAME values(%array)));
Or, provide more justification for parentheses, to make it clear that a
list is being passed:
return print(reverse(sort SUBNAME (values(%array), @more_stuff)));
Finally, %array is a hash. Perhaps perlstyle(1) should use %hash.
So, I would suggest that the parentheses omission code examples in
perlstyle(1) be changed to:
return print reverse sort SUBNAME values %hash;
return print(reverse(sort SUBNAME values(%hash)));
David
2021-07-15 18:59:30 dpchrist at dipsy ~/sandbox/perl
$ cat perlstyle-num-2.pl
#!/usr/bin/env perl
use strict;
use warnings;
$| = 1;
my %hash = ( a => 1, b => 2, c => 3 );
sub num { $a <=> $b }
my $num = 'num';
my $rc = \#
my @more_stuff = (4, 5);
print "\ncase 1: ";
print reverse sort num values %hash;
# case 1: 321
print "\ncase 2: ";
print reverse sort $num values %hash;
# case 2: 321
print "\ncase 3: ";
print reverse sort $rc values %hash;
# case 3: 321
#print "\ncase 4: ";
#return print reverse sort 'num' values %hash;
# syntax error at perlstyle-num-2.pl line 27, near "'num' values"
#print "\ncase 5: ";
#return print reverse sort \&num values %hash;
# syntax error at perlstyle-num-2.pl line 31, near "&num values"
print "\ncase 6: ";
print(reverse(sort num (values(%hash))));
# case 6: 321
print "\ncase 7: ";
print(reverse(sort(num (values(%hash)))));
# Unquoted string "num" may clash with future reserved word at
perlstyle-num-2.pl line 39.
# case 7: 321
print "\ncase 8: ";
print(reverse(sort num(values(%hash))));
# case 8: 321
print "\ncase 9: ";
print(reverse(sort(num(values(%hash)))));
# case 9: Use of uninitialized value $b in numeric comparison (<=>) at
perlstyle-num-2.pl line 10.
# Use of uninitialized value $a in numeric comparison (<=>) at
perlstyle-num-2.pl line 10.
# 0
print "\ncase 10: ";
print(reverse(sort num values(%hash)));
# case 10: 321
print "\ncase 11: ";
print(reverse(sort num (values(%hash), @more_stuff)));
# case 11: 54321
print "\n";
2021-07-15 18:59:31 dpchrist at dipsy ~/sandbox/perl
$ perl perlstyle-num-2.pl
Unquoted string "num" may clash with future reserved word at
perlstyle-num-2.pl line 41.
case 1: 321
case 2: 321
case 3: 321
case 6: 321
case 7: 321
case 8: 321
case 9: Use of uninitialized value $b in numeric comparison (<=>) at
perlstyle-num-2.pl line 10.
Use of uninitialized value $a in numeric comparison (<=>) at
perlstyle-num-2.pl line 10.
0
case 10: 321
case 11: 54321
More information about the SanFrancisco-pm
mailing list