This post is prompted by a sub-thread of
Preferred technique for named subroutine parameters?, specifically
Re^2: Preferred technique for named subroutine parameters? and replies thereto, in which a question is raised
about warnings and errors generated by the technique of
named subroutine parameters recommended in
TheDamian's
Perl Best Practices.
At the suggestion of
LanX, I am making this a new thread.
This question pertains to the hard copy of the first English
edition, printed July 2005, chapter 9, pages
182 - 183, the "Named Arguments" section. I have checked the on-line
errata lists and there are no corrections or alterations for these
pages.
The discussion of "named arguments" contrasts building an
anonymous hash of name/parameter pairs in the argument list
of a function call with directly assigning the argument list
(i.e., @_) to a hash within the body of the function.
With respect to building an anonymous hash of named arguments,
page 183, second paragraph, states that errors
"... will be reported (usually at compile time)
in the caller's context ..." (emphasis added),
but I don't see that this is so.
(However, everything else works as advertised: the anonymous hash
version of named arguments produces a warning at the point of
the function invocation.)
Certainly, a malformed function call like
func({ one => 'uno', two => 'dos', three => });
from the example code below does compile and only warns at run time.
Can anyone give an example of a compile time error (or
warning) associated with this invocation technique, or any
explanation or example of what TheDamian was referring to?
Various malformed examples, code and output:
use warnings;
use strict;
print "program running \n";
# these compile and run WITHOUT any warnings
na_anon ({ one => 'uno', two => 'dos', => });
na_anon ({ one => 'uno', two => 'dos', , });
na_anon ({ one => 'uno', => two => 'dos' });
na_anon ({ one => 'uno', , two => 'dos' });
na_anon ({ one => 'uno', => => two => 'dos' });
na_anon ({ one => 'uno', , , two => 'dos' });
na_assign( one => 'ein', two => 'zwei', => );
na_assign( one => 'ein', two => 'zwei', , );
na_assign( one => 'ein', => two => 'zwei' );
na_assign( one => 'ein', , two => 'zwei' );
na_assign( one => 'ein', => => two => 'zwei' );
na_assign( one => 'ein', , , two => 'zwei' );
# these compile and run WITH run-time warnings
na_anon ({ one => 'uno', two => 'dos', three => });
na_anon ({ one => 'uno', two => 'dos', three => 20..21 });
na_assign( one => 'ein', two => 'zwei', three => );
na_assign( one => 'ein', two => 'zwei', three => 20..21 );
print "program done \n";
sub na_anon {
my %args = %{ $_[0] };
my $n = 'one';
print "anon hash: $n translates to $args{$n} \n";
}
sub na_assign {
my %args = @_;
my $n = 'two';
print "hash assign: $n translates to $args{$n} \n";
}
Output:
>perl np_ah_vs_ha_1.pl
program running
anon hash: one translates to uno
anon hash: one translates to uno
anon hash: one translates to uno
anon hash: one translates to uno
anon hash: one translates to uno
anon hash: one translates to uno
hash assign: two translates to zwei
hash assign: two translates to zwei
hash assign: two translates to zwei
hash assign: two translates to zwei
hash assign: two translates to zwei
hash assign: two translates to zwei
Odd number of elements in anonymous hash at np_ah_vs_ha_1.pl line 71.
anon hash: one translates to uno
Odd number of elements in anonymous hash at np_ah_vs_ha_1.pl line 72.
anon hash: one translates to uno
Odd number of elements in hash assignment at np_ah_vs_ha_1.pl line 85.
hash assign: two translates to zwei
Odd number of elements in hash assignment at np_ah_vs_ha_1.pl line 85.
hash assign: two translates to zwei
program done
Update: Slight clarification in wording of code comments.