Notes taken while reading:
Minimal Perl: For Linux and Unix People
default
T/F Numbers:
0, 0.0, any value equating to 0 is False.
T/F Strings:
"", '', "0", '0' are False.
T/F Expressions:
Variables that have not been assigned, or any
expression without a value, undef is False.
When using one liners:
-e = use command line; instead of a script file
-l = use correct line termination for hosted OS.
-M = loads modules
-n = input reading loop, stored in $_
-p = input reading loop, and print each line.
-s = use custom switches
-w = warnings enabled
-0octal = sets record seperator
-00 for paragraph mode
-0777 for file mode
$_ = when n or p are used $_ holds the last
read input record. aka: data variable.
$. = with n/p $. holds the number of the current
input record. aka: record-number variable.
Using Switches on command line:
-name — Set value to true
-name='yuk' — Set to chosen value
Declaring Switches in script files:
our ($chosenVar) — Makes the chosenVar optional
our ($this, $that) — Declare more than one our switch
Perl can use BEGIN {} and END {} blocks, just like awk.
This lets you seperate parts of the program from the
reading loop when -n -p are used.
***TODO: Add an example script here ***!
perl -wl -e 'print $ENV{HOME};'
Print shells value of $HOME.
Perl stores the shells variables in the
hash: $ENV
perl -wl -e 'print "Holas Mundo!";'
Prints a string.
perl -wl -e 'print 24/7;'
Do a calculation.
perl -wnl -e 'print;' play-data.txt
perl -wnl -e 'print $_;' testing.txt
perl -w -e 'while (<>) {print; }' info.txt
perl -wpl -e '' perl-notes.txt
Print the contents of a file.
perl -wnl -e 'print "$.: $_";' liar.txt
perl -wnl -e 'printf "$.: "; print;' list-em.txt
Print file with line numbers.
perl -wnl -e 'print; exit;' play-data.txt
cat play-data.txt | perl -wnl -e 'print; exit;
Print first line of a file.
perl -wpl -e 's/^/ /g;' somefile.txt
perl -wnl -e 's/^/ /g; print;' list.txt
Add a space to the beginning of each line.
perl -wn -e 'print $.;' count-me.txt
Accumulate each line number of a file.
perl -wnl -e 'print $.;' count-me.txt
perl -wn -e 'print $. , "\n"; ' count-me-too.txt
Replace each line with it's line number.
perl -wl -e '$greet="holas"; print $greet;'
Example of using a variable on command line.
perl -wl -e '$fname="joe"; $lname="mammy";
print "$fname$lname is your daddy!";'
Using variable on command line. Two lines capable
because the ' is not closed till end of second.
perl -00 -wnl -e 'print "$.: $_";' count-para.txt
Numbers paragraphs, with extra lines spacing
removed.
perl -M'Text::Autoformat' -00 -wn -e 'print autoformat "$.: $_", { right => 40 };' test-data.txt
Number each paragraph, autoformat it to be
a little prettier and adjust the with to
40 characters; for easier speed reading.
*** The autoformat module must be installed.
default