in reply to Best practice for handling subroutines that are conditionally loaded?

With a little help from my AI friend and after holding their hand a bit, I got this:

package Test::Utils::Dump::Load; use v5.10; use strict; use warnings; use Exporter 'import'; our @EXPORT = qw(d d0 d1 dd di); my $debug = $ENV{MYMOD_DEBUG_LEVEL}; sub d { _load_and_call('d', @_) if $debug } sub d0 { _load_and_call('d0', @_) if $debug } sub d1 { _load_and_call('d1', @_) if $debug } sub dd { _load_and_call('dd', @_) if $debug } sub di { _load_and_call('di', @_) if $debug } sub _load_and_call { my $sub_name = shift; state $imported = 0; if (!$imported) { require Test::Utils::Dump; Test::Utils::Dump->import(qw(d d0 d1 dd di)); $imported = 1; } no strict 'refs'; ## no critic &{"Test::Utils::Dump::$sub_name"}(@_); } 1;

So now in my module with debug code I can just add this one line:

use Test::Utils::Dump::Load;

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

Replies are listed 'Best First'.
Re^2: Best practice for handling subroutines that are conditionally loaded?
by SankoR (Prior) on Mar 08, 2024 at 16:55 UTC
    That code only proves that AI isn't quite there yet. :)

    This is the 5 minute mess I just came up with:

    package Log::Maybe { use strict; use warnings; use parent 'Exporter'; our %EXPORT_TAGS = ( all => [ our @EXPORT = qw[info debug debugf $ +LOG_LEVEL] ] ); # Don't do this... our $LOG_LEVEL = $ENV{LOG_MAYBE}; + # 0: off, 1: debug, 2: info use Data::Dump qw[]; use Carp; $Carp::CarpLevel = 1; sub info { return 2 unless @_; return unless $LOG_LEVEL >= 2; Carp::cluck join ' ', map { ref $_ ? Data::Dump::dump($_) : $_ + } @_; } sub debug { return 1 unless @_; return unless $LOG_LEVEL >= 1; Carp::cluck '[' . localtime . '] ' . join ' ', map { ref $_ ? +Data::Dump::dump($_) : $_ } @_; } sub debugf { return 1 unless @_; return unless $LOG_LEVEL >= 1; Carp::cluck '[' . localtime . '] ' . sprintf shift, map { ref +$_ ? Data::Dump::dump($_) : $_ } @_; } }; 1;
    Which is used like so:
    use strict; use warnings; use lib './lib'; use Log::Maybe; # uses $ENV var $LOG_LEVEL = debug; # enable/change log leve +l info('oh, yeah, baby!'); # prints only if log lev +el is info debug('hi'); # prints if log level is + debug or info debug( 'wow', \%ENV ); # dumps non-scalars debugf( 'name: %s, age: %d', 'Jack', 23 ); # dumpf takes a sprintf +form $LOG_LEVEL = 0; # disable logging at run +time debug('nothing is logged'); # what it says on the ti +n

    I was feeling clever so the debug function gives you a stack trace. info and debug can also be used to return values used by $LOG_LEVEL. This is simple enough but something like Log::Any would be a wise choice here.

    Edit: Added debugf as an example of things you could do.

      > > With a little help from my AI friend

      (Well honestly 🤮)

      I recently saw a good metaphor for AI code.

      Someone used AI to create fake images of Trump surrounded by "happy" black voters for campaign reasons.¹

      Alas one pic had fantasy letters on the base caps, on the other pic Trump was missing fingers on one hand.

      Human perception might accept these, but computers won't correct fantasy code.

      Looking forward to the day the GOP nominates an AI for president, which nukes countries based on statistical grounds because of ... covfefe?

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

      ¹) https://www.bbc.com/news/world-us-canada-68440150

        I'll send my comments to my friend and see what he can come up with. :)

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

Re^2: Best practice for handling subroutines that are conditionally loaded?
by LanX (Saint) on Mar 08, 2024 at 17:07 UTC
    This looks overly convoluted to me.

    Just write an import (Don't necessarily need Exporter for that) routine which conditionally decides what to export at compile time.

    If you don't want to wire the ENV flag into the original module, just pass it's state as a flag while use -ing.

    I'm reluctant to provide code since your requirements are "drifting". (And you're friends with better qualified AI anyway)

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