# man > B::Hooks::EndOfScope

<think>The user wants me to transform this Perl module documentation into Open Knowledge Format (OKF) Markdown. Let me analyze the content:

1. This is a Perl module documentation for `B::Hooks::EndOfScope`
2. It's not a traditional man page, but a perldoc-style module documentation
3. Key information:
   - Function: `on_scope_end` - executes code after scope compilation
   - Has limitations for pure-perl mode and older Perl versions
   - Version 0.25

Let me structure this according to the OKF format requirements:

- YAML frontmatter with appropriate fields
- Quick Reference section
- Name section
- Synopsis
- Functions (instead of Options for a module)
- Limitations
- See Also

Let me create a clean, token-efficient version.</think>

---
type: CommandReference
command: B::Hooks::EndOfScope
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference

- `on_scope_end { ... }` — register code to run after the surrounding scope finishes compilation
- `use B::Hooks::EndOfScope::XS` — explicitly request the XS implementation
- `use B::Hooks::EndOfScope::PP` — explicitly request the pure-perl implementation
- `$ENV{B_HOOKS_ENDOFSCOPE_IMPLEMENTATION} = 'XS'` — select implementation via environment variable

## Name

`B::Hooks::EndOfScope` — Execute code after a scope finished compilation

## Synopsis

perl
on_scope_end { ... };
## Functions

- `on_scope_end { ... }` — register a code block to run after the surrounding scope has been compiled
- `on_scope_end $code` — register a code reference to run after the surrounding scope has been compiled

Exported by default. Customizable via [Sub::Exporter](https://metacpan.org/pod/Sub::Exporter).

## Limitations

### Pure-Perl Mode Caveat

Applies to any perl where [Variable::Magic](https://metacpan.org/pod/Variable::Magic) is unavailable or disabled.

`Variable::Magic` can throw exceptions from within a callback; the pure-perl implementation cannot. Compile-time exceptions are converted to warnings and execution continues as if nothing happened.

To force an implementation explicitly:

perl
use B::Hooks::EndOfScope::XS;
# or
use B::Hooks::EndOfScope::PP;
Or set `$ENV{B_HOOKS_ENDOFSCOPE_IMPLEMENTATION}` to `"XS"` or `"PP"`.

### Perl 5.8.0 - 5.8.3

A core interpreter bug (rt.cpan.org bug 27040) causes the module to leak a single empty hash per scope being cleaned. `Variable::Magic` is disabled on perls prior to 5.8.4 to stabilize this workaround. Loading `B::Hooks::EndOfScope::XS` explicitly on these systems raises a compile-time exception.

### Perl 5.6.x

Perl before 5.8.0 cannot change the visibility of `%^H` via bit 17 of `$^H`. The module must use the `local` operator explicitly on these platforms, which may interfere with other scope-driven libraries relying on the same mechanism. No known incompatibilities at time of writing.

See `t/02-localise.t` in the distribution for further details.

## See Also

- [Sub::Exporter](https://metacpan.org/pod/Sub::Exporter)
- [Variable::Magic](https://metacpan.org/pod/Variable::Magic)

## Version

version 0.25