| Category: | Utility Scripts |
| Author/Contact Info | /msg Aristotle |
| Description: | If, like me, the vast majority of oneliners you write are -n or -p ones, you'll probably have cursed at the verbosity and unwieldiness of the -e'BEGIN { $foo } s/bar/baz; END { $quux }' construct. Hey, I thought, I can do better than that. So I ripped apart the Getopt::Std code and based this script on it, which adds two options to Perl:
Enjoy. Update: changed hardcoded location of Perl binary to $^X in last line as per bart's suggestion. |
#!/usr/bin/perl -w
use strict;
my %blockname = (
e => '%s;',
B => '; BEGIN { %s };',
E => '; END { %s };',
);
my @arg;
while (@ARGV && (my ($switch, $rest) = $ARGV[0] =~ /^-(.)(.*)/)) {
if ($rest eq '-') { # early exit if --
push @arg, @ARGV;
last;
}
if ($switch !~ /[BEe]/) {
push @arg, shift @ARGV;
next;
}
shift (@ARGV);
if (not length $rest) {
if(@ARGV) {
$rest = shift (@ARGV);
}
else {
$! = 2; # emulate perl(1)
die "No code specified for -$switch.";
}
}
push @arg, -e => sprintf $blockname{$switch}, $rest;
}
exec { $^X } $^X, @arg;
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: New switches for perl(1)
by bart (Canon) on Apr 22, 2003 at 06:37 UTC | |
by Aristotle (Chancellor) on Apr 22, 2003 at 14:05 UTC |