[EMAIL PROTECTED] wrote on 08/03/2005 09:48:33 AM:

> 
> Thanks, Lloyd. It worked that way. My only question now is "is that 
> returning a "reference to an array" (RTOAA)? I have to ask because 
> 1. that's the spec I'm coding to and 2. I'm so hazy on references 
> that I don't have a clue as to whether the "anonymous list" you tell
> me I'm sending back is equivalent to an RTOAA. 
> 
> Thanks again, 
> 
> Deane 
> 
> 

An anonymous list can only be referenced via a list reference; i.e., a 
simple identifier (eg, @x) is not associated with an anonymous list. 
Therefore, by definition, a reference to an array is being returned from 
outer_sub(). This is also indicated by the output:

$x = ARRAY(0x1a4fea0)   <== $x is reference to array
@$x = 1 3 5 7 9         <== list dereferencing of $x provides the list 
(1,3,5,7,9)

An anonymous list exists "out there somewhere" and a program can only get 
to it through a reference. When the last reference to the list disappears 
(goes out of scope), then the anonymous list magically disappears, too.

> 
> Lloyd Sartor <[EMAIL PROTECTED]> 
> 08/03/2005 09:18 
> 
> 
>         To:        [EMAIL PROTECTED] 
>         cc:        [email protected] 
>         Subject:        Re: Return to Sender (w/apologies to Elvis)
> 
> 
> 
> 
> [EMAIL PROTECTED] wrote on 08/03/2005 08:16:53 AM:
> 
> > I have a subroutine which is supposed to return a reference to an 
> > array. All this subroutine does is call another sub (i.e.: it's a 
> > wrapper), which returns the array that the first sub then has to 
> > return the reference to. Isn't there a one-liner way of doing this? 
> > I was using: 
> > 
> > sub outer_sub { 
> >    @ret_ary = _inner_sub(); 
> >    return( [EMAIL PROTECTED] ); 
> > } 
> > 
> > where the returned value from _inner_sub is an array: 
> > 
> > sub _inner_sub { 
> >    # create @inner_ret_ary. . . 
> >    return( @inner_ret_ary ); 
> > } 
> > 
> > which worked fine. But, when I tried to streamline outer_sub like 
this: 
> > 
> > return( \_inner_sub()); # _inner_sub just returns @ 
> > 
> > with the same return value (an array)  from _inner_sub, it fails. 
> 
> The one-liner attempt is returning a reference to the subroutine. You 
can 
> create an anonymous list in outer_sub using the bracket syntax and 
return 
> its reference:
> 
> sub outer_sub { 
>   # @ret_ary = _inner_sub(); 
>   # return( [EMAIL PROTECTED] ); 
>   return([_inner_sub()]);
> } 
> 
> sub _inner_sub { 
>   @inner_ret_ary = (1,3,5,7,9);
>   return( @inner_ret_ary ); 
> } 
> 
> $x = outer_sub();
> print "\$x = $x\n";
> print "[EMAIL PROTECTED] = @$x\n\n";
> 
> ------------------------------------------------
> $x = ARRAY(0x1a4fea0)
> @$x = 1 3 5 7 9
> 
> 

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to