Perl One Liners

From Biowiki
Jump to: navigation, search

Yes, these are generally terrible hacks.

To find out what call chains are calling an oft-called sub the most, put a "use Carp; cluck;" in the sub, redirect STDERR to a file (say, "callchains"), and have at it with the following. It helps if there's not much other noise in the STDERR stream. Also, if there's some common sub name prefix that's just noise in your particular case, you can add another substitution to abbreviate it (e.g., add a "s/Bio::Graphics::Glyph/BGG/g;" before the print).

perl -e '$/ = "\n at"; $a = <>; while (<>) { chomp; s/^.*?\n//; s/^\s+(.*?)\(.*$/$1/mg; s/\n/ /g; print "$_\n"; }' callchains | sort | uniq -c

---

Generating some test JSON; more generally, demonstrating nested map:

$ perl -e 'print "[\n"; print join(",\n", map {$i = $_; join(", ", map {$_ + (10 * $i)} 1..10)} 0..2); print "\n]\n";'
[
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30
]
$ perl -e 'print "[\n"; print join(",\n", map {$i = $_; join(", ", map {$_ + (10 * $i)} 1..10)} 0..999); print "\n]\n";' > testJSON.js
$ wc testJSON.js
 1002 10002 58897 testJSON.js
$ head -3 testJSON.js
[
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
$ tail -3 testJSON.js
9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990,
9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999, 10000
]

-- Mitch Skinner - 06 Apr 2007