in reply to become another via exec in dispatch table

exec 'perl -e "print map {qq!\t$_\n!}"',@_

will fail with ENOENT (No such file or directory) when @_ isn't empty. When using the multiple argument form of exec the first argument is to be the program to execute. There's no file named perl -e "print map {qq!\t$_\n!}", much less a program.

And when @_ is empty, you have a different problem. perl will run, but the shell will have replaced $_ with something else due to improper quoting.

The solution to both is:

exec 'perl', '-e', 'print map {qq!\t$_\n!}', @_

Are these the problems you were asking about?

PS — If you had a problem with exec, you should have checked what error exec returned.

Replies are listed 'Best First'.
Re^2: become another via exec in dispatch table
by Discipulus (Canon) on Jan 31, 2012 at 10:46 UTC
    sorry may be i do not explicate well my idea: i changed the b k key of dispatch table as suggested by ikegami:
    b => sub {exec 'perl', '-e', 'print map {qq!\t$_\n!}', @_},

    but does not work alone like in:
    code-dispatch.pl --execute --key b   a a a
    nor in pipeline, like:
    echo 1 2 3 | code-dispatch.pl --execute --key b

    theese line only print 'ole' (the check if that key exists in the table) and do not print a column of argouments as intended to do (by me)

    thanks

    there are no rules, there are no thumbs..

      Again, "does not work" is not an adequate description. Specifically, you get:

      ole syntax error at -e line 1, at EOF Execution of -e aborted due to compilation errors.

      So lets look at the code:

      print map {qq!\t$_\n!}

      Where's map's list of values?

        ok i was so stupid to cut and paste your suggestion in my code.. i speak too generic like in 'does not work'.. maybe my original idea is stupid/unpractical/bogus prone/or worst...

        but really no one understand what i'm asking for?
        I have changed the dispatch table in this way:
        my %cmds = ( a => sub {exec 'perl', '-e', 'print map {qq!\t$_\n!} @ARG +V', @_}, b => sub {exec 'perl', '-e', 'print map {qq!\t$_\n!} @ARG +V', @_}, b1 => sub {exec 'perl', '-e', 'print map {qq!\t$_\n!} @_' +, @_}, b2 => sub {exec 'perl', '-e', 'print qq!@ARGV!', @_}, c => sub {my $res; foreach (@ARGV) {$res+=$_}; print $res +;exit 0 }, ); ## # calling with --key a b b1 b2 like in # dispatch.pl --execute --key a A B C # everytime produces: # #ole #--a blank line-- #only the c key runs as expected (to me) but not use exec #c:\SCRIPTS>dispatch.pl --execute --key c 1 2 3 4 5 #ole #15 #c:\SCRIPTS> #key c in a pipeline produces: #c:\SCRIPTS>echo 1 2 3 | dispatch.pl --execute --key c #ole #Use of uninitialized value $res in print at c:\SCRIPTS\code-dispatch. +pl line 16. c:\SCRIPTS>

        sorry about my errors
        in any case thanks for the attention
        L*

        there are no rules, there are no thumbs..