===== install modules perl Makefile.PL make make test ===== #!/usr/bin/perl -w # detect non matching braces $str = ; $c = 0; for (split //, $str) { $_ eq '{' && $c++; $_ eq '}' && $c--; $c lt 0 && die "Something's wrong (you closed a brace you never opened)\n"; }; print "$c (0 means o.k., positive value means you forgot $c closing braces)\n"; =================================================== perl -e 'while (1) {$r = int(rand() * 252) + 2; `fping -c 1 24.28.211.$r`;} tcpdump -n -i eth1 arp | perl -ne '@foo = split(/\s+/, $_); `fping -c 1 $foo[3]`;' perl -ne '$today=$_ if /Dec 28/;print $today if /DROP/' /var/log/kern.log mailq|perl -ne '$/="\n\n";$out=s/\n\n/\n/;print if /mystring/' ======================================== CPAN Modules Install perl -MCPAN -e 'install Chocolate::Belgian' ---------------------------------------- perl -MCGI::Carp -e 1 test if module installed ======================================== Perl includes perl -e 'print "@INC"' Include directories perl -MO=Deparse prog.pl Include module Deparse (debug mode) ---------------------------------------- Debug perl scripts perl -w test.pl 2>test.out splain < test.out ======================================== Perl Docs perldoc perlfaq FAQ perldoc perllocal show localy installed modules perldoc perlmodlib ======================================== tools pmtools -- a suite of small programs to help manage modules ======================================== Perl Use perl -mModule::Name ... ======================================== daily updates * 20 * * * /usr/local/bin/rsync -av --delete ftp.funet.fi::CPAN /project/CPAN/ >/dev/null 2>&1 ======================================== #!/usr/bin/env perl # #at the top of the script rather than: # ##!/usr/local/bin/perl # #This trick improves script portability because you don't have to edit #the script if you install a script on a system that has perl installed #somewhere other than /usr/local/bin. # #I'm pretty certain that someone has done something that has changed #the search path that exim passes on to subprocesses. Perhaps some #security patch or something. With the changed path, the script #interpreter cannot be located by env. ======================================== --------------------- calculator perl -l -e 'print 1024 * 1024' ======================================== #!/usr/bin/perl -w # detect non matching braces $str = ; $c = 0; for (split //, $str) { $_ eq '{' && $c++; $_ eq '}' && $c--; $c lt 0 && die "Something's wrong (you closed a brace you never opened)\n"; }; print "$c (0 means o.k., positive value means you forgot $c closing braces)\n"; ======================================== 1. perl -p -i -e 's/this/that/g' filename Search and replace the string 'this' with the string 'that' in the file it's best to avoid .*?[]{}$^ and other odd characters in regex. perl -i -pe 's!^#(START=yes)!$1!' /etc/default/saslauthd --------------------- replace one string with another perl -p -i -e 's/XFree86/x.org/g' file1.txt file2.txt 2. perl -e 'for (@ARGV) { rename $_, lc($_) unless -e lc($_); }' * Rename all the files in the current directory to lower case. The $_ is a sort of 'default' variable which is widely used in Perl. 3. perl -e 'for (@ARGV) { rename $_,$_.'l' unless -e lc($_); }' * Add an 'l' on the end of every filename in the current directory. That is: .htm => .html. The 'unless -e' statement means 'unless the filename exists'. 4. perl -MLWP::Simple -e 'mirror("http://www.perl.com/" , "perl.html")' Copy a file across the Web if it is newer than the local copy. You will need to install the "libnet" and "libwww" bundles to make this work. The LWP package comes with a great documentation page'lwpcook' which has lots of examples of other ways to transfer data across the WWW with Perl. 5. perl -p -i -e 's/' Convert Unix files to DOS files. Unix files have a different end of line character sequence from DOS. 6. perl -e 'if (!fork) {sleep $ARGV[0]*60; print "aaa" ; exit;} exit;' 1 Wait for the number of minutes specified and then beep. Note the use of the fork() to run in the background. Use the linux commnd 'ps' if you want to watch the command running. 7.perl -e 'use Socket; $name =gethostbyaddr(inet_aton($ARGV[0] ),AF_INET); print $name;' 207.153.253.38 Convert the given domain name or dotted IP number to a hostname. It will convert whatever you throw at it to a sensible and consistent hostname. 8. perl -MTime::Local -e '$y2k=timelocal(0,0,0,1,0,2000); $until=$y2k-time; print "seconds $until to y2k ";' This finds the number of seconds until the year 2000 is upon us. 9. perl -e '$n=utime ((stat($ARGV[0]))[8,9], @ARGV) ;print $n' aaa t* Make all files beginning with the letter t have the same time stamp as the file 'aaa' 10. perl -l -e 'open(F,"/usr/dict/english"); $w=join("",sort split(//,$ARGV[0])); print grep {chop;join("",sort split(//,$_)) eq $w} ;' life Find all anagrams of the word 'life'. --------------------- calculator perl -l -e 'print 1024 * 1024' --------------------- replace one string with another perl -p -i -e 's/XFree86/x.org/g' file1.txt file2.txt --------------------- operation on multiple files perl -p -i.old -e 's/oldstring/newstring/g' file* find . | xargs perl -p -i.old -e 's/oldstring/newstring/g' --------------------- extract all usernames from /etc/passwd perl -na -F: -e 'print "$F[0]\n"' < /etc/passwd --------------------- autosplit mode,array to the @F variable perl -naF 'next if /^#/; print "$F[3]\n"' --------------------- access_log.2005-1-1 -> access_log.old.2005-1-1 perl -e 'for(){$a = $_; s/log/log.old/; `cp $a $_`}' --------------------- Perl + ssh copy command and execute perl -e 'for("host1","host2","host3"){`scp command.pl $_:`; `ssh $_ command.pl`; }'