Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^8: Introspecting function signatures

by LanX (Sage)
on Mar 07, 2021 at 12:52 UTC ( #11129261=note: print w/replies, xml ) Need Help??

in reply to Re^7: Introspecting function signatures (Dependency Injection and Monkey Patching)
in thread Introspecting function signatures

I think this niche is mainly filled by conditional use/imports in dynamic languages (see if )

Monkey patching is only one other possibility here (that's replacing a sub/method at runtime), we also have eval and BEGIN blocks ... etc.

> In frustration, I searched all

that's "terminology injection" (aka die "brain overflow" ;-)

Like all these Java OO "design patterns" which are superfluous in Perl.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^9: Introspecting function signatures
by eyepopslikeamosquito (Bishop) on Mar 08, 2021 at 00:32 UTC

    that's "terminology injection" (aka die "brain overflow" ;-)
    Ha ha, I noticed a mind-boggling number of different ways to do this type of stuff in Perl. For example, chromatic refers here to a modernperlbooks article which gives the illustrative example below:

    At its core, dependency injection is a formalization of the design principle "Don't hard-code your dependencies." Consider the code:

    sub fetch { my ($self, $uri) = @_; my $ua = LWP::UserAgent->new; my $resp = $ua->get( $uri ); ... }

    That's not bad code by any means, but it's a little too specific and a little less generic due to the presence of the literal string LWP::UserAgent. That might be fine for your application, but that hard-coding introduces a coupling that can work against other uses of this code. Imagine testing this code, for example. While you could use Test::MockObject to intercept calls to LWP::UserAgent's constructor, that approach manipulates Perl symbol tables to work around a hard coded dependency.

    An alternate approach uses a touch of indirection to allow for greater genericity:

    use Moose; has 'ua', is => 'ro', default => sub { LWP::UserAgent->new }; sub fetch { my ($self, $uri) = @_; my $ua = $self->ua; my $resp = $ua->get( $uri ); ... }
    Now the choice of user agent is up to the code which constructs this object. If it provides a ua to the constructor, fetch() will use that. Otherwise, the default behavior is to use LWP::UserAgent as before.

    Lots of stuff on CPAN too:

    As usual, the hard part is knowing which is good and which is crap. Please let us know of other cool CPAN offerings I've overlooked.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129261]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-05-21 05:57 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.