added docs
[p5sagit/Devel-BeginLift.git] / lib / Devel / BeginLift.pm
1 package Devel::BeginLift;
2
3 use strict;
4 use warnings;
5 use 5.008001;
6
7 our $VERSION = 0.001000;
8
9 use vars qw(%lift);
10 use base qw(DynaLoader);
11
12 bootstrap Devel::BeginLift;
13
14 sub import {
15   my ($class, @args) = @_;
16   my $target = caller;
17   $class->setup_for($target => \@args);
18 }
19
20 sub unimport {
21   my ($class) = @_;
22   my $target = caller;
23   $class->teardown_for($target);
24 }
25
26 sub setup_for {
27   my ($class, $target, $args) = @_;
28   setup();
29   $lift{$target}{$_} = 1 for @$args;
30 }
31
32 sub teardown_for {
33   my ($class, $target) = @_;
34   delete $lift{$target};
35   teardown();
36 }
37
38 =head1 NAME
39
40 Devel::BeginLift - make selected sub calls evaluate at compile time
41
42 =head1 SYNOPSIS
43
44   use Devel::BeginLift qw(foo baz);
45   
46   use vars qw($i);
47   
48   BEGIN { $i = 0 }
49   
50   sub foo { "foo: $_[0]\n"; }
51   
52   sub bar { "bar: $_[0]\n"; }
53   
54   for (1 .. 3) {
55     print foo($i++);
56     print bar($i++);
57   }
58   
59   no Devel::BeginLift;
60   
61   print foo($i++);
62
63 outputs -
64
65 foo: 0
66 bar: 1
67 foo: 0
68 bar: 2
69 foo: 0
70 bar: 3
71 foo: 4
72
73 =head1 DESCRIPTION
74
75 Devel::BeginLift 'lifts' arbitrary sub calls to running at compile time
76 - sort of a souped up version of "use constant". It does this via some
77 slightly insane perlguts magic.
78
79 =head2 import
80
81   use Devel::BeginLift qw(list of subs);
82
83 Calls Devel::BeginLift->setup_for(__PACKAGE__ => \@list_of_subs);
84
85 =head2 unimport
86
87   no Devel::BeginLift;
88
89 Calls Devel::BeginLift->teardown_for(__PACKAGE__);
90
91 =head2 setup_for
92
93   Devel::BeginLift->setup_for($package => \@subnames);
94
95 Installs begin lifting magic (unless already installed) and registers
96 "${package}::$name" for each member of @subnames to be executed when parsed
97 and replaced with its output rather than left for runtime.
98
99 =head2 teardown_for
100
101   Devel::BeginLift->teardown_for($package);
102
103 Deregisters all subs currently registered for $package and uninstalls begin
104 lifting magic is number of teardown_for calls matches number of setup_for
105 calls.
106
107 =head1 AUTHOR
108
109 Matt S Trout - <mst@shadowcatsystems.co.uk>
110
111 Company: http://www.shadowcatsystems.co.uk/
112 Blog: http://chainsawblues.vox.com/
113
114 =head1 LICENSE
115
116 This library is free software under the same terms as perl itself
117
118 =cut
119
120 1;