Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Evaluation of my CPAN module

by tobyink (Canon)
on Aug 16, 2018 at 18:34 UTC ( [id://1220455]=note: print w/replies, xml ) Need Help??


in reply to Evaluation of my CPAN module

Makes a lot of use of caller. This makes it pretty hard for somebody else to wrap your code if they want an extra layer of abstraction.

A better way of doing it is something like this:

sub import { my $me = shift; my $opts = ref($_[0]) ? shift : { for => scalar caller }; my %wanted = map { $_ => 1 } @_; *{ $opts->{for} . "::logf" } = $me->_generate_logf($opts) if $wanted +{logf}; *{ $opts->{for} . "::logw" } = $me->_generate_logw($opts) if $wanted +{logw}; ...; } sub _generate_logf { shift; my $opts = shift; return sub { my $msg = shift; my $log = _get_logger($opts->{for}, shift); return unless $log->is_fatal; $log->fatal($msg); die "\n"; } } ...;

Exporters like Exporter::Tiny and Sub::Exporter support this kind of pattern — generating subs to export which have been specialized for whatever class is importing them — more easily than Exporter.pm. This technique will not only help avoid reliance on caller, making it easier for people to wrap your module, but will also help you further down the line when you are looking at making it more configurable.

Also, you didn't use strict or use warnings in your module. (And there's at least one part which would fail under use strict and currently could cause an issue under very contrived circumstances.)

Replies are listed 'Best First'.
Re^2: Evaluation of my CPAN module
by nysus (Parson) on Aug 16, 2018 at 18:55 UTC

    Awesome, thanks for the advice. Finding a better way to get the callers was on my list of things to improve but I hadn't figured out a good way to accomplish this. In fact, IIRC, I think the code breaks if there are nested calls. I will definitely study this and see if I can get your suggestion incorporated.

    I think I probably had Modern::Perl at some point which automatically inserts those pragmas. I must have accidentally deleted it. Thanks!

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (11)
As of 2024-04-18 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found