I will spare you with another round of the first half of this post and jump straight to the table of anti-patterns and alternatives in Perl. Check out alternatives.pl for detailed examples. These barely scratch the surface for a rich language like Perl, whose motto itself is TMTOWTDI. My Perl knowledge is still amateurish and these concentrate almost exclusively on the "Accessing memory << Calling a function << Forking a process" guideline. This might be useful for anybody switching from Shell to Perl. So I hope you'll have a lot more to contribute.
30th June, 2011: TimeRatio is the ratio of time taken by Alternative to that by Anti-pattern, as taken from two different trials, using Perl 5.8 on Solaris 10 (Sun T5120). I hope these time ratios will highlight better why especially some of the anti-patterns are to be avoided. My suggestion is to not take these numbers on face value.
| Anti-pattern | Alternative | TimeRatio1 | TimeRatio2 |
| awk | open, split, close | 0.78 | 0.79 |
| basename | s/.*\/// | 859.47 | 872.42 |
| cat | open, close | 28.48 | 28.72 |
| cd | chdir | 1588.34 | 1577.63 |
| cp | use File::Copy "cp" | 1.85 | 1.87 |
| chmod | chmod (Perl) | 335.75 | 343.30 |
| cut | split | 306.35 | 307.05 |
| date | localtime | 200.32 | 214.04 |
| dirname | s/\/^\/*$// | 325.41 | 324.56 |
| echo | 4008.82 | 4006.22 | |
| find | use File::Find | 0.55 | 0.55 |
| grep | open, grep (Perl), close | 2.56 | 2.57 |
| head | open, break, close | 42.12 | 42.60 |
| hostname | use Sys::Hostname | 10361.94 | 10451.25 |
| id | getpwnam | 43.38 | 43.28 |
| kill | kill (Perl) | 55.04 | 50.84 |
| ln -s | symlink | 1010.70 | 995.73 |
| ls | opendir, closedir | 32.87 | 24.17 |
| mkdir | mkdir (Perl) | 13.21 | 12.93 |
| mkdir -p | use File::Path | 9.85 | 9.87 |
| mv | use File::Copy "move" | 32.53 | 32.65 |
| ping | use Net::Ping | 1.04 | 0.65 |
| ps -elf | use Proc::ProcessTable | 3.84 | 3.84 |
| pwd | $ENV{'PWD'} | 3575.62 | 3742.53 |
| rm | unlink | 75.05 | 74.07 |
| rmdir | rmdir (Perl) | 20.13 | 19.88 |
| rm -r | use File::Path | 9.00 | 9.02 |
| sed | open, s/find/replace/, close | 25.54 | 25.76 |
| sleep | sleep (Perl) | 2825.82 | 2824.30 |
| sort | open, sort (Perl), close | 4.31 | 4.18 |
| tail | open, close | 2.71 | 2.69 |
| touch | open, close | 65.41 | 65.41 |
| umask | umask (Perl) | 7749.43 | 7742.40 |
| uname | use Config | 13662.56 | 13652.62 |
| uniq | open, unless seen, close | 2.33 | 2.35 |
| wc -l | open, close | 6.79 | 6.82 |
0 comments:
Post a Comment