in reply to Reference to a function
It's written by someone who thinks the language is called PERL, it's out of date, and it's just simply confused as far as I can see.
I'll let other monks comment but here's an excerpt pretty much at random.
In computer science, there are two methods of passing arguments to a subroutine:
When passing by value, the language makes a copy of the argument, and all access inside the subroutine is to that copy. Therefore, changes made inside the subroutine do not effect the calling routine. Such arguments cannot be used as output from the subroutine. The preferred method of outputting from a subroutine is via the return value. Unfortunately, the PERL language doesn't support it. Instead, the programmer must explicitly make the copy inside the subroutine.
- By value
- By reference
In general, I believe it's best to use arguments as input-only.
When passing by reference, the language makes the argument's exact variable available inside the subroutine, so any changes the subroutine makes to the argument affect the arguments value in the calling procedure (after the subroutine call, of course). This tends to reduce encapsulation, as there's no way of telling in the calling routine that the called routine changed it. Passing by reference harkens back to the days of global values, and in general creates less robust code.
All arguments in PERL are passed by reference! If the programmer wishes to make a copy of the argument to simulate passing by value (and I believe in most cases he should), he must explicitly make the copy in the subroutine and not otherwise access the original arguments.
And his notes when telling people how to deferences hashes say:
sub dohash { my(%hash) = ("president"=>"Clinton", "vice president" => "Gore", "intern" => "Lewinsky"); my($ref) = \%hash; # NOTE: Can't put %{ref} inside doublequotes!!! Doesn't work!!! # Prints "internLewinskyvice presidentGorepresidentClinton". # NOTE: Hash elements might print in any order! print %{$ref}; print "\n"; # NOTE: OK to put ${$ref}{} in doublequotes. # NOTE: Prints "Gore". print "${$ref}{'vice president'}\n"; }
($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Reference to a function
by bsb (Priest) on Jul 24, 2003 at 03:20 UTC | |
by oylee (Pilgrim) on Jul 25, 2003 at 21:54 UTC |