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..
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.