develooper Front page | perl.perl5.porters | Postings from March 2006

A user pragma example

Thread Next
Rafael Garcia-Suarez
March 31, 2006 14:18
A user pragma example
Message ID:
Thanks to Nicholas' work, one can now write user pragmas in perl.
Here's a sample code, for reference :

package mypragma;

use strict;
use warnings;

sub import {
    $^H{mypragma} = 1;

sub unimport {
    $^H{mypragma} = 0;

sub in_effect {
    my $hinthash = (caller(0))[10];
    return $hinthash->{mypragma};


And then, it can simply be used like this :

#!perl -l

use strict;
use mypragma (); # don't enable this pragma yet

print mypragma::in_effect();
    use mypragma;
    print mypragma::in_effect();
print mypragma::in_effect();

Of course, in place of this simplistic in_effect() function, you'll
prefer a function that
    does_some_action() if $hinthash->{mypragma}.

Also, currently in blead, that won't work in eval(""). Situation to be
repaired soon.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About