All pastes #1709999 Raw Edit

Miscellany

public text v1 · immutable
#1709999 ·published 2009-12-10 19:56 UTC
rendered paste body
use strict;

# Actual before hook
sub regBefore(&$) {
	my ($sub, $mainname) = @_;
	no warnings qw(prototype redefine);
	no strict "refs";
	my $mainfunc = *{$mainname}{CODE};
	*{$mainname} = sub {&$sub;&{$mainfunc}(@_)};
}

# Some tests
sub mainfunc($) {
	my $arg = shift;
	print "In mainfunc: $arg\n";
}

mainfunc("test:nobefore");

regBefore {print "Before\n"} "mainfunc";

mainfunc("test:before");

use File::Basename qw(basename);

regBefore {print "Call basename!\n"} "basename";

print "Basename of 'c:\\test.txt' is: ".basename("c:\\test.txt")."\n";

use POSIX qw(floor);

my $floorCnt=0;
regBefore {$floorCnt++} "floor";

for(my $i=1; $i<10; $i++) {
	for(my $j=1; $j<10; $j++) {
		my $res = floor($i/$j);
	}
}

print "floor was called $floorCnt times\n";