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.
perl -d:Trace # use Devel::Trace
perl -de0 # poor man's Perl REPL
perl -e 'say "Hi"'
perl -E 'say "Hi"' # same as 'use v5.28;'
-F
input field separator for-a
- R-R-R-R-REVELATION: it's a pattern
-a
"auto-split" into@F
array
-i
"in-place"-n
likesed -n
("no print")-p
likesed -p
("print")
perl -V
perl -V:'.*' | less
perl -V:archname
perl -V::archname
perl -V::archname:
-
-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
automaticallychomp
s, then adds the line ending back onINPUT=$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
Passes string to standard input (like echo "string" | perl
):
perl -lne 'print substr($_,0,2)' <<<"one
two
three"
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
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.
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';