| 1 | package Begin::Declare; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Devel::Declare; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 0 | ▶ | sub croak { | ||||||||||||||||||||||||||
|
Subroutine
croak: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 7 | 0 | s/\s+/ /g for my ($msg, $src) = @_; | |||||||||||||||||||||||||||
| 8 | 0 | ▶ | $src =~ s/^(.{20}).*/$1'.../ or $src .= q('); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 9 | 0 | my $i; | |||||||||||||||||||||||||||
| 10 | 0 | ▶ | 1 while (caller ++$i) =~ /^(?:Begin|Devel)::Declare/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 11 | 0 | my (undef, $file, $line) = caller $i; | |||||||||||||||||||||||||||
| 12 | 0 | die "Begin::Declare: $msg '$src at $file line $line.\n" | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | my @types = qw (my our); | |||||||||||||||||||||||||||
| 16 | 2 | ▶ | push @types, 'state' if $] >= 5.010; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 2 | my $type_re = join '|' => @types; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=7 SCAR=21.8
| |||||||||||||||||||||||||||||
| 20 | 2 | shift; | |||||||||||||||||||||||||||
| 21 | 2 | for (@_) { | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | /^($type_re)$/i or croak "not exported", $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | /^[A-Z]/ or croak "first character must be uc in", $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 2 | ▶ | @_ or @_ = map uc, grep {/my|our/ or $^H{feature_state}} @types; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/my|our/ || $^H{'feature_state'}
MC/DC50%1/2
/my|our/ || $^H{'feature_state'}
/my|our/
$^H{'feature_state'}
Truth table66%2/3
A || B
A= /my|our/B= $^H{'feature_state'}
| |||||||||||||||||||||||||||||
| 26 | 2 | my $caller = caller; | |||||||||||||||||||||||||||
| 27 | Devel::Declare->setup_for ( | ||||||||||||||||||||||||||||
| 28 | 2 | $caller => {map {$_ => {const => \&parser}} @_} | |||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | *{$caller.'::'.$_} = sub (@):lvalue {@_[0 .. $#_]} for @_ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | our $prefix = ''; | |||||||||||||||||||||||||||
| 35 | 140 | ▶ | sub get {substr Devel::Declare::get_linestr, length $prefix} | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 84 | ▶ | sub set { Devel::Declare::set_linestr $prefix . $_[0]} | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 14 | ▶ | sub parser { | ||||||||||||||||||||||||||
|
Subroutine
parser: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 14 | local $prefix = substr get, 0, $_[1]; | |||||||||||||||||||||||||||
| 40 | 14 | my $type = strip_type(); | |||||||||||||||||||||||||||
| 41 | 14 | my $vars = strip_vars(); | |||||||||||||||||||||||||||
| 42 | 14 | check_assign(); | |||||||||||||||||||||||||||
| 43 | 14 | set "$type $vars; use Begin::Declare::Lift $vars " . get | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 49 | ▶ | sub strip_space { | ||||||||||||||||||||||||||
|
Subroutine
strip_space: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 49 | my $skip = Devel::Declare::toke_skipspace length $prefix; | |||||||||||||||||||||||||||
| 48 | 49 | set substr get, $skip; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 14 | ▶ | sub strip_type { | ||||||||||||||||||||||||||
|
Subroutine
strip_type: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 14 | strip_space; | |||||||||||||||||||||||||||
| 53 | 14 | ▶ | get =~ /^($type_re)(?:\b|$)/i or croak "not /$type_re/i", get; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 14 | $prefix .= $1 . ' '; | |||||||||||||||||||||||||||
| 55 | 14 | lc $1 | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 14 | ▶ | sub strip_vars { | ||||||||||||||||||||||||||
|
Subroutine
strip_vars: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 14 | strip_space; | |||||||||||||||||||||||||||
| 60 | 14 | ▶ | strip_parens() or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 7 | ▶ | (my $line = get) =~ s/^([\$\%\@])// | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | or croak "not a valid sigil", get =~ /(.)/; | ||||||||||||||||||||||||||||
| 63 | 7 | my $sigil = $1; | |||||||||||||||||||||||||||
| 64 | 7 | set $line; | |||||||||||||||||||||||||||
| 65 | 7 | strip_space; | |||||||||||||||||||||||||||
| 66 | 7 | ▶ | ($line = get) =~ s/^(\w+)// | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | or croak "not a lexical variable name", $sigil.$line; | ||||||||||||||||||||||||||||
| 68 | 7 | set $line; | |||||||||||||||||||||||||||
| 69 | 7 | $sigil . $1 | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 14 | ▶ | sub strip_parens { | ||||||||||||||||||||||||||
|
Subroutine
strip_parens: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 14 | ▶ | if (get =~ /^\(/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 7 | my $length = Devel::Declare::toke_scan_str length $prefix; | |||||||||||||||||||||||||||
| 76 | 7 | my $parens = Devel::Declare::get_lex_stuff; | |||||||||||||||||||||||||||
| 77 | 7 | Devel::Declare::clear_lex_stuff; | |||||||||||||||||||||||||||
| 78 | 7 | set substr get, $length; | |||||||||||||||||||||||||||
| 79 | 7 | $parens =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 80 | 7 | return "($parens)" | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 14 | ▶ | sub check_assign { | ||||||||||||||||||||||||||
|
Subroutine
check_assign: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 14 | strip_space; | |||||||||||||||||||||||||||
| 86 | 14 | ▶ | /^=[^=]/ or croak "assignment '=' expected before", $_ for get | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | $INC{'Begin/Declare/Lift.pm'}++; | |||||||||||||||||||||||||||
| 90 | 14 | ▶ | sub Begin::Declare::Lift::import {} | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 NAME | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Begin::Declare - compile time lexical variables | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 VERSION | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | version 0.06 | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | don't you hate writing: | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | my ($foo, @bar); | ||||||||||||||||||||||||||||
| 108 | BEGIN { | ||||||||||||||||||||||||||||
| 109 | ($foo, @bar) = ('fooval', 1 .. 10); | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | when you should be able to write: | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | MY ($foo, @bar) = ('fooval', 1 .. 10); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | just C< use Begin::Declare; > and you can. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | use Begin::Declare; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | is the same as: | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | use Begin::Declare qw (MY OUR); # and STATE if "use feature 'state';" | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | you can also write: | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | use Begin::Declare qw (My Our); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | if you prefer those names. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 DECLARATIONS | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 MY ... = ...; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =over 4 | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | works just like the keyword C< my > except it lifts the assignment to compile | ||||||||||||||||||||||||||||
| 139 | time. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | MY $x = 1; # my $x; BEGIN {$x = 1} | ||||||||||||||||||||||||||||
| 142 | MY ($y, $z) = (2, 3); # my ($y, $z); BEGIN {($y, $z) = (2, 3)} | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =back | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head2 OUR ... = ...; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =over 4 | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | works just like the keyword C< our > except it lifts the assignment to compile | ||||||||||||||||||||||||||||
| 151 | time. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | OUR ($x, @xs) = 1 .. 10; # our ($x, @xs); BEGIN {($x, @xs) = 1 .. 10} | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 STATE ... = ...; | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =over 4 | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | works better than the keyword C< state > (since it supports list assignment | ||||||||||||||||||||||||||||
| 162 | and is a proper lvalue (at least on it's rhs)) and it lifts the assignment to | ||||||||||||||||||||||||||||
| 163 | compile time. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | STATE ($x, @xs) = 1 .. 10; # state ($x, @xs); BEGIN {($x, @xs) = 1 .. 10} | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | for (1 .. 5) { | ||||||||||||||||||||||||||||
| 168 | print ++STATE $x = 'a'; # bcdef | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =back | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Eric Strom, C<< <asg at cpan.org> >> | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 BUGS | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 180 | C<bug-begin-declare at rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||
| 181 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Begin-Declare>. I will be | ||||||||||||||||||||||||||||
| 182 | notified, and then you'll automatically be notified of progress on your bug as | ||||||||||||||||||||||||||||
| 183 | I make changes. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | the authors of L<Devel::Declare> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | copyright 2011 Eric Strom. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 194 | the terms of either: the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||
| 195 | Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | see http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 2 | 1 | |||||||||||||||||||||||||||