hi,
In continuation with my old node :
removing debugging code at compile time ?
Today again :) i tried to figure a way to lower the number of the typed characters for doing Debugging, Logging and eventually error reporting.
So the question was how can i plug some debugging info into modules :
- which I can control from outside
- Is removed from the code if disabled i.e. in production I don't want this to take cpu cycles (pruned at compile time)
- is short to write i.e. as less keystrokes as possible.
To solve the dillema, my thought was that I have to use constants. The other tweak I decided to use sub(){} <=> constants, so I can add code
The problem was that to be able to mimic sub() like constant and vs. versa the prototype of the sub has to be sub(){...}
So far so good, but on the other hand I wanted to pass as many parameters as I want to this sub w/o getting the "Prototype mismatch error".
The solution was to call the sub like this &mysub($p1,$p2,..), so that it does not check the prototype
And here is my final solution Slicer module
package Slicer;
use base Exporter;
use Data::Dumper;
our @EXPORT = qw(slice unslice);
sub slice() {
my @caller = caller;
print $caller[0];
print ":: sliced :" , @_;
}
sub unslice {
for my $m (@_) {
eval "sub $m\::slice() {}";
}
}
1;
Think of slice() as anything you write i.e. debug, error,log, whatever ... (this one is just example for experiments)
Then I have my test module SomeModule.pm
package SomeModule;
use Slicer;
sub something {
#code....
&slice("let me say something\n");
}
1;
and finally the test script to see if all this works
#!/usr/bin/perl
use Slicer;
#unslice 'SomeModule';
use strict;
use SomeModule;
#use constant slice => 0;
#sub slice() {} ;
sub mainsub {
my $x = 12;#more code....
&slice("mainsub\n");
}
slice && print "&& sliced\n";
print "if sliced\n" if slice;
&slice(debug => "main\n");
mainsub;
SomeModule::something;
slice;
As you can see I can use all different methods to trigger slicing:
if, &&, slice, &slice(params)
Then I can disable slicing in many ways too i.e. :
use constant, sub() {}, unslice(@ModulesNames)
(Slicing in a sense that i slice the code at this point, then in my slice function I will implement all debug,log,error logic. It could be 'fat' 'cause it is pruned when I disable it anyway. Or if needed I can implement separate sub/constants logto,debug,err and slice :) ).
The things that still bothers me :
- I dont like &slice(...) syntax very much. If I can will prefer "&slice param1 => 123,p2 => 223", but using & requires braces ;(
- I'm not sure does unslice(), has to be executed in BEGIN{} !! Is the code pruned if i call it this way ? Yes I see slice() is not executed, but does it really remove the call to it ?
Any more comments are welcome...
PS> Dont forget i define slice with prototype i.e. slice()
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.