in reply to Dollar Bracket Star ${*...}

There's a lot going on there, but this might be helpful, Chapter 8. Symbol Tables and Typeglobs (from Mastering Perl by bdf).

If you've ever seen the construct of locally mocking a subroutine, it's the same concept:
# code not tested sub test_this { my $stuff = call_to_external_service(); return $stuff; } #... # subroutine mock for a unit test, e.g., { no warnings qw/redefine/; my $expected = { some => 'expected result'}; local *call_to_external_service = sub { return $expected, }; # now call a test or something that calls is_deeply test_this(), $expected, qw{whatever...}; }
The * on the LHS is like a wild card to match the "type" on the RHS. Another term related to this is monkey patching.

Replies are listed 'Best First'.
Re^2: Dollar Bracket Star ${*...}
by cavac (Prior) on Jan 11, 2022 at 12:56 UTC

    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";'
      $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";'