North America Canada ftp://enterprise.ic.gc.ca/pub/perl/CPAN/ California ftp://ftp.digital.com/pub/plan/perl/CPAN/ ftp://ftp.cdrom.com/pub/perl/CPAN/ Colorado ftp://ftp.cs.colorado.edu/pub/perl/CPAN/ Florida ftp://ftp.cis.ufl.edu/pub/perl/CPAN/ Illinois ftp://uiarchive.cso.uiuc.edu/pub/lang/perl/CPAN/ Massachusetts ftp://ftp.iguide.com/pub/mirrors/packages/perl/CPAN/ New York ftp://ftp.rge.com/pub/languages/perl/ North Carolina ftp://ftp.duke.edu/pub/perl/ Oklahoma ftp://ftp.ou.edu/mirrors/CPAN/ Oregon ftp://ftp.orst.edu/pub/packages/CPAN/ Texas ftp://ftp.sedl.org/pub/mirrors/CPAN/ ftp://ftp.metronet.com/pub/perl/ g Match globally, i.e., find all occurrences. i Do case-insensitive pattern matching. m Treat string as multiple lines. o Compile pattern only once. s Treat string as single line. x Extend your pattern's legibilty with whitespace and comments. \ Quote the next metacharacter ^ Match the beginning of the line . Match any character (except newline) $ Match the end of the line | Alternation () Grouping [] Character class * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times {n} Match exactly n times {n,} Match at least n times {n,m} Match at least n but not more than m times *? Match 0 or more times +? Match 1 or more times ?? Match 0 or 1 time {n}? Match exactly n times {n,}? Match at least n times {n,m}? Match at least n but not more than m times *? Match 0 or more times +? Match 1 or more times ?? Match 0 or 1 time {n}? Match exactly n times {n,}? Match at least n times {n,m}? Match at least n but not more than m times \t tab \n newline \r return \f form feed \v vertical tab, whatever that is \a alarm (bell) \e escape (think troff) \033 octal char (think of a PDP-11) \x1B hex char \c[ control char \l lowercase next char (think vi) \u uppercase next char (think vi) \L lowercase till \E (think vi) \U uppercase till \E (think vi) \E end case modification (think vi) \Q quote regexp metacharacters till \E \w Match a "word" character (alphanumeric plus "_") \W Match a non-word character \s Match a whitespace character \S Match a non-whitespace character \d Match a digit character \D Match a non-digit character \b Match a word boundary \B Match a non-(word boundary) \A Match only at beginning of string \Z Match only at end of string \G Match only where previous m//g left off *****How to see the module man pages % pod2text name_of_module.pm % pod2man name_of_module.pm | nroff -man | more *****How to redirect a page #!/usr/local/bin/perl . . . print "Content-type: text/plain\n" print "Location: http://some.machine/some.doc\n\n""; *****How to find a blank-line if (/^\s*$/) # i.e. line consists of a number (possibly 0) space-type chars or unless (/\S/) # i.e. line doesn't contain a non space-type char. *****How to determine wheter or not 2 character exists in $string if (index($string,'@') > -1 && index($string, '.') > -1) /^(?=.*?\@)(?=.*?\.)/ /^(?=.*?\@).*?\./ ########### traping malicious emails########### if ($email =~ /[;><&\*`\|]/) { # E-mail contains malicious characters } else { # E-mail address is okay } ####### Deletes spaces within a string ###### $var =~ s/^\s*(.*)/$1/; **** will delete leading spaces, tabs, etc. $var =~ s/^\s+//; **** tested RC ok s/^\s+|\s+$//g; **** tested RC ltrim(rtrim($var)) s/^\s+//; from E.F Frield Mastering regexp page 290 s/\s\s+/ /g; ***will delete spaces within text strip blank space from the beginning/end of a string? for ($string) { s/^\s+//; s/\s+$//; } ######## Match var into a text ########################### /(var|var2|var3)/ OR operator /var/ && /var2/ && /var3/ AND operator /\bvar\b/ if embededd /^(?=.*var)(?=.*var2)(?=.*var3)/s AND operator #### expression that will check for a valid number##### print "It's valid!\n" if ($n <= 10) && ($n >= 1); * if ($number == 0 && "$number" ne "0") ** { print "Not a number!\n"; } else { print "Number!\n"; } $is_numeric = ($pn != 0) || ($pn eq '0'); *** I stay with this perl -e '$var="123";print "not a number" if ($var =~ /\D/);' ####### Count digits or characters in a string ######## $cnt = tr/*/*/; *** count digits in $_ $cnt = $var =~ tr/*/*/; $string="-9 55 48 -2 23 -76 4 14 -44"; $cnt++ while $string =~ /-\d+/g; print "There are $count negative numbers in the string"; $string = "ThisXlineXhasXsomeXx'sXinXit": $count = ($string =~ tr/X//); print "There are $count X charcters in the string"; ####### replace cr/lf by / / ################### $_ =~ s/\s+/ /g; ####### splits strings into separates variables####### s/^([^ ]*) *([^ ]*)/$2 $1/; # swap first two words if (/Time: (..):(..):(..)/) { $hours = $1; $minutes = $2; $seconds = $3; } * split in 2 separates by : #!/bin/perl -w $email = "Subject : Subscribe"; $_ = $email; ($nothing, $subj ) = split /:/, $_,2; print "this is the subject:$subj","\n"; ##### for quoted a variable for free use as pattern#### $pattern =~ s/(\W)/\\$1/g; book camel 74 ###### separates the text contained between ()##### #!/bin/perl -w $_="this (is) an (example) of multiple parens"; while (m#\((.*?)\)#g ) ) { print "$1\n"; } ###### store the current directory########### $original_dir = `pwd`; #########convert dates from sat feb 12 to 1997/02/12############ my %mon = ( Jan => 1, Feb => 2, Mar => 3, Apr => 4 ); # etc.... $date = "Sat Feb 15 23:11:21 PST 1997"; $date =~ s/^....(\w{3})\s+(\d+)\s+(\d{1,2}):(\d\d):(\d\d)\s+(\w+)\s(\d{4})/ sprintf("%4d%02d%02d%02d%02d%02d", $7, $mon{$1}, $2, $3, $4, $5)/e; print "$date\n"; #######################Interactive stuff###################### from the command line >perl -e 'print ("Hello\n");' #######################Simple nslookup######################## open(NS, "echo ls $domain | nslookup |"); while () { print } #######################Ping using net::ping################### Type "perldoc Net::Ping". It'll describe a pinging module. Here's an example: > perl -le 'use Net::Ping;print pingecho($ARGV[0],10) ? "up" : "down";' www If you have a host named "www" this will report "up", else "down". ##############to take just the number in the begin############## perl -e '$_ = "881 /usr/spool/mmdf/lock/home";/([0-9]+)\s*\//;$VALUE = $1;print ($VALUE,"\n");' ###############take statments between <>######## file named in contains: perl -e '$_=`cat in`;/]*)>/;print ($1);' result: NAME="area 1" ROWS="10" COLS="10" ##################greedy and non greedy################## $s1 = $s2 = "I am very very cold"; $s1 =~ s/ve.*y //; # I am cold $s2 =~ s/ve.*?y //; # I am very cold #################grep with file############################ open (FILE,"input"); $pass_exist = grep /$pattern/ , ; exist = $pass_exist > 1 ################Perl debugger###################### $ perl -d ./test -s DB<1> s 'perl -w -d script' and then do a 'c' #################change a substring####################### If you want to modify part of a string, the simplest way is often to use substr() as an lvalue: substr($a, 0, 3) = "Tom"; Although those with a regexp kind of thought process will likely prefer $a =~ s/^.../Tom/; ########To make the first letter of each word upper case:##### $line =~ s/\b(\w)/\U$1/g; ############System calls############# system("cat /etc/termcap") == 0 or die "cat program failed!"; #################strips out all non numbers######## $foo = "I_LIKE_THE_NUMBER_9"; $foo =~ s/[^\d]+//g; #################How to mask date from OS Solaris 2.5############# >perl -e '$today = `date '+%y%m%d'`;print $today;' #################summation accurated for R.SC ################# @nums = ( 1, 2, 3, 4 ); ! $sum = 0; !$sum = 0; foreach $n (@nums) { !for (@nums) { $sum += $_ } $sum += $n; ! }