How do I export a LEAVE phaser to the outer scope of a use statement

Thanks to Zoffix++ for pointing out a very hacky way of doing this.

sub EXPORT() {
    $*W.add_phaser: $*LANG, 'LEAVE', { code you want to run }
    {}  # need to show that we're not exporting anything
}

This hack depends on various Rakudo internals, and is therefore not recommended to be used "in the wild". And it's quite likely that a better, more supportable way will be implemented for this functionality in the near future.

This hack was needed for a module that supports a sort of timely destruction other than from the direct scope in which an object is created (aka LEAVE phaser). This is typically handled in Perl 5 by using reference counting and calling DESTROY if the reference count of an object goes to 0.

This module can now be found in the Perl 6 ecosystem: FINALIZER. This module allows module developers to mark created objects for finalization: by default on program exit. Or from a scope indicated by the client program.

Tags:

Raku