How do I redefine built in Perl functions?

前端 未结 2 1468
暖寄归人
暖寄归人 2021-01-11 15:39

I want to do two things:

In production code, I want to redefine the open command to enable me to add automagic file logging. I work on data processing applications/f

2条回答
  •  隐瞒了意图╮
    2021-01-11 15:46

    For open: This worked for me.

    use 5.010;
    use strict;
    use warnings;
    use subs 'open';
    use Symbol qw;
    
    sub open (*$;@) { 
        say "Opening $_[-1]";
        my ( $symb_arg ) = @_;
        my $symb;
        if ( defined $symb_arg ) { 
            no strict;
            my $caller = caller();
            $symb = \*{$symb_arg};
        }
        else { 
            $_[0] = geniosym;
        }
        given ( scalar @_ ) { 
            when ( 2 ) { return CORE::open( $symb // $_[0], $_[1] ); }
            when ( 3 ) { return CORE::open( $symb // $_[0], $_[1], $_[2] ); }
        }
        return $symb;
    }
    
    open PERL4_FH, '<', 'D:\temp\TMP24FB.sql';
    open my $lex_fh, '<', 'D:\temp\TMP24FB.sql';
    

    For Printf: Did you check out this question? -> How can I hook into Perl’s print?

提交回复
热议问题