Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Subroutine return value

by monk2b (Pilgrim)
on Nov 09, 2007 at 15:54 UTC ( [id://649922]=perlquestion: print w/replies, xml ) Need Help??

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

I am attempting to write a subroutine and get back the return value. I am unaware of a key ingredient that makes this work properly because I am not able to get the return value.

This is what I have so far. I would appreciate any help in making me understand what I don't know about passing the return value back to the rest of the script.
#!/usr/bin/perl use warnings; use strict; my $List = "/bin/ls"; my $tmp = "/tmp"; my $var = "/var"; my ($Results1, $Results2); &DoList($List, $Results1, $Results2, $tmp, $var); print "Tmp is\n$Results1\n"; print "Var is\n$Results2\n"; sub DoList { my ($List, $Results1, $Results2, $tmp, $var); ($List, $Results1, $Results2, $tmp, $var) = @_; $Results1 = `$List $tmp`; $Results2 = `$List $var`; return( $Results1, $Results2); }
I got 99 problems, but a @%$()_ ain't one.

Replies are listed 'Best First'.
Re: Subroutine return value
by moritz (Cardinal) on Nov 09, 2007 at 15:58 UTC
    You have to assign the return value of your subroutine:

    my ($result1, $result2) = DoList($List, $tmp, $var);

    There's no need to pass $Results1, $Results2 to your sub.

      Thank you for the quick response.

      I got 99 problems, but a @%$()_ ain't one.
Re: Subroutine return value
by FunkyMonk (Chancellor) on Nov 09, 2007 at 16:02 UTC
    You probably want:
    my ($Results1, $Results2) = &DoList($List, $Results1, $Results2, $tmp, + $var);

    but it comes with free advice:

    • Don't use & when calling subroutines
    • Perl can read directory listings without resorting to external commands - take a look at opendir, readdir and glob.
Re: Subroutine return value
by shmem (Chancellor) on Nov 09, 2007 at 19:12 UTC
    my ($Results1, $Results2); &DoList($List, $Results1, $Results2, $tmp, $var);

    If you want the subroutine to modify its parameters, you have to modify elements of @_ directly, not taking a copy:

    sub DoList { my $List = $_[0]; # first element in @_ my ($tmp, $var) = @_[3,4]; $_[1] = `$List $tmp`; $_[2] = `$List $var`; }

    See perlsub, in the DESCRIPTION section just after the first code examples: "... a function is free to do in-place modifications of @_ and change its caller's values".

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Subroutine return value
by downer (Monk) on Nov 09, 2007 at 16:52 UTC
    you need to assign data to Results1 and Results2:
    my($results1, $results2) = &DoList(....);
Re: Subroutine return value
by narainhere (Monk) on Nov 10, 2007 at 05:45 UTC
    This would help if you don't want to modify your code much
    #!/usr/bin/perl use warnings; use strict; my $List = "/bin/ls"; my $tmp = "/tmp"; my $var = "/var"; my ($Results1, $Results2); &DoList(); print "Tmp is\n$Results1\n"; print "Var is\n$Results2\n"; sub DoList { $Results1 = `$List $tmp`; $Results2 = `$List $var`; }
    But mind this..global variables are discouraged in larger scripts.The safe thing is to assign the return value(s) as suggested by moritz.

    The world is so big for any individual to conquer

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found