Binding to a sub's return value

2005-05-10 Thread Joshua Gatcomb
I am wondering what the proper behavior of binding to a sub's return
value should be

sub some_routine {
my $foo = 42;
return $foo;
}
my $rv := some_routine();

Should $rv be bound to $foo or to a copy of $foo?  I ask because with
state() and closures, it makes a difference since the value can
change.

sub some_rourtine {
state $foo = 42;
return $foo++;
}

My apologies if this has been previously discussed or is documented
somewhere.  I am still playing catch up.  Ok, ok - it's true - I am
looking for a little instant gratification.

Cheers,
Joshua Gatcomb
a.k.a. L~R


Re: Binding to a sub's return value

2005-05-10 Thread Juerd
Joshua Gatcomb skribis 2005-05-10 15:52 (-0400):
 sub some_routine {
 my $foo = 42;
 return $foo;
 }
 my $rv := some_routine();
 Should $rv be bound to $foo or to a copy of $foo?  I ask because with
 state() and closures, it makes a difference since the value can
 change.

:= is the thing that implements subroutine arguments. Ask yourself the
same question with:

sub another_routine ($rv) {
...
}
another_routine(some_routine());

I'd expect $rv to be an alias to a copy of $foo's value, 42.

UNLESS some_routine is lvalue (which in this case it is not), in which
case, I'd expect $rv to be an alias to $foo itself

 sub some_rourtine {
 state $foo = 42;
 return $foo++;
 }

rvalue some_rourtine: copy 42

lvalue some_rourtine: error, ++ doesn't return an lvalue
(although prefix ++ probably could).


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html


Re: Binding to a sub's return value

2005-05-10 Thread Brent 'Dax' Royal-Gordon
Juerd [EMAIL PROTECTED] wrote:
 := is the thing that implements subroutine arguments. Ask yourself the
 same question with:

 sub another_routine ($rv) {
 ...
 }
 another_routine(some_routine());

 I'd expect $rv to be an alias to a copy of $foo's value, 42.

Really?  Because the default parameter binding is constant reference,
last I checked.

I actually like that answer.  It means that you can bind the return
value, but you can't mutate it, unless the function 'is rw'.  (And
perhaps you could mark it as 'is copy' and 'is ref', too...)

--
Brent 'Dax' Royal-Gordon [EMAIL PROTECTED]
Perl and Parrot hacker