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

A user pragma example

Thread Next
From:
Rafael Garcia-Suarez
Date:
March 31, 2006 14:18
Subject:
A user pragma example
Message ID:
b77c1dce0603311417t2e77bb65ja5a93f5e5e269582@mail.gmail.com
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};
}

1;

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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About