Discipulus has asked for the wisdom of the Perl Monks concerning the following question:

hello wise ones

i had a strange Perl idea but i can get it works: i want a program that can transform itself into another thing on given argouments.
I would like to use it alone:
   me.pl --execute  --key sum
or in a pipeline, like in:
   echo '1 2 3' | me.pl --execute  --key sum | cat

I' m using Getopt::Long as it can grab options that i need in the main program, and can also leave other things in @ARGV..
The below code only works in the case c and not in pipeline:
   me.pl --execute  --key c 1 2 3
Any suggestion or explication are welcome

thanks
L*

#!/usr/bin/perl use warnings; use strict; use Getopt::Long; my ( $execute, $key); GetOptions ( "key=s" => \$key, "execute" => \$execute)||die; my %cmds = ( a => sub {exec 'perl -e "print map {qq!\t$_\n!}"',@_ }, b => sub {exec 'perl -e "$|++;print qq(ee r r );sleep 5"' +}, c => sub {my $res; foreach (@ARGV) {$res+=$_}; print $res +;exit 0 }, ); if ($execute && exists $cmds{$key} ) { print "ole\n"; $cmds{$key}->(@ARGV); } # theoretically never here .. print "still in \@ARGV: ", (join("|",@ARGV),"\n");
UPDATE:
on ikegami suggestion i add what i'm getting on tests: echo 1 2 3 | code-dispatch.pl --execute --key c gives something like:"the process proved to write in an unexisting pipe"
ole

Use of uninitialized value $res in print at c:\SCRIPTS\code-dispatch.pl line 14.

echo 1 2 3 | code-dispatch.pl --execute --key a prints:
ole
c:\SCRIPTS>syntax error at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
and program hangs..

L*
there are no rules, there are no thumbs..

Replies are listed 'Best First'.
Re: become another via exec in dispatch table
by ikegami (Patriarch) on Jan 31, 2012 at 10:29 UTC
    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.

      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?

Re: become another via exec in dispatch table
by ikegami (Patriarch) on Jan 31, 2012 at 10:23 UTC
    You didn't specify what it does wrong and what it should be doing instead.