-
-
Save ironcamel/1240122 to your computer and use it in GitHub Desktop.
steroids for your perl one-liners.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
Thanks :) I forgot about exec. Fixed now.
I have promoted this to a real module and pushed it to CPAN as App::p.
https://github.com/ironcamel/App-p
https://metacpan.org/module/App::p
https://metacpan.org/module/p
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice! How come you don't use exec? ;-)