File: BeginLift.pm

package info (click to toggle)
libdevel-beginlift-perl 0.001003-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 156 kB
  • ctags: 101
  • sloc: perl: 1,092; makefile: 2
file content (140 lines) | stat: -rw-r--r-- 2,672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package Devel::BeginLift;

use strict;
use warnings;
use 5.008001;

our $VERSION = 0.001003;

use vars qw(%lift);
use base qw(DynaLoader);
use B::Hooks::OP::Check::EntersubForCV;

bootstrap Devel::BeginLift;

sub import {
  my ($class, @args) = @_;
  my $target = caller;
  $class->setup_for($target => \@args);
}

sub unimport {
  my ($class) = @_;
  my $target = caller;
  $class->teardown_for($target);
}

sub setup_for {
  my ($class, $target, $args) = @_;
  $lift{$target} ||= [];
  push @{ $lift{$target} }, map {
    $class->setup_for_cv($_);
  } map {
    ref $_ eq 'CODE'
      ? $_
      : \&{ "${target}::${_}" }
  } @{ $args };
}

sub teardown_for {
  my ($class, $target) = @_;
  $class->teardown_for_cv($_) for @{ $lift{$target} };
  delete $lift{$target};
}

=head1 NAME

Devel::BeginLift - make selected sub calls evaluate at compile time

=head1 SYNOPSIS

  use Devel::BeginLift qw(foo baz);
  
  use vars qw($i);
  
  BEGIN { $i = 0 }
  
  sub foo { "foo: $_[0]\n"; }
  
  sub bar { "bar: $_[0]\n"; }
  
  for (1 .. 3) {
    print foo($i++);
    print bar($i++);
  }
  
  no Devel::BeginLift;
  
  print foo($i++);

outputs -

foo: 0
bar: 1
foo: 0
bar: 2
foo: 0
bar: 3
foo: 4

=head1 DESCRIPTION

Devel::BeginLift 'lifts' arbitrary sub calls to running at compile time
- sort of a souped up version of "use constant". It does this via some
slightly insane perlguts magic.

=head2 import

  use Devel::BeginLift qw(list of subs);

Calls Devel::BeginLift->setup_for(__PACKAGE__ => \@list_of_subs);

=head2 unimport

  no Devel::BeginLift;

Calls Devel::BeginLift->teardown_for(__PACKAGE__);

=head2 setup_for

  Devel::BeginLift->setup_for($package => \@subnames);

Installs begin lifting magic (unless already installed) and registers
"${package}::$name" for each member of @subnames to be executed when parsed
and replaced with its output rather than left for runtime.

=head2 teardown_for

  Devel::BeginLift->teardown_for($package);

Deregisters all subs currently registered for $package and uninstalls begin
lifting magic is number of teardown_for calls matches number of setup_for
calls.

=head2 setup_for_cv

  $id = Devel::BeginLift->setup_for_cv(\&code);

Same as C<setup_for>, but only registers begin lifting magic for one code
reference. Returns an id to be used in C<teardown_for_cv>.

=head2 teardown_for_cv

  Devel::BeginLift->teardown_for_cv($id);

Deregisters begin lifting magic referred to by C<$id>.

=head1 AUTHOR

Matt S Trout - <mst@shadowcatsystems.co.uk>

Company: http://www.shadowcatsystems.co.uk/
Blog: http://chainsawblues.vox.com/

=head1 LICENSE

This library is free software under the same terms as perl itself

=cut

1;