Skip to content

Instantly share code, notes, and snippets.

@dex4er
Last active Jun 19, 2021
Embed
What would you like to do?
Perl oneliners
# Encoding file
perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' < file
# Decoding file
perl -MMIME::Base64 -ne 'print decode_base64($_)' < file.b64
# HMAC-SHA1
perl -MDigest::HMAC_SHA1 -le '$_=Digest::HMAC_SHA1->new($ARGV[0])->add($ARGV[1])->b64digest; $_.="=" x length % 4; print' TestKey TestString
# Normalize date
perl -MPOSIX::strptime=strptime -MPOSIX=strftime -le '$f="%Y-%m-%d %H:%M:%S"; print strftime $f, strptime "2012-3-14 5:6:7", $f'
# Perl 2 JSON
perl -MJSON -ple '$_=encode_json eval $_'
# JSON 2 Perl
perl -MJSON::PP -MJSON::Syck=Load -MData::Dumper -pe '$_=Dumper(Load(encode_json(JSON::PP->new->relaxed->allow_barekey->decode($_))));'
# MD5 checksum (whole file)
perl -MDigest::Perl::MD5=md5_hex -le 'undef $/; $_=<STDIN>; print md5_hex($_)'
rlwrap -m -A -pgreen -S"perl> " perl -CS -w -MData::Dump -wnE'@_=eval;print"\033[0;33m";dd@_;print"\033[0;31m$@"if$@'
# Test webserver
plackup -e 'sub{[200,[],[Data::Dumper::Dumper@_]]}'
# Fileserver
plackup -MPlack::App::Directory -e 'Plack::App::Directory->new'
# HTTP proxy (Starlight is the most stable server)
starlight -MPlack::App::Proxy -e 'enable q{AccessLog};enable q{Proxy::Connect};enable q{Proxy::Requests};Plack::App::Proxy->new->to_app'
# Serving cgi-bin scripts
plackup -MPlack::App::CGIBin -e 'Plack::App::CGIBin->new(root=>".",exec_cb=>sub{1})->to_app'
# Assemble regexp from list of strings
perl -MRegexp::Assemble -pe 'BEGIN { $r=Regexp::Assemble->new }; $r->add($_); undef $_; END { print $r->re, "\n" }'
# Encode UTF-8 string as GSM 03.38 7-bit string
perl -MEncode -ple '$_=encode "GSM0338", decode "UTF-8", $_; $l=length($_)*2; $l -= $l>6 ? int($l/8) : 0; $b=unpack "b*", $_; $b=~s/(.{7})./$1/g; $_=sprintf "%02X%s", $l, uc unpack "H*", pack "b*", $b'
# Decode GSM 03.38 7-bit string to UTF-8
perl -MEncode -ple 's/^(..)//; $l = (hex($1)+2)*4; $b=unpack "b*", pack "H*", $_; $b=~s/(.{7})/$1./g; $b=substr $b, 0, $l; $b=substr $b, 0, int(length($b)/8)*8; $_=decode "GSM0338", pack "b*", $b;'
# Encode UTF-8 string as ESTI GSM 03.38 IRA hex string
perl -MEncode -ple '$_=uc unpack "H*", encode "GSM0338", decode "UTF-8", $_'
# Decode ESTI GSM 03.38 to UTF-8 IRA hex string
perl -MEncode -ple '$_=encode "UTF-8", decode "GSM0338", pack "H*", $_'
# Encode UTF-8 as UCS2-LE IRA hex string
perl -MEncode -ple '$_=uc unpack "H*", pack "n*", unpack "v*", encode "UCS-2LE", decode "UTF-8", $_'
# Decode UCS2-LE IRA hex string to UTF-8
perl -MEncode -ple '$_=encode "UTF-8", decode "UCS-2LE", pack "v*", unpack "n*", pack "H*", $_'
# URL encoding by RFC 3986 (by line)
perl -MURI::Escape -pe '$_=uri_escape($_)'
perl -MURI::Escape -pe '$_=uri_unescape($_)'
# Pretty printing in compact form
perl -MXML::Twig -MText::Wrap -e 'package Text::Wrap; $columns=78; $huge="overflow"; XML::Twig->new(pretty_print=>"indented_c")->parse(\*STDIN)->print;'
# Pretty printing with attributes in separate lines (VCS-friendly)
perl -MXML::Twig -MText::Wrap -e 'package Text::Wrap; $columns=78; $huge="overflow"; XML::Twig->new(pretty_print=>"indented_a")->parse(\*STDIN)->print;'
# XSD 2 example XML
perl -MXML::Compile::Schema -le '$r=shift @ARGV; $s=XML::Compile::Schema->new; $s->importDefinitions($_) for @ARGV; print $s->template("XML", $r, show=>"ALL")' '{http://some.name.space/}rootTag' *.xsd
# XSD 2 example YAML
perl -MYAML -MXML::Compile::Schema -le '$r=shift @ARGV; $s=XML::Compile::Schema->new; $s->importDefinitions($_) for @ARGV; print Dump eval $s->template("PERL", $r, show=>"ALL")'
# XML to YAML
perl -MYAML -MXML::Compile::Schema -MXML::Compile::Util=type_of_node -le '$x=XML::LibXML->new->parse_fh(\*STDIN); $r=$x->documentElement; print Dump (XML::Compile::Schema->new(\@ARGV)->compile(READER=>type_of_node($r), sloppy_integers=>1, sloppy_floats=>1)->($r));' *.xsd < file.xml
# YAML to XML
perl -MYAML=LoadFile,Dump -MXML::Compile::Schema -le
'$d=LoadFile(shift @ARGV); $r=shift @ARGV; $x=XML::LibXML::Document->new("1.0", "UTF-8"); $x=XML::Compile::Schema->new(\@ARGV)->compile(WRITER => $r)->($x, $d); print $x->toString(1)' file.yml '{http://some.name.space/}rootTag' *.xsd
# YAML to XML where data are inside hash key
perl -MYAML=LoadFile,Dump -MXML::Compile::Schema -le
'$d=LoadFile(shift @ARGV); $r=shift @ARGV; ($a=$r)=~s/{.*}//; $x=XML::LibXML::Document->new("1.0", "UTF-8"); $x=XML::Compile::Schema->new(\@ARGV)->compile(WRITER => $r)->($x, $d->{$a}); print $x->toString(1)' file.yml '{http://some.name.space/}action' *.xsd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment