in reply to Reference to a function

I would be very wary of that article.

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.

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
    In computer science, there are two methods of passing arguments to a subroutine:

    Three, counting pass by name.

    See "Programming Language Pragmatics"
    Language features that most designers now believe were mistakes, at least in part because of implementation difficulties:
    * by-name parameters in Algol 60: Section 8.3.1 (Call By Name)

    The argument gets evaluated each time it is used in the subroutine.

      There's also call-by-need, which is basically just call-by-name with cached lazy evaluation. Basically, turn all your parameters into thunks (closure of no arguments) so they don't get evaluated when the method is invoked, and then evaluate them once and only once if they are referenced in the method.

      This post made for full-on nitpicking compliance :).
      Allen