in reply to Re: Dollar Bracket Star ${*...}
in thread Dollar Bracket Star ${*...}
Monkey patching can be quite useful in another way, too. It allows to generate lots of functions in a loop instead of writing them one-by-one. I sometimes use this in some wrapper modules, so i can for example log all database calls for debugging.
BEGIN { # Auto-magically generate a number of similar functions without ac +tually # writing them down one-by-one. This makes changes much easier, bu +t # you need perl wizardry level +10 to understand how it works... my @simpleFuncs = qw(commit rollback errstr); my @stdFuncs = qw(do quote pg_savepoint pg_rollback_to pg_release) +; for my $a (@simpleFuncs){ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitN +oStrict) *{__PACKAGE__ . "::$a"} = sub { $_[0]->checkDBH(); return $_[0 +]->{mdbh}->$a(); }; } for my $a (@stdFuncs){ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitN +oStrict) *{__PACKAGE__ . "::$a"} = sub { $_[0]->checkDBH(); if($_[0]->{ +isDebugging}) {print STDERR $_[0]->{modname}, " ", $_[1], "\n";}; ret +urn $_[0]->{mdbh}->$a($_[1]); }; } };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Dollar Bracket Star ${*...}
by choroba (Cardinal) on Jan 11, 2022 at 17:29 UTC | |
by cavac (Prior) on Jan 11, 2022 at 18:03 UTC |