Perl Tricks, Perl One Liners, Oneliners, PCRE, March 4, 2014


Remember Casey West's Perl Power Tools, PPT::Util

* To make glob work correctly across Windows and Linux, use Win32::AutoGlob, this is perl only code that will work on Linux or Windows

* List where perl is getting its includes, @INC

env -i perl -V

* Equivalent of the strings(1) linux command

perl -nlwe "print for m/\w{3,}/g"

* Perl equivalent to grep(1)

perl -MGlobArgs -ne "/\bwhateverToSearch\b/ & print"

or something a little easier to type

perl -ne "print if /PATTERN/"

* Remove empty directories

perl -MFile::Find -e"finddepth( sub { rmdir }, '.')"

* Remove a directory tree

perl -e 'use File::Path; rmtree("p:/directoryToRemove");'

* Create a directory if it does not exist

perl -e "mkdir($$ARGV[0], 0700) unless(-d $$ARGV[0])" "p:/directoryToMake"

* Check to see if a module is installed, for example File::Spec

perldoc -l File::Spec

* print an array from the command line

Use print join ',', @array

perl -MFile::Spec -e"print join ',', File::Spec->splitter('r:\tools\edt' )"

* Print the arguments from the command line (implement /bin/echo)

perl -e"print join ' ', @ARGV, \"\n\”"

* Print each element of the PATH on a separate line on Windows

path | perl -an -F; -e “print join($/, @F)”
or
perl -MEnv -e”print join($/, split(/;/, $PATH))"

* Print each element of the PATH on a separate line on Linux

echo $PATH | perl -an -F: -e”print join($/, @F)"

* Using ack-grep like a recursive grep

ack -u -G .gpj FAST

-u -- unrestricted file extensions, all files
-G -- match .gpj in the file name
"FAST" -- match FAST in the files found

* Base64 decode

perl -MMIME::Base64 -ne 'binmode STDOUT; print decode_base64($_)' < ../mailblob.txt > foo.zip

* Implementing the UNIX touch(1) command

perl -MWin32::Autoglob -e"utime time(), time(), @ARGV" files_to_touch

* Perl Command Line Calculator

Start perl in the debugger with the script "1;", which will return true. You can then type in expressions in the debugger and have them evaluated.

perl -d -e 1

* Perl "say" can be implemented on older versions of Perl with

sub say { print @_, "\n" }

* Installing packages manually

* Go to CPAN find the package
* Over on the right side, near the top, there will be a tar.gz link
* wget the package
* unpack it
* perl Makefile.PL
* make
* make test
* sudo make install

• Initializing a list of variables with a value

$_ = 4 for my ($x, $y, $z)
Report abuse