On Tuesday 18 July 2006 18:05, Rob Dixon wrote:
> 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.

> 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.

OK.

> 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;

Ah! I see. I didn't even know this was possible. Guess I'll have to pick up 
the alpaca book soon.

> 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?

I have the fourth. It explains that the ampersand is not needed, but as far as 
I remember it doesn't say that it /should/ be omitted (but hey, I only read 
it once). But after the initial explanation of subroutines it really don't 
use subroutines that much, all the examples are as simple as they can be - 
without subroutines. So I guess this was beyond the scope of the book. I have 
Steven Holzers /Perl Black Book/ as well, and there the ampersands are 
omitted, but I didn't read that book from start till end, it works better as 
a problem solver. It had a lot more on this topic as well:-)

Thanks, then I learned something new this morning as well:-)

-- 
Bjørge Solli - Office:+47 55205847
Mohn-Sverdrupsenteret, Nansensenteret, Høyteknologisenteret T47
Thormöhlensgate 47, 5006 Bergen, Norway - www.nersc.no
Google Earth: www.nersc.no/GE - TOPAZ: topaz.nersc.no

--
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