Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi all, I find I am confusing of return a vector(hash & array): return their reference(return \@array) or return their variable(return @array) looks no different, but return reference seems I need write more code...what does actually perl do when dealing with vector type parameter and return value? Is reference work better than variable?

Replies are listed 'Best First'.
Re: Best way to return array or hash?
by swampyankee (Parson) on Feb 29, 2008 at 14:00 UTC

    I've not found returning references to involve significantly more coding (but then, I find hashes of arrays and hashes of hashes to be Perl's most useful data structures). As moritz said, references do eliminate the need for copying, which could be wasteful of time and space. Unless I knew an array was going to be small (less than a dozen or so elements), I'd probably return a reference. Since I come from the Fortran world (where everything is passed by reference), I don't find the "pass-by-reference" issue to be a real one.


    emc

    Information about American English usage here and here.

    Floating point issues? Read this before posting: http://docs.sun.com/source/806-3568/ncg_goldberg.html

Re: Best way to return array or hash?
by locked_user sundialsvc4 (Abbot) on Feb 29, 2008 at 17:34 UTC

    perldoc perlsub has a lot to say on this matter. Basically, all of the arguments to a function are passed-in as a list of scalars (any of which may be references), and what is returned is likewise ... a completely-flattened list of scalars.

    From perldoc perlsub:   “If you return one or more aggregates (arrays and hashes), these will be flattened together into one large indistinguishable list.”

    Later in that same document, under the heading “Pass by Reference”:   If you want to pass more than one array or hash into a function--or return them from it--and have them maintain their integrity, then you're going to have to use an explicit pass-by-reference. Before you do that, you need to understand references as detailed in perldoc perlref. This section may not make much sense to you otherwise... ”

    Note:   this documentation-page can be found at http://perldoc.perl.org/perlsub.html#Pass-by-Reference, and of course, all the other perldoc pages are to be found there also.

Re: Best way to return array or hash?
by moritz (Cardinal) on Feb 29, 2008 at 13:11 UTC
    Returning an array directly copies it, so if your arrays are large or you're doing it often, use references instead.
Re: Best way to return array or hash?
by leocharre (Priest) on Feb 29, 2008 at 17:17 UTC
    As I understand it.. Subroutines do not return arrays or hashes.
    They can return one zero or more values, that at the receiving end can be sown together into an array/hash/whatever, making it seem like you returned an array or hash.

    Some example code (untested.)..

    my @array1 = ('Paula', 'Sherri', 'Cynthia', 'Ludovico'); my @gotback = names_a(); $gotback[0] = 'Jimmy'; # @array1 is still ('Paula', 'Sherri', 'Cynthia', 'Ludovico') my $gotback = name_aref(); $gotback->[0] = 'Jimmy'; # @array1 is now ('Jimmy', 'Sherri', 'Cynthia', 'Ludovico') my @gotback2 = @{ names_aref() }; # makes a copy $gotback2[0] = 'Back to Paula'; # @array1 is still ('Jimmy', 'Sherri', 'Cynthia', 'Ludovico') # because we made a copy by doing @{ names_aref() } sub names_a { # this really returns # 'Jimmy', 'Sherri', 'Cynthia','Ludovico' # and not @array1 # when received at the other end, it may be slapped # back togeter into an array, # but it will be a new list (a new array) return @array1; } sub names_aref { # this returns a reference, therefore both what this is # returning and the original @array1 both point to the # same space in the symbol table (place in memory). return \@array1; }
Re: Best way to return array or hash?
by uday_sagar (Scribe) on Aug 21, 2012 at 07:51 UTC

    By the way, How can i get an array from a perl code (say a.pl) by calling it from the other perl code (b.pl)

    a.pl is the called code and b.pl the calling one.

    Thanks