Skip to content

Instantly share code, notes, and snippets.

@run4flat
Created December 13, 2016 14:33
Show Gist options
  • Save run4flat/a0ba85a067b676f29967e18dca0fa6b5 to your computer and use it in GitHub Desktop.
Save run4flat/a0ba85a067b676f29967e18dca0fa6b5 to your computer and use it in GitHub Desktop.
Use C::Blocks to create a keyword hoook and investigate hints hash scoping at the ends of blocks.
use strict;
use warnings;
use C::Blocks;
use C::Blocks::PerlAPI;
# Write a little keyword hook that simply prints the keywords it encounters
clex {
int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
int prev_count;
int my_keyword_plugin(pTHX_ char *keyword_ptr, STRLEN keyword_len, OP **op_ptr) {
printf("%s\t", keyword_ptr);
PL_hints |= HINT_LOCALIZE_HH;
gv_HVadd(PL_hintgv);
// get the current value of the counter, with modifier flag set
SV ** counter_p = hv_fetchs(GvHV(PL_hintgv), "counter", 1);
if (!counter_p) {
printf("hints-hash fetch failed!\n");
}
else {
SV * counter = *counter_p;
sv_inc(counter);
int curr_count = SvIV(counter);
if (curr_count != prev_count + 1) {
printf(" *** just changed count -> ");
}
prev_count = curr_count;
printf("%d potential keywords\n", curr_count);
}
return next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
}
}
# Install the keyword hook
BEGIN {
cblock {
printf("Hooking parser\n");
next_keyword_plugin = PL_keyword_plugin;
PL_keyword_plugin = my_keyword_plugin;
prev_count = 0;
}
}
my $thing = 0;
for (1) {
print "\$_ = $_\n";
my $a;
my $b;
my $c;
}
;
printf "post-loop\n";
my $foo;
for (1) {
print "\$_ = $_\n";
my $a;
my $b;
my $c;
}
printf "post-loop (again)\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment