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

Personally, I would avoid using eval, but if you're going to use eval, you might as well do it this way:

#!/usr/bin/perl -w use strict; use warnings; my $condition = 0; sub RunProg # Run program only if it exists { my $CMD = join(' ', @_); my $PRG = ($CMD =~ m/^([a-zA-Z0-9_\/\-.~!]+)/) ? $1 : ''; if (length(`which $PRG`)) { return system("$CMD"); } return -1; } my @args = ('yad --help'); my $RET = eval(($condition ? 'system' : 'RunProg') . '(@args);' ); print "\nRETURN VALUE = $RET\n";

Replies are listed 'Best First'.
Re^2: how to assign 'system' to a value, and eval it.
by Anonymous Monk on Jun 10, 2024 at 01:06 UTC
      if (length(`which $PRG`))

    Use the core module IPC::Cmd for increased portability and functionality:

    use IPC::Cmd 'can_run'; my $path = can_run($PRG);
      I don't know IPC has it.
Re^2: how to assign 'system' to a value, and eval it.
by vincentaxhe (Scribe) on Jun 10, 2024 at 01:53 UTC
    That helps too.