EnglishFrenchSpanish

OnWorks favicon

perlpragma - Online in the Cloud

Run perlpragma in OnWorks free hosting provider over Ubuntu Online, Fedora Online, Windows online emulator or MAC OS online emulator

This is the command perlpragma that can be run in the OnWorks free hosting provider using one of our multiple free online workstations such as Ubuntu Online, Fedora Online, Windows online emulator or MAC OS online emulator

PROGRAM:

NAME


perlpragma - how to write a user pragma

DESCRIPTION


A pragma is a module which influences some aspect of the compile time or run time
behaviour of Perl, such as "strict" or "warnings". With Perl 5.10 you are no longer
limited to the built in pragmata; you can now create user pragmata that modify the
behaviour of user functions within a lexical scope.

A basic example


For example, say you need to create a class implementing overloaded mathematical
operators, and would like to provide your own pragma that functions much like "use
integer;" You'd like this code

use MyMaths;

my $l = MyMaths->new(1.2);
my $r = MyMaths->new(3.4);

print "A: ", $l + $r, "\n";

use myint;
print "B: ", $l + $r, "\n";

{
no myint;
print "C: ", $l + $r, "\n";
}

print "D: ", $l + $r, "\n";

no myint;
print "E: ", $l + $r, "\n";

to give the output

A: 4.6
B: 4
C: 4.6
D: 4
E: 4.6

i.e., where "use myint;" is in effect, addition operations are forced to integer, whereas
by default they are not, with the default behaviour being restored via "no myint;"

The minimal implementation of the package "MyMaths" would be something like this:

package MyMaths;
use warnings;
use strict;
use myint();
use overload '+' => sub {
my ($l, $r) = @_;
# Pass 1 to check up one call level from here
if (myint::in_effect(1)) {
int($$l) + int($$r);
} else {
$$l + $$r;
}
};

sub new {
my ($class, $value) = @_;
bless \$value, $class;
}

1;

Note how we load the user pragma "myint" with an empty list "()" to prevent its "import"
being called.

The interaction with the Perl compilation happens inside package "myint":

package myint;

use strict;
use warnings;

sub import {
$^H{"myint/in_effect"} = 1;
}

sub unimport {
$^H{"myint/in_effect"} = 0;
}

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

1;

As pragmata are implemented as modules, like any other module, "use myint;" becomes

BEGIN {
require myint;
myint->import();
}

and "no myint;" is

BEGIN {
require myint;
myint->unimport();
}

Hence the "import" and "unimport" routines are called at compile time for the user's code.

User pragmata store their state by writing to the magical hash "%^H", hence these two
routines manipulate it. The state information in "%^H" is stored in the optree, and can be
retrieved read-only at runtime with "caller()", at index 10 of the list of returned
results. In the example pragma, retrieval is encapsulated into the routine "in_effect()",
which takes as parameter the number of call frames to go up to find the value of the
pragma in the user's script. This uses "caller()" to determine the value of
$^H{"myint/in_effect"} when each line of the user's script was called, and therefore
provide the correct semantics in the subroutine implementing the overloaded addition.

Key naming


There is only a single "%^H", but arbitrarily many modules that want to use its scoping
semantics. To avoid stepping on each other's toes, they need to be sure to use different
keys in the hash. It is therefore conventional for a module to use only keys that begin
with the module's name (the name of its main package) and a "/" character. After this
module-identifying prefix, the rest of the key is entirely up to the module: it may
include any characters whatsoever. For example, a module "Foo::Bar" should use keys such
as "Foo::Bar/baz" and "Foo::Bar/$%/_!". Modules following this convention all play nicely
with each other.

The Perl core uses a handful of keys in "%^H" which do not follow this convention, because
they predate it. Keys that follow the convention won't conflict with the core's
historical keys.

Implementation details


The optree is shared between threads. This means there is a possibility that the optree
will outlive the particular thread (and therefore the interpreter instance) that created
it, so true Perl scalars cannot be stored in the optree. Instead a compact form is used,
which can only store values that are integers (signed and unsigned), strings or "undef" -
references and floating point values are stringified. If you need to store multiple
values or complex structures, you should serialise them, for example with "pack". The
deletion of a hash key from "%^H" is recorded, and as ever can be distinguished from the
existence of a key with value "undef" with "exists".

Don't attempt to store references to data structures as integers which are retrieved via
"caller" and converted back, as this will not be threadsafe. Accesses would be to the
structure without locking (which is not safe for Perl's scalars), and either the structure
has to leak, or it has to be freed when its creating thread terminates, which may be
before the optree referencing it is deleted, if other threads outlive it.

Use perlpragma online using onworks.net services


Free Servers & Workstations

Download Windows & Linux apps

  • 1
    archlabs_repo
    archlabs_repo
    Package repo for ArchLabs This is an
    application that can also be fetched
    from
    https://sourceforge.net/projects/archlabs-repo/.
    It has been hosted in OnWorks in...
    Download archlabs_repo
  • 2
    Zephyr Project
    Zephyr Project
    The Zephyr Project is a new generation
    real-time operating system (RTOS) that
    supports multiple hardware
    architectures. It is based on a
    small-footprint kernel...
    Download Zephyr Project
  • 3
    SCons
    SCons
    SCons is a software construction tool
    that is a superior alternative to the
    classic "Make" build tool that
    we all know and love. SCons is
    implemented a...
    Download SCons
  • 4
    PSeInt
    PSeInt
    PSeInt is a pseudo-code interpreter for
    spanish-speaking programming students.
    Its main purpose is to be a tool for
    learning and understanding the basic
    concep...
    Download PSeInt
  • 5
    oStorybook
    oStorybook
    oStorybook l'outil privil�gi� des
    �crivains. ATTENTION : voir sur
    http://ostorybook.tuxfamily.org/v5/
    --en_EN oStorybook the right tool for
    writers. WARNIN...
    Download oStorybook
  • 6
    Asuswrt-Merlin
    Asuswrt-Merlin
    Asuswrt-Merlin is a third party
    firmware for select Asus wireless
    routers. Based on the Asuswrt firmware
    developed by Asus, it brings tweaks, new
    features and ...
    Download Asuswrt-Merlin
  • More »

Linux commands

  • 1
    aafigure
    aafigure
    aafigure - convert ASCII art to an
    image ...
    Run aafigure
  • 2
    aafire
    aafire
    aafire, aainfo, aasavefont, aatest -
    aalib example programs ...
    Run aafire
  • 3
    coqtop.opt
    coqtop.opt
    coqtop.opt - The native-code Coq
    toplevel ...
    Run coqtop.opt
  • 4
    coqwc
    coqwc
    coqwc - print the number of
    specification, proof and comment lines
    in Coq files ...
    Run coqwc
  • 5
    g15composer
    g15composer
    g15composer - Scriptable command
    interface to libg15render(3) drawing
    functions DESCRIPTION: G15composer is a
    scriptable command interface to the
    libg15render ...
    Run g15composer
  • 6
    g15daemon
    g15daemon
    g15daemon - provides access to extra
    keys and the LCD available on the
    logitech G15 keyboard. DESCRIPTION:
    G15Daemon allows users access to all
    extra keys by d...
    Run g15daemon
  • More »

Ad