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]); }; } };

perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

Replies are listed 'Best First'.
Re^3: Dollar Bracket Star ${*...}
by choroba (Cardinal) on Jan 11, 2022 at 17:29 UTC
    $a is a special variable. Don't use it for anything else.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Whooops. Good catch, thanks!

      perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'