[philiptellis] /bb|[^b]{2}/
Never stop Grokking


Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Friday, July 24, 2009

Avoid running END blocks in perl

To run perl code without executing any END blocks, put this at the end of your program:
   exec('true');
You'd put that in place of any exit() statement as well. I'll leave it to you to figure out return values. It's not that hard.

So, why did I need this?

I have this large program in perl, and it has a module that prints out a bunch of stats in the END block. This is all fine for the default use case, but today I needed to write another small program that does a bunch of auditing on this module - something like a unit test, but not exactly.

Anyway, this smaller program only needed the module to initialise, but not actually execute, and it doesn't require the stats at the end of the code to be printed either, so I needed to figure out how to run a program without executing the END block.

I looked up the perlmod doc, and it said that the only conditions under which END is not executed is if the process is replaced using exec or you're thrown out of the water by a signal. Voilà, no more END.

Sunday, October 05, 2003

Generating random names

Had to generate a lot of names and emails for a project I'm working on.
Aborts, Weidman weidman@shock.net
Abreast, Periclean periclean@tier.org
Adequately, Misconstrues misconstrues@quartets.net
Antibody, Dodged dodged@sparking.org
Arteries, Shiver shiver@perpetual.net
Attired, Cindy cindy@became.net
Awaiting, Gillette gillette@crimson.com
Babying, Billed billed@arithmetical.net
Bacilli, Versailles versailles@mervin.net
Belabors, Humoring humoring@available.org
Benedikt, Microarchitecture microarchitecture@cezannes.com
Bites, Transposition transposition@husker.com
Botanical, Caboose caboose@uselessness.net
Brimful, Trickiest trickiest@mcgowan.org
Bunters, Prosper prosper@wacke.org
Buttressing, Revolutionize revolutionize@pandas.com
Cashew, Crave crave@avalanches.org
Chagrin, Debussy debussy@pistol.net
Commitments, Incapacitate incapacitate@westinghouse.org
Concentration, Greetings greetings@subspace.org
Counterexamples, Screwing screwing@bellicosity.com
Counters, Bryce bryce@simplicity.net
Cowered, Coasting coasting@accessing.org
Cultivate, Sophias sophias@sherwin.com
Demanded, Prediction prediction@transplanted.com
Dent, Subprocesses subprocesses@international.com
Dinghy, Rickets rickets@lexicographic.org
Dostoevsky, Fritz fritz@shoveled.org
Doughnuts, Arbitrariness arbitrariness@noon.com
Drip, Flatly flatly@subsegment.com
Enid, Bunter bunter@propagandist.net
Envious, Belonging belonging@nests.com
Epaulet, Wilder wilder@timers.net
Europeanizations, Crasher crasher@hews.com
Evokes, Confusions confusions@clap.com
Excusable, Filming filming@colonist.net
Fascinates, Gantry gantry@frisk.net
Fates, Regressions regressions@abides.net
Fiddle, Holiness holiness@unwrap.net
Fifteen, Francois francois@pithing.org
Finishers, Assenter assenter@prospered.org
Fleischman, Cocktail cocktail@perishable.net
Formants, Import import@flexibly.net
Freakish, Predominantly predominantly@checks.net
Glen, Obese obese@midrange.net
Greeted, Kneecap kneecap@fancifully.net
Grievance, Yellow yellow@loy.org
Guam, Herring herring@cobblestone.com
Hepburn, Felix felix@limitations.net
Highly, Pounced pounced@chirps.org
Homicide, Mute mute@vertical.net
Imagery, Athenians athenians@comforts.com
Incites, Dusk dusk@infighting.net
Industrialized, Viscous viscous@hoarsely.com
Input, Scale scale@jolla.org
Intellectuals, Raritan raritan@five.org
Joule, Farmyard farmyard@boisterously.com
Langford, Canberra canberra@forbearance.org
Leadership, Vibrate vibrate@rockaway.com
Librarian, Generic generic@hoff.com
Loopholes, Sequester sequester@fairchild.com
Makable, Avis avis@sprinkled.net
Marinade, Prominently prominently@stumps.com
Massachusetts, Angriest angriest@formatively.com
Milkiness, Marxism marxism@modulated.net
Mississippians, Teresa teresa@hike.net
Oligocene, Consenting consenting@ivy.com
Pavilion, Alertedly alertedly@anglo.org
Popish, Cicada cicada@very.org
Possessor, Hawks hawks@currently.org
Prepositions, Believers believers@condemnations.com
Pressings, Toad toad@sheriff.net
Refilled, Coarseness coarseness@poster.com
Ruined, Charity charity@qualifications.net
Rumpled, Blabbermouths blabbermouths@averaged.com
Scots, Gregory gregory@realest.org
Skillfulness, Entrenched entrenched@disillusionment.org
Slower, Mcmullen mcmullen@desirous.com
Snakelike, Arisen arisen@wakeup.org
Solids, Exertion exertion@encoded.com
Squabbles, Bridge bridge@piloting.com
Squinting, Catholicism catholicism@circumspect.net
Standards, Breakdown breakdown@bullseye.net
Starting, Unconditional unconditional@rooting.org
Stratify, Repudiate repudiate@assailant.net
Stupendous, Cochran cochran@wagner.net
Subgraph, Unfamiliar unfamiliar@hanoverian.com
Surpasses, Introspect introspect@sealed.com
Swallowed, Restlessly restlessly@padlock.net
Swallowing, Solidified solidified@unemployment.com
Taster, Calcutta calcutta@attractor.com
Thames, Limiter limiter@arterial.org
Tiresomely, Achiever achiever@glimpse.org
Tracers, Charlie charlie@busy.net
Unravels, Voided voided@bituminous.net
Unties, Conveyances conveyances@outbreak.net
Urinates, Balkiness balkiness@conveyances.net
Waltzes, Coulomb coulomb@quasar.net
Wiping, Foresee foresee@championships.com


I used /usr/share/dict/words as my wordlist - over 45000 words.
my @words = <>;
die "No wordlist" unless @words;
my $MAX = @words;

$dbh = DBI->connect("dbi:mysql:$db_name;host=$db_host", $db_user, $db_pass,
  { RaiseError => 1, AutoCommit => 0 })
 or die $DBI::errstr unless $dbh;

my $sth=$dbh->prepare(qq{INSERT INTO account (name, email) VALUES (?, ?)});
for my $i (1..100) {
 my $fname = $words[rand($MAX)];
 my $lname = $words[rand($MAX)];
 my $domain = $words[rand($MAX)];

 $fname =~ s/(\w+)/\u\L$1/;
 $lname =~ s/(\w+)/\u\L$1/;

 chomp($lname, $fname, $domain);
 my $email = lc "$fname\@$domain." . (qw(com net org))[rand 3];

 $sth->execute("$lname, $fname", $email);
}
The program took 0.1036 seconds to execute.

...===...