sunshine_august has asked for the wisdom of the Perl Monks concerning the following question:

hi, all monks:

I notice that, $clientSocket->recv( $buffer, $length) can pass the data back to $buffer, even though the $buffer is value-passed, rather than ref-passed.

I write a test subroutine, if I want to pass the data back from a subroutine, I need to pass the variable by ref. like this:

my $buffer; foosub( \$bufffer ); print $buffer, "\n"; sub foosub { my $rs_buffer = shift; $$rs_buffer = "Hello, world"; }
if I pass it by value, it gets nothing back:
my $buffer; foosub( $buffer); print $buffer, "\n"; sub foosub { my $buffer = shift; $buffer = "Hello, world"; }

so, why the recv() can pass the data back by a value-passed?

Replies are listed 'Best First'.
Re: How $clientSocket->recv( $buffer, $length) pass the data back by a value-passed?
by GrandFather (Saint) on Sep 12, 2008 at 07:43 UTC

    Consider:

    my $value = 1; editValue ($value); print $value; sub editValue { $_[0]++; }

    prints '2' because @_ contains aliases to the parameters passed into the sub. If you edit the contents of elements of @_ you are editing the contents of the parameters.

    You can make it more explicit by taking a reference to an element:

    sub editValue { my $value = \$_[0]; $$value++; }

    Perl reduces RSI - it saves typing
Re: How $clientSocket->recv( $buffer, $length) pass the data back by a value-passed?
by ikegami (Patriarch) on Sep 12, 2008 at 07:50 UTC

    even though the $buffer is value-passed, rather than ref-passed.

    That's hard to believe since Perl always passes arguments by reference, never by value. You seem to be confusing "passing by reference" and "passing a reference". @_ contains aliases to the values passed to the function (rather than the a copy of the values), so modifying elements of @_ modifies the parameters in the caller.

Re: How $clientSocket->recv( $buffer, $length) pass the data back by a value-passed?
by sunshine_august (Scribe) on Sep 12, 2008 at 09:27 UTC
    thx, I got it:)
    And, I find two paragraph about this in Programming Perl(3rd):
    6.2. Semantics
    ....
    Any arguments passed to a Perl routine come in as the array @_. If you call a function with two arguments, they are accessible inside the function as the first two elements of that array: $_[0] and $_1. Since @_ is a just a regular array with an irregular name, you can do anything to it you'd normally do to an array.2 The array @_ is a local array, but its values are aliases to the actual scalar parameters. (This is known as pass-by-reference semantics.)
    6.2.1. Tricks with Parameter Lists
    Perl does not yet have named formal parameters, but in practice all you do is copy the values of @_ to a my list, which serves nicely for a list of formal parameters. (Not coincidentally, copying the values changes the pass-by-reference semantics into pass-by-value, which is how people usually expect parameters to work anyway, even if they don't know the fancy computer science terms for it.)