- Sexy, Useful Subs (subs must work under use strict)
All these have been benchmarked against many other alternatives. These are the fastest.
- sub is_anagram{join(0,sort pop=~/./gs)eq join 0,sort pop=~/./gs}
- sub array_freq{my %h; $h{$_}++ for @_; %h}
- sub max{((sort@_)[$#_])} # note: on a large @_ you may want to use a schwartzian transform
- sub min{((sort@_)[0])}
- Patterns
- perl -e'$}=1|1<<pop;print($"x$},(map$_?$=:$"x2,@;),$/),@;=(1,map$;[$_]^$;[-$_],1..@;)while$}--' 4
# hint: try 4,5,6...
- Network Stuff
- perl -e'map{$_=`ping -c 1 -w 1 192.168.1.$_`;print if /[^0] packets re/}1..254' #ping ip range, print on success
- Generating 'Random' Sequences
- perl -e"map{print+(H,T)[rand 2]}a..gr" #flip coin x 200
- Files
- perl -i.bak -e -p 's/\r\n/\n/' myfile.pl # thanks Hopkins26!
- perl -e'/(.*)\.TXT$/, rename($_, "$1.txt") || die $! for <*.TXT>' # rename all .TXT files to .txt
- perl -e'printf("%20s: %5d\n",($_,`cat $_ | wc -l`)) for(<./*>)' #how many lines in these files?
- perl -n -le'/\s(\S+)$/, $h{int($1)}++; END{printf("%3d:%s %d\n",($_,"#"x sqrt($h{$_}),$h{$_})) for (reverse sort {$a<=>$b} keys %h)}' filename #print dist based on #s by regex
- perl -le'$o = $_, s/\b(\w)(\w+)/\U\1\L\2/g, s/Mp3$/mp3/g, print `mv "$o" "$_"` for <*mp3>' # capitalize words
- Filter
- perl -ne'/(?!.*(.).*\1).{10}/&&print' <file #all lines with 10 consecutive unique chars
- Incestuous
- perl -le'map{-d && m!/([A-Z].+)$! && print $1 for<$_/*>}@INC' #print out list of available packages
- perl -n -e'chop; if(/(#[^\$;\}\{\/\\]*)$/){print STDERR "$1 ?"; s/\s*$1// if(<STDIN>=~/y/i)}print $_,$/' comments.pl > no_comments.pl #strips out comments with a prompt... by no means fool-proof
- General
- perl -e'printf "%5d:%s\t%s",$_,chr,($_%8<1?$/:"")for 25..127' #printable lower ASCII table
- perl -MLWP::Simple -e'getprint "http://parseerror.com/p"' |more
- Email
- perl -e '`echo "wazaaaaaa" |mail -s "Important Documents" coworker@job.com` for (1..100);'
- Completely Useless
- perl -MFile::Find -le'1 while find(sub{print $File::Find::name, sleep rand}, "/")'
- perl -le'1 while print "*" x rand(65)' #make it look like your comp is busy ;)
- perl -e"print '.:|' while 1 " #pretty
- perl -e'print chr(7) while 1' #annoying!
- perl -e'fork while 1' #do not run!