1 package Devel::Declare::Context::Simple;
6 use B::Hooks::EndOfScope;
9 our $VERSION = '0.006012';
18 @{$self}{ qw(Declarator Offset WarningOnRedefined) } = @_;
24 return $self->{Offset}
29 $self->{Offset} += shift;
34 return $self->{Declarator}
37 sub warning_on_redefine {
39 return $self->{WarningOnRedefined}
44 my $decl = $self->declarator;
45 my $len = Devel::Declare::toke_scan_word($self->offset, 0);
46 confess "Couldn't find declarator '$decl'"
49 my $linestr = $self->get_linestr;
50 my $name = substr($linestr, $self->offset, $len);
51 confess "Expected declarator '$decl', got '${name}'"
52 unless $name eq $decl;
54 $self->inc_offset($len);
59 $self->inc_offset(Devel::Declare::toke_skipspace($self->offset));
64 my $line = Devel::Declare::get_linestr();
71 Devel::Declare::set_linestr($line);
77 if (my $len = Devel::Declare::toke_scan_word( $self->offset, 1 )) {
78 my $linestr = $self->get_linestr();
79 my $name = substr( $linestr, $self->offset, $len );
80 substr( $linestr, $self->offset, $len ) = '';
81 $self->set_linestr($linestr);
92 if (my $len = Devel::Declare::toke_scan_ident( $self->offset )) {
93 my $linestr = $self->get_linestr();
94 my $ident = substr( $linestr, $self->offset, $len );
95 substr( $linestr, $self->offset, $len ) = '';
96 $self->set_linestr($linestr);
108 my $linestr = $self->get_linestr();
109 if (substr($linestr, $self->offset, 1) eq '(') {
110 my $length = Devel::Declare::toke_scan_str($self->offset);
111 my $proto = Devel::Declare::get_lex_stuff();
112 Devel::Declare::clear_lex_stuff();
113 $linestr = $self->get_linestr();
115 substr($linestr, $self->offset,
116 defined($length) ? $length : length($linestr)) = '';
117 $self->set_linestr($linestr);
124 sub strip_names_and_args {
130 my $linestr = $self->get_linestr;
131 if (substr($linestr, $self->offset, 1) eq '(') {
132 # We had a leading paren, so we will now expect comma separated
134 substr($linestr, $self->offset, 1) = '';
135 $self->set_linestr($linestr);
138 # At this point we expect to have a comma-separated list of
139 # barewords with optional protos afterward, so loop until we
140 # run out of comma-separated values
143 my $thing = $self->strip_name;
144 # If there's no bareword here, bail
145 confess "failed to parse bareword. found ${linestr}"
146 unless defined $thing;
148 $linestr = $self->get_linestr;
149 if (substr($linestr, $self->offset, 1) eq '(') {
150 # This one had a proto, pull it out
151 push(@args, [ $thing, $self->strip_proto ]);
153 # This had no proto, so store it with an undef
154 push(@args, [ $thing, undef ]);
157 $linestr = $self->get_linestr;
159 if (substr($linestr, $self->offset, 1) eq ',') {
160 # We found a comma, strip it out and set things up for
162 substr($linestr, $self->offset, 1) = '';
163 $self->set_linestr($linestr);
166 # No comma, get outta here
171 # look for the final closing paren of the list
172 if (substr($linestr, $self->offset, 1) eq ')') {
173 substr($linestr, $self->offset, 1) = '';
174 $self->set_linestr($linestr);
178 # fail if it isn't there
179 confess "couldn't find closing paren for argument. found ${linestr}"
182 # No parens, so expect a single arg
183 my $thing = $self->strip_name;
184 # If there's no bareword here, bail
185 confess "failed to parse bareword. found ${linestr}"
186 unless defined $thing;
187 $linestr = $self->get_linestr;
188 if (substr($linestr, $self->offset, 1) eq '(') {
189 # This one had a proto, pull it out
190 push(@args, [ $thing, $self->strip_proto ]);
192 # This had no proto, so store it with an undef
193 push(@args, [ $thing, undef ]);
204 my $linestr = Devel::Declare::get_linestr;
207 if (substr($linestr, $self->offset, 1) eq ':') {
208 while (substr($linestr, $self->offset, 1) ne '{') {
209 if (substr($linestr, $self->offset, 1) eq ':') {
210 substr($linestr, $self->offset, 1) = '';
211 Devel::Declare::set_linestr($linestr);
217 $linestr = Devel::Declare::get_linestr();
219 if (my $len = Devel::Declare::toke_scan_word($self->offset, 0)) {
220 my $name = substr($linestr, $self->offset, $len);
221 substr($linestr, $self->offset, $len) = '';
222 Devel::Declare::set_linestr($linestr);
224 $attrs .= " ${name}";
226 if (substr($linestr, $self->offset, 1) eq '(') {
227 my $length = Devel::Declare::toke_scan_str($self->offset);
228 my $arg = Devel::Declare::get_lex_stuff();
229 Devel::Declare::clear_lex_stuff();
230 $linestr = Devel::Declare::get_linestr();
231 substr($linestr, $self->offset, $length) = '';
232 Devel::Declare::set_linestr($linestr);
234 $attrs .= "(${arg})";
239 $linestr = Devel::Declare::get_linestr();
246 sub get_curstash_name {
247 return Devel::Declare::get_curstash_name;
252 my $pack = $self->get_curstash_name;
253 Devel::Declare::shadow_sub( $pack . '::' . $self->declarator, $_[0] );
256 sub inject_if_block {
259 my $before = shift || '';
263 my $linestr = $self->get_linestr;
264 if (substr($linestr, $self->offset, 1) eq '{') {
265 substr($linestr, $self->offset + 1, 0) = $inject;
266 substr($linestr, $self->offset, 0) = $before;
267 $self->set_linestr($linestr);
273 sub scope_injector_call {
275 my $inject = shift || '';
276 return ' BEGIN { ' . ref($self) . "->inject_scope('${inject}') }; ";
283 my $linestr = Devel::Declare::get_linestr;
284 return unless defined $linestr;
285 my $offset = Devel::Declare::get_linestr_offset;
286 substr( $linestr, $offset, 0 ) = ';' . $inject;
287 Devel::Declare::set_linestr($linestr);