NAME

Manip::END - Mess around with END blocks

SYNOPSIS

use Manip::END qw( clear_end_array set_end_array );

clear_end_array();

set_end_array(sub {...}, sub {...});

$ar = Manip::END->get_ref;
$ar->unshift(sub {...}, sub {...});

DESCRIPTION

Perl keeps an array of subroutines that should be run just before your program exits (see perlmod manpage for more details). This module allows you to manipulte this array.

WARNING

This module gives you access to one of Perl's internal arrays that you're not supposed to see so there are a couple of funny things going on.

The array contains an undef for each END blcok that has been encountered, it's not really an undef though, it's some sort of raw coderef that's not wrapped in a scalar ref. This leads to fun error messages like

Bizarre copy of CODE in sassign

when you try to assign one of these values to another variable. This can make manipulating the array a little delicate if you want to preserve these values.

That said, you can erase them without any problem and you can add your own coderefs without any problem too. If you want to selectively remove items from the array, that's where the fun begins. You cannot do

@$ref = grep {...} @$ref

if any of the undef coderefs will survive the grep as they will cause an error such as the one above. It's probably best to use the provided filter methods.

HOW TO USE IT

EXPORTED FUNCTIONS

clear_end_array()

This will clear the array of END blocks.

set_end_array(@blocks)

@blocks is an array of subroutine references. This will set the array of END blocks.

CLASS METHODS

Manip::END->get_ref()

This will return a blessed reference to the array of END blocks which you can manipulate yourself. You can also invoke several methods on this array reference.

OBJECT METHODS

$obj->unshift(@blocks)

@blocks is an array of references to code blocks. This will add the blocks to the start of the array. By adding to the start of the array, they will be the first code blocks executed by Perl when it is exiting

$obj->push(@blocks)

@blocks is an array of references to code blocks. This will add the blocks to the end of the array. By adding to the end of the array, they will be the last code blocks executed by Perl when it is exiting

$obj->clear()

This clears the array.

$obj->filter_sub($code)

$code is a reference to a subroutine. For each element of the array, this will execute the subroutine in $code, passing in the element as the first argument. If the subroutine returns a true value, the element will be kept. If it returns false, the element will be removed from the array.

$obj->remove_isa($class)

$class is a string containing the name of a class. This removes all of the elements which inherit from $class.

$obj->remove_class($class)

$class is a string containing the name of a class. This removes all of the elements which are blessed into $class.

TODO

It would be nice if Perl didn't store those funny undef values but rather stored real CODE refs.

AUTHOR

Written by Fergal Daly <fergal@esatclear.ie>. Suggested by Mark Jason Dominus at his talk in Dublin.

LICENSE

Copyright 2003 by Fergal Daly <fergal@esatclear.ie>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html