Do you have some kind of resilence if STDLOG is not set up from the using script?

As I said, STDERR (via Carp croak() and cluck() ) mostly serves my purposes, but for sake of demonstration:

Given Logndebug.pm:

package Logndebug; use strict; use warnings; use Exporter; our @ISA = qw[ Exporter ]; our @EXPORT = qw[ LOG DEBUG logging ]; sub LOG { return unless fileno( *STDLOG ); print STDLOG @_; } sub DEBUG { return unless fileno( *STDDBG ); print STDDBG @_; } sub logging { my( $logging, $debugging ) = @_; *STDLOG = *{ $logging } if defined $logging and fileno( $loggi +ng ); *STDDBG = *{ $debugging } if defined $debugging and fileno( $debug +ging ); } 1;

And SillyVec3D.pm:

package SillyVec3D; use strict; use warnings; use Logndebug; sub new { my $class = shift; my $self = bless { @_ }, $class; for my $attr ( keys %{ $self } ) { no strict 'refs'; eval qq[ *{ $attr } = sub { LOG( "$attr called with [\@_]" ); my \$self = shift; DEBUG( "$attr set to: ", \$self->{ $attr } = shift ) i +f \@_; \$self->{ $attr }; } ]; } return $self; } 1;

And t-SillyVec3D.pl:

#! perl -slw use strict; use SillyVec3D; our( $LOG, $DBG ); SillyVec3D::logging( $LOG ? *STDERR : undef, $DBG ? *STDERR : undef, ); my $o = SillyVec3D->new( X => 1, Y => 2, Z => 3 ); print $o->X, $o->Y, $o->Z; $o->X( 4 ); $o->Y( 5 ); $o->Z( 6 ); print $o->X, $o->Y, $o->Z;

In use:

C:\test>t-SillyVec3D 123 456 C:\test>t-SillyVec3D -LOG X called with [SillyVec3D=HASH(0x32cf38)] Y called with [SillyVec3D=HASH(0x32cf38)] Z called with [SillyVec3D=HASH(0x32cf38)] 123 X called with [SillyVec3D=HASH(0x32cf38) 4] Y called with [SillyVec3D=HASH(0x32cf38) 5] Z called with [SillyVec3D=HASH(0x32cf38) 6] X called with [SillyVec3D=HASH(0x32cf38)] Y called with [SillyVec3D=HASH(0x32cf38)] Z called with [SillyVec3D=HASH(0x32cf38)] 456 C:\test>t-SillyVec3D -DBG 123 X set to: 4 Y set to: 5 Z set to: 6 456 C:\test>t-SillyVec3D -LOG -DBG X called with [SillyVec3D=HASH(0x8cf48)] Y called with [SillyVec3D=HASH(0x8cf48)] Z called with [SillyVec3D=HASH(0x8cf48)] 123 X called with [SillyVec3D=HASH(0x8cf48) 4] X set to: 4 Y called with [SillyVec3D=HASH(0x8cf48) 5] Y set to: 5 Z called with [SillyVec3D=HASH(0x8cf48) 6] Z set to: 6 X called with [SillyVec3D=HASH(0x8cf48)] Y called with [SillyVec3D=HASH(0x8cf48)] Z called with [SillyVec3D=HASH(0x8cf48)] 456

You get logging and tracing embedded in the package, with minimal impact when turned off, and controlled from the calling script that can send the output wherever it choses with a single call on a package by package basis.

And if you set logging/debug on by passing in tied filehandles, they can add whatever boilerplate you desire.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re^5: Global or not global by BrowserUk
in thread Global or not global by McA

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.