In some perl modules I see code:
if (exists $h->{Location}) {
%args = (%args, -Status => 302);
}
Why author uses -Status
instead of just Status
?
Why I hack my own modules, when I must use '-' sign in hash key names?
UPD
Reading answers it seems they are must be same, but they are not
my $a = { -status => 'a', status => 3, };
print $a->{ status }, $a->{ -status };
print pp $a;
OUTPUT
3a{ -status => "a", status => 3 }
Summary of my perl5 (revision 5 version 20 subversion 2) configuration:
Does this mean that old module is broken now?
It's a historical artifact. One of the first major modules to use the hash-key argument convention for its functions was Perl/Tk, and it brought over the initial-dash naming convention on the keys from TCL/Tk (which it was ported from). It spread and was quite popular for a while. Back before we had the "fat comma" (=>
) operator it helped somewhat in telling keys names and their arguments apart. Today there is really no reason to use it, apart from personal taste.
It's a convention. Similar to the options that a program can take from the command line, for example 'ls -l'. Many Perl programmers use it to be more clear when passing parameters to a subroutine. That way your programs are more readable and easy to maintain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With