Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

To call a sub from another sub

by tamaguchi (Pilgrim)
on Feb 02, 2006 at 11:21 UTC ( #527304=perlquestion: print w/replies, xml ) Need Help??
tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

I would like to call a subrutine from another subrutine is this possible, and how does one do it? Thank you for any help.

Replies are listed 'Best First'.
Re: To call a sub from another sub
by davorg (Chancellor) on Feb 02, 2006 at 11:29 UTC

    Er... yes. It works exactly how you'd expect it to. Or, at least, how I'd expect it to. What did you try? What happened when you tried it?

    sub a_subroutine { print "this is a subroutine\n"; } sub another_subroutine { print "this is another subroutine\n"; a_subroutine(); } another_subroutine();

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: To call a sub from another sub
by tirwhan (Abbot) on Feb 02, 2006 at 11:27 UTC

    Did you try it out? It just works(tm).

    perl -e 'sub one{print "one\n"};sub two{one()};two()'

    Dogma is stupid.
Re: To call a sub from another sub
by bart (Canon) on Feb 02, 2006 at 11:29 UTC
    The same way as you'd call it from the top level. Have you tried it?

    I think you should spend some time in the first chapters of some Perl books for beginners, like Learning Perl by our own merlyn, or Beginning Perl by Simon Cozens. If you look closely at the latter page, you can find a link to a free online version.

Re: To call a sub from another sub
by TedPride (Priest) on Feb 02, 2006 at 11:31 UTC
    He probably wants to know how to pass one sub as an argument to another:
    use strict; use warnings; bongo(\&bingo); sub bingo { print "bingo"; } sub bongo { my $p = $_[0]; $p->(); }
      I would like that subrutine1 sbould get a value from subrutine2, It should process this value and return the result to subrutine2. Is this possible or do I have to pass subrutine1 to subrutine2?
        Yes, it'll work just fine. It will even work if the outer subroutine is a lexical closure and passes the other subroutine one of the lexically-retained references it is holding, like this:
        sub :: {return lc pop} sub jonadab { my $lex = shift; @ARGV=qw(http://); return sub{ my ($ical) = @_; my $closure = :: $lex.".".$ical."/".$/; }} my $foo = jonadab("PerlMonks"); $\=$foo->("Org"); print shift;

        Note: if you turn in code like that for homework, don't expect a good grade.

        Perhaps you want this :
        sub subroutine_1 { my $val = subroutine_2(); print "$val this is subroutine_2 output!"; } sub subroutine_2 { return "w00t!"; }
        It does not pass it back to subroutine_2 but should give you a rough idea of how things like these work.
Re: To call a sub from another sub
by TedPride (Priest) on Feb 02, 2006 at 12:04 UTC
    use strict; use warnings; whatever(); sub whatever { my $value = 5; $value = increment($value); print $value; } sub increment { return $_[0] + 1; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://527304]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2017-06-27 04:44 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (598 votes). Check out past polls.