| 1 | package here; | ||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use Filter::Util::Call qw(filter_add filter_del); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | # fear not a filter that filters not not a filter be | ||||||||||
| 6 | 3 | our $DEBUG; | |||||||||
| 7 | |||||||||||
| 8 | 24 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=2 SCAR=7.0
| |||||||||||
| 9 | 24 | shift; | |||||||||
| 10 | 24 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 11 | 20 | my $code = join ';' => map {(my $x = $_) =~ s/;\s*$//; $x} @_; | |||||||||
| 12 | 20 | my (undef, $file, $line) = caller; | |||||||||
| 13 | 20 | ▶ | filter_add sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.9
| |||||||||||
| 14 | 20 | ▶ | if ($DEBUG) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 0 | (my $msg = $code) =~ s/\s+/ /g; | |||||||||
| 16 | 0 | warn "use here: $msg at $file line $line.\n"; | |||||||||
| 17 | } | ||||||||||
| 18 | 20 | $_ = "# line $line\n$code;\n# line $line\n\n"; | |||||||||
| 19 | 20 | filter_del; | |||||||||
| 20 | 20 | 1 | |||||||||
| 21 | } | ||||||||||
| 22 | 20 | } | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | sub croak { | ||||||||
|
Subroutine
croak: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 26 | 0 | s/\s+/ /g for my $msg = "@_"; | |||||||||
| 27 | 0 | my $i; | |||||||||
| 28 | 0 | ▶ | 1 while (caller ++$i) =~ /^here(::.+)?$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | my (undef, $file, $line) = caller $i; | |||||||||
| 30 | 0 | die "$msg at $file line $line.\n" | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 3 | my ($key, %data); | |||||||||
| 34 | 29 | ▶ | sub store { | ||||||||
|
Subroutine
store: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 35 | 29 | $data{++$key} = $_[0]; | |||||||||
| 36 | 29 | "here::fetch($key)" | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 29 | ▶ | sub fetch { | ||||||||
|
Subroutine
fetch: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||
| 40 | 29 | ▶ | if (exists $data{$_[0]}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 29 | delete $data{$_[0]} | |||||||||
| 42 | } | ||||||||||
| 43 | 0 | else {croak "here::fetch: invalid key '$_[0]'"} | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 3 | our $VERSION = '0.03'; | |||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | =head1 NAME | ||||||||||
| 50 | |||||||||||
| 51 | here - insert generated source here | ||||||||||
| 52 | |||||||||||
| 53 | =head1 VERSION | ||||||||||
| 54 | |||||||||||
| 55 | version 0.03 | ||||||||||
| 56 | |||||||||||
| 57 | =head1 SYNOPSIS | ||||||||||
| 58 | |||||||||||
| 59 | this module replaces a call to C< use here LIST; > with the contents of | ||||||||||
| 60 | C< LIST > at compile time. perl then compiles C< LIST > and the remaining code. | ||||||||||
| 61 | there is B<not> an implicit block around C< LIST > | ||||||||||
| 62 | |||||||||||
| 63 | an example is probably best: | ||||||||||
| 64 | |||||||||||
| 65 | my $x; | ||||||||||
| 66 | use here 'my $y'; | ||||||||||
| 67 | my $z; | ||||||||||
| 68 | |||||||||||
| 69 | is exactly equivalent to: | ||||||||||
| 70 | |||||||||||
| 71 | my $x; | ||||||||||
| 72 | my $y; | ||||||||||
| 73 | my $z; | ||||||||||
| 74 | |||||||||||
| 75 | the important thing here is that C< $y > is still in scope, which would not be | ||||||||||
| 76 | the case with a runtime C< eval >: | ||||||||||
| 77 | |||||||||||
| 78 | my $x; | ||||||||||
| 79 | eval 'my $y'; | ||||||||||
| 80 | my $z; # $y is not in scope here! | ||||||||||
| 81 | |||||||||||
| 82 | =head1 EXPORT | ||||||||||
| 83 | |||||||||||
| 84 | this module does not export anything, and must always be invoked at compile time | ||||||||||
| 85 | as: | ||||||||||
| 86 | |||||||||||
| 87 | use here LIST; | ||||||||||
| 88 | |||||||||||
| 89 | it is intended to be used with a transformation function to allow new syntactic | ||||||||||
| 90 | sugar: | ||||||||||
| 91 | |||||||||||
| 92 | sub my_0 {map {"my \$$_ = 0"} @_} | ||||||||||
| 93 | |||||||||||
| 94 | use here my_0 qw(x y z); | ||||||||||
| 95 | |||||||||||
| 96 | which results in perl compiling: | ||||||||||
| 97 | |||||||||||
| 98 | my $x = 0; my $y = 0; my $z = 0; | ||||||||||
| 99 | |||||||||||
| 100 | note the inserted semicolons (between every element of C<LIST> and at the end). | ||||||||||
| 101 | |||||||||||
| 102 | you can utilize the C< here::install > mechanism to make the code even shorter: | ||||||||||
| 103 | |||||||||||
| 104 | use here::install my_0 => sub {map {"my \$$_ = 0"} @_}; | ||||||||||
| 105 | |||||||||||
| 106 | use my_0 qw(x y z); | ||||||||||
| 107 | |||||||||||
| 108 | C< here::install > has dynamic lexical scope if L<B::Hooks::EndOfScope> is | ||||||||||
| 109 | available. otherwise it is global and you can call: | ||||||||||
| 110 | |||||||||||
| 111 | no here::install 'my_0'; | ||||||||||
| 112 | |||||||||||
| 113 | when you are done with the macro if you want to clean up. | ||||||||||
| 114 | |||||||||||
| 115 | =head1 SEE ALSO | ||||||||||
| 116 | |||||||||||
| 117 | see L<here::install> and L<here::declare> for additional examples. | ||||||||||
| 118 | |||||||||||
| 119 | see L<here::debug> to view what C<here> is doing. | ||||||||||
| 120 | |||||||||||
| 121 | =head1 AUTHOR | ||||||||||
| 122 | |||||||||||
| 123 | Eric Strom, C<< <asg at cpan.org> >> | ||||||||||
| 124 | |||||||||||
| 125 | =head1 BUGS | ||||||||||
| 126 | |||||||||||
| 127 | code following a C< use here ...; > line must be placed on a new line if that | ||||||||||
| 128 | code needs to be in the scope of the C< use here > | ||||||||||
| 129 | |||||||||||
| 130 | $first->() | ||||||||||
| 131 | use here '$second->()'; # comments are fine | ||||||||||
| 132 | $third->(); | ||||||||||
| 133 | |||||||||||
| 134 | $first->(); | ||||||||||
| 135 | use here '$third->()'; $second->(); # but this is out of order | ||||||||||
| 136 | $fourth->(); | ||||||||||
| 137 | |||||||||||
| 138 | use here 'my $x = 1'; # $x not in scope | ||||||||||
| 139 | # $x in scope | ||||||||||
| 140 | |||||||||||
| 141 | as far as i can tell, this is a limitation of perl /C< Filter::Util::Call > and | ||||||||||
| 142 | not of this module. patches welcome if this is not the case. | ||||||||||
| 143 | |||||||||||
| 144 | please don't fear that i've mentioned that this module uses | ||||||||||
| 145 | L<Filter::Util::Call>, since this module filters naught. all it does is insert | ||||||||||
| 146 | C< LIST > at the top of perl's queue of lines to compile. the filter is removed | ||||||||||
| 147 | at the same time, never to be called again. so fear not a filter that filters | ||||||||||
| 148 | not not a filter be. | ||||||||||
| 149 | |||||||||||
| 150 | write C< use here::debug; > before a C< use here LIST; > line to carp the | ||||||||||
| 151 | contents of C< LIST > when it is inserted into the source. | ||||||||||
| 152 | |||||||||||
| 153 | please report any bugs or feature requests to C<bug-here at rt.cpan.org>, or | ||||||||||
| 154 | through the web interface at | ||||||||||
| 155 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=here>. I will be notified, and | ||||||||||
| 156 | then you'll automatically be notified of progress on your bug as I make changes. | ||||||||||
| 157 | |||||||||||
| 158 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 159 | |||||||||||
| 160 | copyright 2011 Eric Strom. | ||||||||||
| 161 | |||||||||||
| 162 | this program is free software; you can redistribute it and/or modify it | ||||||||||
| 163 | under the terms of either: the GNU General Public License as published | ||||||||||
| 164 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 165 | |||||||||||
| 166 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 167 | |||||||||||
| 168 | =cut | ||||||||||
| 169 | |||||||||||
| 170 | 3 | 1 | |||||||||