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

Sounds like a job for coderefs.

#!/usr/bin/env perl use strict; use warnings; sub myrun { print "Yeah, just printing @_\n"; } sub mysystem { system @_; } for (0..6) { my $run = $_ % 2 ? \&mysystem : \&myrun; $run->("echo Number $_"); }
did not work like bash do

You'll have to give more detail here. An SSCCE would be good.


🦛

Replies are listed 'Best First'.
Re^2: how to assign 'system' to a value, and eval it.
by Fletch (Bishop) on Jun 09, 2024 at 17:06 UTC

    And the google fodder you're (the OP) probably going to want next is "dispatch table".

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^2: how to assign 'system' to a value, and eval it.
by vincentaxhe (Scribe) on Jun 09, 2024 at 17:13 UTC
    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.

      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.