To call a sub from another sub
tamaguchi
created: 2006-02-02 06:21:40
I would like to call a subrutine from another subrutine is this possible, and how does one do it? Thank you for any help.
Re: To call a sub from another sub
created: 2006-02-02 06:27:23

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
created: 2006-02-02 06:29:30

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();
--
<http://dave.org.uk>

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

Re: To call a sub from another sub
created: 2006-02-02 06:29:59
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
created: 2006-02-02 06:31:38
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->();
}
Re^2: To call a sub from another sub
created: 2006-02-02 06:55:04
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?
Re^3: To call a sub from another sub
created: 2006-02-02 07:02:36
Perhaps you want this :
sub subroutine_1 {
  my $val = subroutine_2();
  print "$val this is subroutine_2 output!";
}

sub subroutine_2 {
  return "w00t!";
}
Re: To call a sub from another sub
created: 2006-02-02 07:26:43
Two points:
  • You missed the part where a value is passed in, processed, and returned.
  • Don't just hand him complete working code. He made no attempt to write any code himself, and he won't learn anything until he does, and it's obviously homework, the first assignment of the semester by the look of it.
Re: To call a sub from another sub
created: 2006-02-02 07:19:22
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.

Re: To call a sub from another sub
created: 2006-02-02 07:04:29
use strict;
use warnings;

whatever();

sub whatever {
    my $value = 5;
    $value = increment($value);
    print $value;
}

sub increment {
    return $_[0] + 1;
}

perlmonks.org content © perlmonks.org and Anonymous Monk, bart, davorg, jkva, jonadab, tamaguchi, TedPride, tirwhan

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03