Dumb
Dumb

Reputation: 91

how to call another subroutine after the return statement

How can I call another subroutine from existing subroutine just after the return statement in perl. I dont want to call before the return statement as it is taking time to render. I do not want to wait. return it and then call another subroutine before the exit. Is it possible in perl?

Upvotes: 0

Views: 284

Answers (2)

simbabque
simbabque

Reputation: 54333

Your question is tagged as Moose, so here is how you do what you want with a method modifier. The after modifier runs after a sub is, but its return value is ignored.

package Foo;
use Moose;

sub frobnicate {
  my $self = shift;

  # ...

  return 123;
}

after frobnicate => sub {
  my ($self, $rv) = @_;

  $self->barnicate;
};

1;

Now whenever frobnicate is done, barnicate will be called.

Upvotes: 1

mob
mob

Reputation: 118635

You can fork and run your subroutine in a new process while the original process is returning.

sub do_something {
    my ($var1, $var2, $var3) = @_;
    my $output = ...

    if (fork() == 0) {
        # child process
        do_something_else_that_takes_a_long_time();
        exit;
    }
    # still the parent process
    return $output;
}

Upvotes: 5

Related Questions