Skip to content

@ironcamel /p forked from mfontani/raptor.sh
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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

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

@ironcamel
Owner

Thanks :) I forgot about exec. Fixed now.

@ironcamel
Owner

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
Something went wrong with that request. Please try again.