Skip to content

Instantly share code, notes, and snippets.

@Prajithp
Forked from dex4er/base64.sh
Created January 17, 2018 09:57
Show Gist options
  • Select an option

  • Save Prajithp/7de5f832a71724c5d40dbf9a6ace61d7 to your computer and use it in GitHub Desktop.

Select an option

Save Prajithp/7de5f832a71724c5d40dbf9a6ace61d7 to your computer and use it in GitHub Desktop.
Perl oneliners
# Perl 2 JSON
perl -MJSON -ple '$_=encode_json eval $_'
# JSON 2 Perl
perl -MJSON::PP -MJSON::Syck=Load -MData::Dumper -ple '$_=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($_)'
# Encode ASCII string as GSM 7bit string (without charmap conversion)
perl -ple '$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))'
# Encode UTF-8 as ESTI GSM 03.38
perl -MEncode -pi -e '$_=encode("gsm0338", $_)'
# Decode ESTI GSM 03.38 to UTF-8
perl -MEncode -pi -e '$_=decode("gsm0338", $_)'
# URL encoding by RFC 3986 (by line)
perl -MURI::Escape -ple '$_=uri_escape($_)'
perl -MURI::Escape -ple '$_=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