How to taunt with built-in operators

I need to make fun of the built-in type operators unlinkand renameas part of a test suite. I can't get this to work usingTest::MockObject

>> my $mock = Test::MockObject->new();
>> $mock->mock('unlink', sub { print "Mocked!\n"; });
>> unlink "foo";
0

Is it possible to mock such built-in functions?

+4
source share
1 answer

See overriding core functions :

To override the Perl built-in procedure with your own version, you need to import it at compile time. This can be done using subs pragma. This will only affect the package into which you imported the specified routine:

   use subs 'chdir';
    sub chdir { ... }
    chdir $somewhere;

( ), CORE::GLOBAL :

    BEGIN {
        *CORE::GLOBAL::hex = sub {
            # ... your code here
        };
    }

, :

   print hex("0x50"),"\n";            # prints 1

, , , CORE:::

   print CORE::hex("0x50"),"\n";      # prints 80

. perldoc perlsub:

, ( ) . , . , prototype "CORE::builtin_name" (. prototype).

, , , (, chomp). , .

do, require glob can , - , . ( do BLOCK).

require : require require Foo::Bar, "Foo/Bar.pm" @_. . require.

, , glob, glob <*> .

readline I/O <FILEHANDLE>. , readpipe `` qx//.

, (, exists grep) .

+5

Source: https://habr.com/ru/post/1680339/


All Articles