in reply to Unit testing built-in functions

You can only override some builtins, and those you can override must be overridden by importing the replacement functions from another module.

>type TestHarness.pm

use strict; use warnings; package TestHarness; BEGIN { our @EXPORT_OK = qw( unlink ); require Exporter; *import = \&Exporter::import; } sub unlink { print("PRE unlink\n"); # Don't forget to properly handle context. my $rv = unlink(@_); print("POST unlink\n"); return $rv; } 1;

>type test.pl

use strict; use warnings; BEGIN { # Parse options or something. $::TESTING = 1; } use if $::TESTING, TestHarness => qw( unlink ); unlink();

>perl test.pl

PRE unlink POST unlink

You could control TestHarness::unlink's behaviour using globals.

Replies are listed 'Best First'.
Re^2: Unit testing built-in functions
by Sidhekin (Priest) on Nov 09, 2006 at 01:06 UTC

    You can only override some builtins, and those you can override must be overridden by importing the replacement functions from another module.

    The former: Yes.

    The latter: Um, no. Or at least, not quite:

    ~$ perl -le 'BEGIN { *CORE::GLOBAL::unlink = sub(@) { print "@_" } } u +nlink "aaa".."aad"' aaa aab aac aad

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!