Joshua ben Jore


pragma - A pragma for controlling other user pragmas


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


All methods may be subclassed. Importing pragma with the single parameter '-base' will do the proper stuff so your class is now a pragma.

  package your_pragma;
  use pragma -base;

  # Woot!


Subclassed pragmas are stored in the hints hash with their package name as a prefix. This prevents pragmas from unintentionally stomping on each other.

  # sets 'your::pragma::foo = 42
  use your_prama foo => 42;


Assume you're using the myint pragma mentioned in perlpragma. For ease, that pragma is duplicated here. You'll see it sets the myint value to 1 when on and 0 when off.

    package myint;
    use strict;
    use warnings;
    sub import {
        $^H{myint} = 1;
    sub unimport {
        $^H{myint} = 0;

Other code might casually wish to dip into myint:

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

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

The above could have been written without the pragma module as:

    BEGIN { delete $^H{myint} }
    BEGIN { $^H{myint} = 42 }

    print $^H{myint};


use pragma PRAGMA => VALUE
pragma->import( PRAGMA => VALUE )
pragma->poke( PRAGMA => VALUE )

Sets PRAGMA's value to VALUE.

no pragma PRAGMA
pragma->unimport( PRAGMA )

Unsets PRAGMA.

pragma->peek( PRAGMA )

Returns the current value of PRAGMA.