Create a gist now

Instantly share code, notes, and snippets.

@ironcamel /p forked from mfontani/
Created Sep 25, 2011

What would you like to do?
steroids for your perl one-liners.
#!/usr/bin/env perl
# Based on Randy Stauner's
# and Marco Fontani's
# 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;
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? ;-)


ironcamel commented Sep 28, 2011

Thanks :) I forgot about exec. Fixed now.


ironcamel commented Sep 29, 2011

I have promoted this to a real module and pushed it to CPAN as App::p.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment