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.


In reply to Re^2: Best practice for handling subroutines that are conditionally loaded? by SankoR
in thread Best practice for handling subroutines that are conditionally loaded? by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.