Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I've considered this before, but never bothered to implement it. tie your array. Here's the idea: You have an array that something strange is happening to and you can't find where/when/why. You want to inspect it without altering your code with a bunch of print statements. Sure, there's the Perl debugger, and that's probably where you ought to turn first (and the main reason I haven't gotten around to giving this a try before). But today I'm going to try something different.

Tie an array to a class that provides debug info whenever the array is modified in some way. Often tied entities are discouraged as they create action at a distance; it's very difficult to look at the primary source code and see why some behavior is happening -- too easy to forget about the tied behavior. But in this case, that's exactly what we're after: Behavior that is mostly invisible to the primary script, but helpful to us in some way.

Start by using Tie::Array, and subclassing Tie::StdArray since it provides us with all the methods that emulate a plain old array. Then just override the ones we care about. It turns out we care about a lot of the methods (any that alter the array). But the source for Tie::Array (under the Tie::StdArray section) is a good starting point for maintaining default Array behavior while providing hooks for additional functionality.

Note that with the "use parent" pragma, we have to say '-norequire', since Tie::StdArray is part of Tie::Array, and has already been loaded. We could just manipulate @ISA directly but that's not very Modern Perlish.

I used Carp because its cluck() function is perfectly verbose. And I created an object method called $self->debug() that is a setter and getter for debug status. With debug set to '1' (or any true value) we get a noisy tied array. Set to zero, the array becomes silent again. Here's one fun aspect of a tied entity; we get the side-effect behavior (the primary objective of the tie is to create side-effects in our variables), and we also get the ability to control our variable's behavior via object oriented interface. If our tied variable is @array, and our tie returns an object to $o, then $o->debug(1) will turn on debugging verbosity for @array.

What's it all for? Tie @ARGV in the beginning of your script (be sure to save its content and restore it after the tie). Then watch what other parts of your script do to @ARGV. Put the tie in a BEGIN{} block, and put that block before any 'use Module;' statements that may involve tinkering with @ARGV.

Here's some somewhat messy code that demonstrates what I'm talking about:

package TestArray; use Tie::Array; use parent -norequire, 'Tie::StdArray'; use strict; use warnings; use Carp qw/cluck/; $| = 1; our %STASH; sub TIEARRAY { my $o = bless [], $_[0]; $STASH{$o} = { DEBUG => 0}; return $o; } sub DESTROY { my $o = shift; if( $STASH{$o}{DEBUG} ) { cluck "Called DESTROY."; } delete $STASH{$o}; } sub debug { my $self = shift; my $newstate = shift; if( defined $newstate ) { $STASH{$self}{DEBUG} = $newstate; } return $STASH{$self}{DEBUG}; } sub STORE { if( $STASH{$_[0]}{DEBUG} ) { cluck "Called STORE on element [$_[1]] with value '$_[2]'"; } $_[0]->[$_[1]] = $_[2] } sub POP { if( $STASH{$_[0]}{DEBUG} ) { cluck "Called POP: Returned '$_[0][-1]'."; } pop( @{$_[0]} ); } sub SHIFT { if( $STASH{$_[0]}{DEBUG} ) { cluck "Called SHIFT: Returned '$_[0][0]'."; } shift( @{$_[0]} ); } sub PUSH { local $" = "', '"; my $o = shift; if( $STASH{$o}{DEBUG} ) { cluck "Called PUSH with args '@_'"; } push( @$o, @_ ); } sub UNSHIFT { local $" = "', '"; my $o = shift; if( $STASH{$o}{DEBUG} ) { cluck "Called UNSHIFT with args '@_'"; } unshift( @$o, @_ ); } sub CLEAR { if( $STASH{$_[0]}{DEBUG} ) { cluck "Called CLEAR."; } @{$_[0]} = (); } sub DELETE { if( $STASH{$_[0]}{DEBUG} ) { cluck "Called DELETE with arg $_[1]"; } delete $_[0]->[$_[1]]; } package main; use strict; use warnings; use v5.12; our $o; # Tied array object. BEGIN { @ARGV = ( 'A' .. 'Z' ); my @temp = @ARGV; $o = tie @ARGV, 'TestArray'; @ARGV = @temp; $o->debug(1); } say "@ARGV"; say "pop() test."; my $test = pop( @ARGV ); say "push() test."; push @ARGV, 'ZZ'; say "shift() test."; $test = shift( @ARGV ); say "unshift() test."; unshift( @ARGV, $test ); say "delete() test."; delete( $ARGV[-1] ); say "Assignment test."; $ARGV[0] = 'AA'; say "CLEAR() test."; @ARGV = ();

If you read the code you will also see that I'm creating a sort of inside-out object in tandom with the primary blessed array-ref. This is because Tie::Array and Tie::StdArray tie our array to an array-ref, which makes it simple to implement a tied array. But it doesn't help much if we need some name-space for additional storage per tied object. So by creating a hash called %STASH as a package global within the tie class module I create room for a namespace. I can create keys for the %STASH with names that are just the stringified version of the blessed object reference. And when the object is distroyed, I made sure to delete the key. There are probably better approaches nowadays, but it's been awhile since I played with such things, and that's how I remember doing it in the past.

Your output should be:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z pop() test. Called POP: Returned 'Z'. at mytest.pl line 43 TestArray::POP('TestArray=ARRAY(0x250e91c)') called at mytest.pl l +ine 110 push() test. Called PUSH with args 'ZZ' at mytest.pl line 59 TestArray::PUSH('TestArray=ARRAY(0x250e91c)', 'ZZ') called at myte +st.pl line 113 shift() test. Called SHIFT: Returned 'A'. at mytest.pl line 50 TestArray::SHIFT('TestArray=ARRAY(0x250e91c)') called at mytest.pl + line 116 unshift() test. Called UNSHIFT with args 'A' at mytest.pl line 68 TestArray::UNSHIFT('TestArray=ARRAY(0x250e91c)', 'A') called at my +test.pl line 119 delete() test. Called DELETE with arg 25 at mytest.pl line 82 TestArray::DELETE('TestArray=ARRAY(0x250e91c)', 25) called at myte +st.pl line 122 Assignment test. Called STORE on element [0] with value 'AA' at mytest.pl line 36 TestArray::STORE('TestArray=ARRAY(0x250e91c)', 0, 'AA') called at +mytest.pl line 125 CLEAR() test. Called CLEAR. at mytest.pl line 75 TestArray::CLEAR('TestArray=ARRAY(0x250e91c)') called at mytest.pl + line 128 Called DESTROY. at mytest.pl line 20 TestArray::DESTROY('TestArray=ARRAY(0x250e91c)') called at mytest. +pl line 0 eval {...} called at mytest.pl line 0

Now that you've seen this atrocity, be glad that Perl has a debugger. ;)


Dave


In reply to Re: debugging during compile by davido
in thread debugging during compile by pileofrogs

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-19 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found