Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Personally defined pragmas

by crabbdean (Pilgrim)
on Jun 07, 2004 at 10:26 UTC ( #361924=perlquestion: print w/ replies, xml ) Need Help??
crabbdean has asked for the wisdom of the Perl Monks concerning the following question:

I'm wondering if its possible to create a pragma just for my module? I've created a module in which every call to "push" is only performed if a verbose option is set.
push (@{$self->{output}}, ["chmod:\t$dir"]) if ($self->{verbose});

Is it possible to create this so its just like this ...
push (@{$self->{output}}, ["chmod:\t$dir"]);
... but only within my module?

Dean
The Funkster of Mirth
Programming these days takes more than a lone avenger with a compiler. - sam
RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

Comment on Personally defined pragmas
Select or Download Code
Re: Personally defined pragmas
by andyf (Pilgrim) on Jun 07, 2004 at 10:38 UTC
    If I understand correctly Crabbdean you want to add a state to your module, just one extra hash key that acts as a flag. You might turn this on and off in an instance with
    $thisthing->verboseon; some more code needing verbosity $thisthing->verboseoff;
    The push would be internally short circuited to a nop where the verbose flag is not on. Or am I missing your point?
Re: Personally defined pragmas
by borisz (Canon) on Jun 07, 2004 at 10:41 UTC
Re: Personally defined pragmas
by BrowserUk (Pope) on Jun 07, 2004 at 11:00 UTC

    How would your pragma know the difference between this line which should only happen when verbose is enabled

    push (@{$self->{output}}, ["chmod:\t$dir"]);

    and this line that should always happen?

    push (@{$self->{output}}, ["chmod:\t$dir"]);

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Personally defined pragmas
by BUU (Prior) on Jun 07, 2004 at 11:19 UTC
    A better solution would probably (Ignoring prebuilts like log4perl that solve this problem) to define your own "logging/error/warn" func that you can just turn off and on. Observe:
    sub my_warn_of_god(@) { if( $Main::Verbosity::Is::Double::PLus::Good ) { print @_; } } #stuff my_warn_of_god "Some stuff I want to see if debug"; #more stuff my_warn_of_god "Other stuff"; #yet more stuff
    This way you A) don't have to mess about with source filtering (which I don't trust..), and B) you don't have to do odd things like disabling push.
Re: Personally defined pragmas
by dragonchild (Archbishop) on Jun 07, 2004 at 11:37 UTC
    sub my_push { my $self = shift; return unless $self->{verbose}; push @{$self->{output}}, @_; } # Somewhere later ... $self->my_push( ["chmod:\t$dir"] );

    And, every call to push is converted into a call to my_push(). This is how OO is meant to be used.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://361924]
Approved by Paulster2
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-08-31 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls