Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What reason to use '-' sign in perl hash key name?

Tags:

hash

perl

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?

like image 940
Eugen Konkov Avatar asked Aug 29 '15 09:08

Eugen Konkov


2 Answers

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.

like image 135
Calle Dybedahl Avatar answered Oct 10 '22 03:10

Calle Dybedahl


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.

like image 21
Miguel Prz Avatar answered Oct 10 '22 02:10

Miguel Prz