Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Subroutines and Passing data ...

by dtharby (Acolyte)
on Jun 13, 2005 at 13:17 UTC ( #466148=perlquestion: print w/replies, xml ) Need Help??

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

Hi Guys,
having a hard day ....... sorry for the stupid questions...
Anyway I would like to return some information from a subroutine to the main program .. example
sub test1 { ...some tests $message1 = "Hello Everyone"; $Indicator1 = 1; return $message1, $Indicator1; }
Can I send out more that one value ?
I have sent one out and tried to view the value but get nothing...
any ideas or help
Thanks Danny

p.s. I have been reading on subroutines but this doesn't seem very clear ! ;-)

Replies are listed 'Best First'.
Re: Subroutines and Passing data ...
by Limbic~Region (Chancellor) on Jun 13, 2005 at 13:22 UTC
    Returning data from a sub is much like passing data to the sub. It is simple as long as they are all scalars. If you have hashes and arrays then you likely need to pass or return by reference - here is an example:
    my ($scalar, $a_ref, $h_ref) = some_sub(42, \@foo, \%bar); sub some_sub { my $answer = shift; my ($foo, $bar) = @_; return ($answer, $foo, $bar); }
    Let us know if that doesn't clear things up.

    Cheers - L~R

      Cheers L~R,
      That has certainly cleared it up and I have it working !!
      thanks again
        Note that when you're returning a known number of scalars and 1 single array that you won't need a reference. ie the following works just fine:
        sub foo { return($scalar, $anotherscalar, @array) } my($s1, $s2, @a) = foo;
        The array always needs to go last though...

        Remember rule one...
Re: Subroutines and Passing data ...
by aukjan (Friar) on Jun 13, 2005 at 13:21 UTC
    You can return more values by passing them out in an array, so you were on the right track:
    sub test { .... return ($mes1,$mes2); } @array = test; -or- ($val1,$val2) = test;

    .:| If it can't be fixed .. Don't break it |:.

Re: Subroutines and Passing data ...
by mrborisguy (Hermit) on Jun 13, 2005 at 13:23 UTC

    There are a couple of different ways to do this.

    ## 1. As an array sub one { return ( $val1, $val2 ); } my @ar = one(); print $ar[0], $ar[1]; ## 2. As an arrayref sub two { return [ $val1, $val2 ]; } my $aref = two(); print $aref->[0], $aref->[1]; ## 3. As a hashref sub three { return { val1 => $val1, val2 => $val2 }; } my $href = three(); print $href->{ 'val1' }, $href->{ 'val2' };

    I'm sure there are other ways too, but these are commonly the way I do it. I would suggest 3. for unrelated values, and 1. only if you actually want to return an array of similar values.


      I would choose method three as well. When I was starting out doing this I tried returning data in a regular hash (not a hash ref), and that worked, kind of sort of, but then my data got a little more complicated, and I tried putting refs to other hashes in my hash, and this crashed and burned.

      So, I would get used to passing data back and forth with hashrefs, or in some circumstances, array refs. I wouldn't return any non-ref array or hash values, just for the sake of... well, that is what my gut says.

      another way:
      sub another_way { ... return (val1 => $val1, val2 => $val2); } my %hash = another_way(); print "$hash{val1} $hash{val2}\n";
Re: Subroutines and Passing data ...
by jdhedden (Deacon) on Jun 13, 2005 at 13:48 UTC
    > p.s. I have been reading on subroutines but this doesn't seem very clear ! ;-)

    I found at least 3 examples that show what you wanted to know in perldoc perlsub.

    There's always one more bug.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://466148]
Approved by robartes
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2019-11-13 00:10 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (66 votes). Check out past polls.