Push a subroutine with argument into a stack in Perl

Viewed 166

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.

2 Answers
Related