ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> activeperl
activeperl
Re: Sub returning a scalar and an array ref?
by Andy Bach other posts by this author
Aug 2 2007 1:39PM messages near this date
Sub returning a scalar and an array ref? | Re: Sub returning a scalar and an array ref?
>  Is it possible for a subroutine to return both a scalar (an integer, 
specifically) *and* a reference to an array of arrays? 
sub add_row { 
my ($counter, @ary) = @_; 

   # do stuff to @ary... 

   # add scalar to @ary: 
   push( @ary, ++$counter ); 

   return( \@ary ); 
} 

# sample call: 
@added_stuff = @{ add_row( $out_count, @added_stuff) }; 
$out_count = pop( @added_stuff ); 

yeah, you can return a list in list context. 
sub add_row { 
my ($counter, @ary) = @_; 
   # do stuff to @ary... 
   ++$counter; 

   return( \@ary, $counter); 
} 

my ($add_stuff_ref, $out_count) = add_row($out_count, @added_stuff);

A little counter-intuitive, the call and return orderings here. So this 
does smack of some need for refactoring here.   You could pass these by 
ref, not value, so that would avoid some of this sort of back and forth.
sub add_row { 
my ($counter_ref, $ary_ref) = @_; 
   # do stuff to @{ $ary_ref }... 
   ++$counter_ref; 
     return;
}

add_row( \$out_count, \@added_stuff);

That's ugly action at a distance so use the return value as some kind of 
success marker:
sub add_row { 
   my ($ary_ref) = @_; 
      my $do_stuff_worked = 0;
   # do stuff to @{ $ary_ref }... 
   return $do_stuff_worked; 
}

$out_count++ if add_row(\@added_stuff);

unless you need $out_count in the "do stuff" code.  But it's a start.

a

Andy Bach
Systems Mangler
Internet: andy_bach@[...].gov
VOICE: (608) 261-5738  FAX 264-5932

"Men are always wicked at bottom unless they are made good by some 
compulsion." 
Niccolo Macchiavelli
_______________________________________________
ActivePerl mailing list
ActivePerl@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Deane Rothenmaier
Andy Bach
Todd Beverly

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved