Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Subroutine's output as input to a subroutine

by props (Hermit)
on Nov 20, 2007 at 22:33 UTC ( [id://652009]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks is it possible or how to send the return value of a subroutine A as an input to subroutine B ? here is my example:
sub makeitbig { if (@_) { $default_value =shift; } else{ return $default_value; } sub testmode { my $random1 = int( rand(&makeitbig()) ); return $random1; }
in &testmode the &makeitbig is not returning anything is expecting a value to process.Any Perl technique to tackle this ? Many thanks

Replies are listed 'Best First'.
Re: Subroutine's output as input to a subroutine
by ikegami (Patriarch) on Nov 20, 2007 at 22:42 UTC

    Subroutines don't "send", they "return".
    When you say "input", you seem to mean "argument".
    Please learn the terminology. It'll help make your posts understandable.

    in &testmode the &makeitbig is not returning anything is expecting a value to process

    No, not nothing. It's returning the value in $default_value. Did you forget to assign something meaningful to $default_value?

Re: Subroutine's output as input to a subroutine
by shmem (Chancellor) on Nov 20, 2007 at 23:46 UTC
    is it possible or how to send the return value of a subroutine A as an input arguments to subroutine B ?

    Sure, by placing a subroutine call inside the parens which delimit another sub-call's arguments. If

    sub A { return $_[0] x 2 . ' (from A)'; } sub B { my $arg = shift; return "B got $arg as argument\n"; }

    then

    print B(A("c")),"\n";

    results in

    B got cc (from A) as argument

    The return value from A() ends up at the right place - inside the parens of B().

    --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's output as input to a subroutine
by perlfan (Vicar) on Nov 20, 2007 at 23:28 UTC
    I have no idea what you're trying to do, but you're missing a closing } and get rid of the &
    sub makeitbig { my $default_value = 'xxx'; if (@_) { $default_value = shift; } return $default_value; } sub testmode { my $random1 = int( rand(makeitbig()) ); return $random1; }
    BTW, this could be rewritten:
    # not tested, but you get the gist sub makeitbig { return @_ ? shift @_ : 'xxx'; } sub testmode { return int( rand(makeitbig()) ); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-24 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found