Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: passing array references

by Prior Nacre V (Hermit)
on Dec 05, 2004 at 05:55 UTC ( #412462=note: print w/replies, xml ) Need Help??


in reply to passing array references

The following is an abstraction that shows a way to do what you want. It will handle "... a lot of values ..." and can be extended (e.g. you could add support for hashrefs at some later time).

[ ~/tmp ] $ cat return_multi_refs # return_multi_refs use strict; use warnings; use constant REF_SYM_MAP => { '' => ['$', '', ''], SCALAR => ['$', '${', '}'], ARRAY => ['@', '@{', '}'], }; my $rh_params = subroutine(); spawn_vars($rh_params); { no strict qw(vars); no warnings qw(once); print "scalar1 [$scalar1]\nscalar2 [$scalar2]\nscalar3 [$scalar3]\ +n", "array1[1] [$array1[1]]\narray2[2] [$array2[2]]\n"; } exit; sub subroutine { my @array1 = ( qw(a b c ) ); my $ra_array2 = [ qw(x y z) ]; my $scalar2 = 'Scalar the Second'; my $scalar3 = 'Scalar the Third'; my %params = ( scalar1 => 'Scalar the First', array1 => \@array1, scalar2 => \$scalar2, array2 => $ra_array2, scalar3 => $scalar3, ); return \%params; } sub spawn_vars { eval join '', map {( REF_SYM_MAP->{ref($_[0]->{$_})}[0], __PACKAGE__, '::', $_, + ' = ', REF_SYM_MAP->{ref($_[0]->{$_})}[1], '$_[0]->{', $_, '}', REF_SYM_MAP->{ref($_[0]->{$_})}[2], ';', )} keys(%{$_[0]}); die $@ if $@; }

Test output:

[ ~/tmp ] $ perl return_multi_refs scalar1 [Scalar the First] scalar2 [Scalar the Second] scalar3 [Scalar the Third] array1[1] [b] array2[2] [z] [ ~/tmp ] $

Regards,

PN5

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://412462]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-05-29 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?