pragma.pm plays well with others

jjore on 2006-12-07T20:39:03

For kicks I wrote the pragma pragma which just mucks with other user pragmas. I'm not sure what good this is except that I wanted a simple API to poke at other people's pragmas. I might even upload this to CPAN just so it's handy.

package pragma;
use strict;
use warnings;

our $VERSION = ~0;

=head1 NAME

pragma - A pragma for controlling other user pragmas

=head1 DESCRIPTION

The C pragma is a module which influences other user pragmata such as L. With Perl 5.10 you can create user pragmata and the C pragma can modify and peek at other pragmata.

=head1 A basic example

Assume you're using the C pragma mentioned in L. For ease, that pragma is duplicated here.

package myint;

use strict; use warnings;

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

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

sub value { my $level = shift // 0; my $hinthash = (caller($level))[10]; return $hinthash->{myint}; }

1;

Other code might casually wish to dip into C:

no pragma 'myint'; # delete $^H{myint} use pragma myint => 42; # $^H{myint} = 42

print pragma->peek( 'myint' ); # prints '42'

=cut

=head1 CLASS METHODS

=over

=item C<< use pragma PRAGMA => VALUE >>

=item C<< pragma->import( PRAGMA => VALUE ) >>

=item C<< pragma->poke( PRAGMA => VALUE ) >>

Sets C's value to C.

=cut

sub import { my ( undef, $pragma, $value ) = @_;

$^H{$pragma} = $value; return; } *poke = \&import;

=item C<< no pragma PRAGMA >>

=item C<< pragma->unimport( PRAGMA ) >>

Unsets C.

=cut

sub unimport { my ( undef, $pragma ) = @_;

delete $^H{$pragma} if exists $^H{$pragma}; return; }

=item C<< pragma->peek( PRAGMA ) >>

Returns the current value of C.

=cut

sub peek { my ( undef, $pragma ) = @_;

return $^H{$pragma}; }

=back

=head1 SUBCLASSING

All methods may be subclassed.

=cut

q[And I don't think an entire stallion of horses, or a tank, could stop you two from getting married.];