Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active July 27, 2023 22:07
Show Gist options
  • Save ernstki/22aa60057b9b0c30f83d7623569947e8 to your computer and use it in GitHub Desktop.
Save ernstki/22aa60057b9b0c30f83d7623569947e8 to your computer and use it in GitHub Desktop.
perlrun whirwind tour

perlrun whirlwind tour

Here are some shebang (#!/path/to/interpreter) fun facts.

And why shebang? In music, '#' means sharp. So just shorten #! to sharp-bang. Or it might be derived from "shell bang". All this probably under the influence of the american slang idiom "the whole shebang" (everything, the works, everything involved in what is under consideration). See also the wiktionary, jargon dictionary or Merriam-Websters. Sometimes it's also called hash-bang, pound-bang, sha-bang/shabang, hash-exclam, or hash-pling (british, isn't it?).

According to Dennis M. Ritchie (email answer to Alex North-Keys) it seems it had no name originally.

And Doug McIllroy mentioned (TUHS mailing list), that the slang for # at Bell Labs most probably was "sharp" at the time.

-d

perl -d:Trace  # use Devel::Trace
perl -de0      # poor man's Perl REPL

-e, -E

perl -e 'say "Hi"'
perl -E 'say "Hi"'  # same as 'use v5.28;'

awk-like

  • -F input field separator for -a
    • R-R-R-R-REVELATION: it's a pattern
  • -a "auto-split" into @F array

sed-like

  • -i "in-place"
  • -n like sed -n ("no print")
  • -p like sed -p ("print")

-V

perl -V
perl -V:'.*' | less
perl -V:archname
perl -V::archname
perl -V::archname:

record splitting

  • -0777 read entire file as a string

      # try all variations of -lne / -lpe // -pe
      echo -e "one\ntwo\nthree\n" | perl -0777 -lne 'print "“$_”";'
    
    • the alternative:

        echo -e "one\ntwo\nthree\n" | perl -e '
            undef $/;
            $s = <>;
            print "“$s”\n";
        '
      
  • -l automatically chomps, then adds the line ending back on

      INPUT=$HOME/src/WRL/scrub-bed/t/data/input.bed
      perl -F\\t -lane 'print join(":", @F[3,0..2])' $INPUT
      perl -F\\t -ane 'print join(":", @F[3,0..2])' $INPUT
    

    Possibly useful with find -print0

Bash quoting

Here strings

Passes string to standard input (like echo "string" | perl):

perl -lne 'print substr($_,0,2)' <<<"one
two
three"

Here docs

Passes "document" to standard input; end with the same token you started with. You can add -TOKEN to strip leading tabs for alignment within scripts.

perl -lne 'print substr($_,0,2)' <<EOF
> one
> two
> three
> EOF

ANSI-C strings

Sometimes useful to get special characters into a quoted string that are easiest to represent with ANSI-C escape sequences, like \t, \r, \a.

With Perl switches like -F it's not that useful. With BSD sed, though, for example, it might be useful for getting a literal newline into a replacement pattern.

Modulinos

Source: https://perlmaven.com/modulino-both-script-and-module

#!/usr/bin/env perl
# see: https://perlmaven.com/modulino-both-script-and-module

use strict;
use warnings;
use v5.10;

doit() if not caller;

sub doit {
    say "We did it.";
}

1;

Then try:

perl -I. -Mmodulino -e0
./modulino.pm
perl -MO=Deparse -E 'for ($i = 1; $i <= 10; $i++) { say $i; }'

# deparse at "level 9"
perl -MO=Deparse,-x9 -E 'for ($i = 1; $i <= 10; $i++) { say $i; }'
print B::Deparse->new(qw/-p -q -P/, "-x$xlevel")->coderef2text($_[0]);
# as a Perl function; courtesy of Chris
sub deparse (&;$) {
    package console;
    (require B::Deparse);
    (my $xlevel = (defined($_[1]) ? $_[1] : 5));
    print(((($_[0] . ' -x') . $xlevel) . "\n"));
    print('B::Deparse'->new(('-p', '-q', '-P'), ('-x' . $xlevel))->coderef2text($_[0]));
}
deparse { print "foo" }

The output of perl -E when run through -MO=Deparse

use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'postderef_qq', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment