Bjørge Solli wrote:
>
> On Tuesday 18 July 2006 16:45, John W. Krahn wrote:
>
>>Leonid Grinberg wrote:
>>
>>>sub function
>>>{
>>> my %operating_systems = &populate_hash(%operating_systems);
>>
>>  my %operating_systems = populate_hash(%operating_systems);
>
>
> Why do you want to remove the ampersand? In /the Llama book/, including the
> ampersand is what they teach you. I find code a lot easier to understand at
> once if the ampersands are included, even though I know they don't always
> need to be.

Hi Bjørge

In this instance the ampersand makes no difference, but it is unnecessary and
better removed. When it /does/ make a difference, the ampersand does two
potentially dangerous things. First of all it disables parameter type checking
for templated subroutines, which means not just that you can pass the wrong sort
of data, but even if you pass the right sort of data it can be passed in the
wrong way. Secondly, if used without brackets, the subroutine is called with the
current @_ as parameters, leaving it to wreak untold damage on the calling
code's values.

To avoid these pitfalls, it is better to use the plain subroutine name when you
are calling it, but add the ampersand when you're talking about the subroutine
itself rather than its return value, like

  if (defined &populate_hash) { };

or

  my $subref = \&populate_hash;

I am surprised that the Llama book encourages coding in this way as the
ampersand has been undesirable for a long time. Do you have the latest edition -
the third?

Rob


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to