Skip to content

Instantly share code, notes, and snippets.

@ironcamel
Forked from mfontani/raptor.sh
Created September 25, 2011 02:12
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ironcamel/1240122 to your computer and use it in GitHub Desktop.
steroids for your perl one-liners.
#!/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;
@rwstauner
Copy link

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

@ironcamel
Copy link
Author

Thanks :) I forgot about exec. Fixed now.

@ironcamel
Copy link
Author

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