HOME
In Our Opinion
Writing Code
How To
The Command Line
Hack It Yourself
Security Everything
Linux Distributions
Mr-Oss's Store
News Feeds
News
Links
Blog
HOME
Stupid Perl Tricks PDF Print E-mail
Written by Mr-Oss   
Sunday, 18 January 2009

Image

STUPID PERL TRICKS

A collection of useful and useless perl command line techniques.

perl -e 'print "STUPID PERL TRICKS TO USE ON THE COMMAND LINE"'

 

Printing out your perl module include path

root@slacker:~# perl -e 'print join ("\n", @INC)' 

/usr/lib/perl5/5.8.8/i486-linux-thread-multi
/usr/lib/perl5/5.8.8
/usr/lib/perl5/site_perl/5.8.8/i486-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.8
/usr/lib/perl5/site_perl
 

Checking for specific perl modules existence ( does not exist on the system )

 root@slacker:~# perl -e 'use MIME::Lite'

Can't locate MIME/Lite.pm in @INC (@INC contains: /usr/lib/perl5/5.8.8/i486-linux-thread-multi /usr/lib/perl5/5.8.8 /usr/lib/perl5/site_perl/5.8.8/i486-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl .) at -e line 1. BEGIN failed--compilation aborted at -e line 1.
 

Install a perl module

root@slacker:~# perl -MCPAN -e 'install MIME::Lite'

........lots and lots of truncated text and questions to answer yes to

Installing /usr/lib/perl5/site_perl/5.8.8/MIME/Lite.pm
Installing /usr/lib/perl5/site_perl/5.8.8/MIME/changes.pod
Installing /usr/share/man/man3/MIME::Lite.3
Installing /usr/share/man/man3/MIME::changes.3
Writing /usr/lib/perl5/site_perl/5.8.8/i486-linux-thread-multi/auto/MIME/Lite/.packlist
Appending installation info to /usr/lib/perl5/5.8.8/i486-linux-thread-multi/perllocal.pod
  /usr/bin/make install  -- OK

 

Re-Check for a perl modules existence ( does exist on the system )

root@slacker:~# perl -e 'use MIME::Lite'
root@slacker:~#         

Print if pattern matches current file line

root@slacker:~# perl -nle 'print if /root/' /etc/passwd

 root:x:0:0::/root:/bin/bash

Print match when piped from command

root@slacker:~# ps -ef | perl -nle 'print if /bash/'

root      3907  3906  0 07:45 pts/1    00:00:00 -bash
root      3920  3907  0 07:45 pts/1    00:00:00 -bash
root      6534  6533  0 14:10 pts/3    00:00:00 -bash
root      7792  6534  0 14:48 pts/3    00:00:00 perl -nle print if /bash/

Conditional printing from regex match

 cat /etc/passwd | perl -nle '(=~/root/)?print "MATCHED FOR $_\n":print "NO MATCH\n"'

MATCHED FOR root:x:0:0::/root:/bin/bash

NO MATCH

NO MATCH

 Print the full path to your system Config.pm perl module

root@slacker:~# perl -MCPAN -le 'print $INC{"Config.pm"}'

 /usr/lib/perl5/5.8.8/i486-linux-thread-multi/Config.pm

Edit a files contents directly with search and replace

 root@slacker:~# more example.txt
DOG
CAT
DOG
CAT
DOG

root@slacker:~# perl -p -i -e 's/CAT/DOG/g' example.txt
root@slacker:~# more example.txt
DOG
DOG
DOG
DOG
DOG

 Check your current operating system

root@slacker:~# perl -e 'print $^O,"\n"'

 linux

That's all for now folks

 

Check back for future updates!

Image 

 

 
Next >