> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of Lloyd Sartor
> Sent: Wednesday, August 03, 2005 10:19 AM
> 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()]);
> } 


When I play with the "outer_sub" return a little I got the following
results (from ActiveState build 638 on a MS Windows XP system):

#   return [ _inner_sub() ];
#     calls _inner_sub in a LIST context and returns a ref to an
anonymous array
#     created from the returned list.
# 
#   return _inner_sub();
#   return ( _inner_sub() );
#     calls _inner_sub in a SCALAR context and so returns the length of
the list
#     returned.
#
#   return \_inner_sub();
#   return \( _inner_sub() );
#     calls _inner_sub in a LIST context and returns a reference to the
last
#     element of the list.

> 
> sub _inner_sub { 
>    @inner_ret_ary = (1,3,5,7,9);
>    return( @inner_ret_ary ); 
> } 


If "_inner_sub" is part of code that you "own" or control, it might be a
thought to consider using wantarry() to make it context-sensitive. Have
it return a reference to the array if called in a scalar context and the
contents of the array as a list if called in a list context. For
example:

sub _inner_sub { 
    # ... do whatever you do to build the array ...
    if ( wantarray() ) { return @inner_ret_ary;  }
    else               { return [EMAIL PROTECTED]; }
}

That might eliminate the need for the wrapper and may avoid the work and
memory usage for creating and populating yet another array value.

> 
> $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
> 

Gary L. Stump.,
Sr. Engineer,  IS/OTS

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

Reply via email to