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

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.

Replies are listed 'Best First'.
Re^3: Best practice for handling subroutines that are conditionally loaded? (AI generated code)
by LanX (Saint) on Mar 08, 2024 at 17:22 UTC
    > > 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

        Cool, in the meantime you're on my ignore list. :)

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