in reply to Re: how to assign 'system' to a value, and eval it.
in thread SOLVED:how to assign 'system' to a value, and eval it.

yeah, thank you.that's the way I want.now I realized the necessity of using reffed subroutine.
my %run = (x => \&system, X => \&myrun); my ($run, @args); foreach(@ARGV){ if (/^-([xX])$/){ $run = $run{$1} }elsif (/^-/){ push @args, $_ } $run->(@args)
It's using prepared hash to switch run command, that's the logical way instead of using 'if ($run eq 'x'){system ...}', the ugly way I tried to avoid.

Replies are listed 'Best First'.
Re^3: how to assign 'system' to a value, and eval it.
by Corion (Patriarch) on Jun 09, 2024 at 17:54 UTC

    Consider using Getopt::Long instead of hand-rolling your option parsing. Getopt::Long can also set up your callbacks but will handle many more features:

    use Getopt::Long qw(:config pass_through); my $run; GetOptions( 'x|run-using-system' => sub { $run = \&system }, 'X|run-using-mysystem' => sub { $run = \&mysystem }, ); $run->(@ARGV);
      I'm writing rsync wrapper script,It means to collect and separate rsync args from filenames, anyway thanks for your advice, 'passthrough' is good.