Skip to content

Instantly share code, notes, and snippets.

@dex4er
Last active April 3, 2024 08:13
Show Gist options
  • Select an option

  • Save dex4er/1330416 to your computer and use it in GitHub Desktop.

Select an option

Save dex4er/1330416 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))'
# 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
# Arguments: '{http://some.name.space/}rootTag' *.xsd
perl -MXML::Compile::Schema -le '$r=shift @ARGV; $s=XML::Compile::Schema->new; $s->importDefinitions($_) for @ARGV; print $s->template("XML", $r, show=>"ALL")'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment