2009/12/9 John W. Krahn <jwkr...@shaw.ca>:
> Jeff Pang wrote:
>>
>> Noah:
>>>
>>> sub exiting {
>>>    my ($hostname, %login) = @_;
>>
>> Passing arguments like this has no such problem.
>> But you'd better pass the hash as a reference to the subroutine.
>>
>> exitint($hostname, \%login);
>>
>> sub exiting {
>>    my $hostname = shift;
>>    my %login = %{+shift};
>
> What is the point of passing a reference if you are just going to copy the
> whole hash anyway (which is what the OP was doing)?

One possible reason is that if you later want to add a new, optional
parameter to the subroutine, you can do this without modifying the
existing interface (and therefore modifying all code which calls
exiting):

sub exiting {
   my $hostname = shift;
   my %login = %{+shift};
   my $optional = shift // $default_value; # use || before 5.10 and be careful
   # ...
}

This isn't possible if you are passing the hash as a list:

sub exiting {
  my ($hostname, %login) = @_;
  my $optional = ??? # whoops, we've used all arguments already
}

I'd therefore argue that passing a hashref is more maintainable.

Phil

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to