Search code examples
perlsubroutineperl-data-structures

Push a subroutine with argument into a stack in Perl


I would like to push a subroutine with arguments into a stack, but I can't figure out the syntax. Consider a working example with no arguments:

#!/usr/bin/perl -w
use strict;
use warnings;

sub hi    { print "hi\n";    }
sub hello { print "hello\n"; }
sub world { print "world\n"; }

my @stack;
push (@stack, \&hi   );
push (@stack, \&hello);
push (@stack, \&world);

while (@stack) {
    my $proc = pop @stack;
    $proc->();
}

when I run the code:

% ./pop-proc.pl
world
hello
hi

Now my question is, what if the subroutine looked like this:

sub mysub 
{
    chomp( my( $arg ) = @_ );
    print "$arg\n"; 
}

and I'd like to push subroutines with arguments, such as:

mysub("hello");
mysub("world");

your input is highly appreciated.


Solution

  • Use an anonymous sub (which may even be a closure).

    push @stack, sub { mysub("hello") };
    push @stack, sub { mysub("world") };
    

    For example,

    sub hi { say "@_" }
    my $arg = "Hello";
    my $sub = sub { hi($arg, @_) };
    $sub->("World");   # Hello World