public
Last active — forked from mfontani/raptor.sh

steroids for your perl one-liners.

  • Download Gist
p
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#!/usr/bin/env perl
# Based on Randy Stauner's http://blogs.perl.org/users/randy_stauner/2011/06/exploratory-one-liners-with-less-typing.html
# and Marco Fontani's https://gist.github.com/1042504.
# To install all dependencies:
# sudo cpanm File::Slurp JSON::XS Data::Dump YAML::XS utf8::all Class::Autouse
# Installation:
# copy this file to ~/bin/p
# Example usage:
# p 'dd [File::Spec->path]' # dynamically load arbitrary modules
# p -pe 's/foo/bar/' foo.txt # use your favorite options like -lane
# p 'say "hello world!" # -E is assumed if no options are provided
# p 'dd yl r "config.yml"' # chain commands
# p 'dd ExtUtils::Installed->new->modules' # list all installed modules
use strict;
use warnings;
 
die q{
Usage: p [-lneE etc] 'code'
The code can make use of:
r to File::Slurp::read_file
w to File::Slurp::write_file
S to say()
p to print()
dd to Data::Dump::dd()
jd to JSON::XS::encode (utf8/pretty)
jl to JSON::XS::decode (utf8/allow nonref) a thing
yd to YAML::Dump()
yl to YAML::Load()
} unless @ARGV;
unshift @ARGV, '-E' if @ARGV == 1;
 
exec
'perl',
'-Mwarnings',
'-MFile::Slurp',
'-MJSON::XS',
'-MData::Dump',
'-MYAML::XS',
'-Mutf8::all',
'-MClass::Autouse=:superloader',
'-E',
q[BEGIN {
sub r { scalar read_file shift }
sub w { write_file @_ }
sub S { say @_ ? @_ : $_ }
sub p { print @_ ? @_ : $_ }
sub yd { print Dump(shift) }
sub yl { Load(shift) }
sub jd { print JSON::XS->new->utf8->pretty->encode(shift) }
sub jl { JSON::XS->new->utf8->allow_nonref->decode(shift) }
}], @ARGV;

Very nice! How come you don't use exec? ;-)

Thanks :) I forgot about exec. Fixed now.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.