How do I make private functions in a Perl module?

前端 未结 9 940
后悔当初
后悔当初 2020-12-23 20:43

I am working on a little Perl module and for some reason I had the test driver script that was using my new module call one of the functions that I thought would be private,

相关标签:
9条回答
  • 2020-12-23 21:06

    If you use a system like Moose, you can get a public/private distinction as seen here.

    0 讨论(0)
  • 2020-12-23 21:12

    There is only "The Kludge" of storing a code reference in a lexical variable, which no one outside that scope can see:

    my $priv_func1 = sub { my $self = shift; say 'func1'; };
    
    sub public_sub { 
        my $self = shift;
    
        $priv_func1->( $self );
    }
    

    And I can't think of a way to make rigorously "protected" fields.

    That's it as far as I know ( besides source filters...shhhh. I didn't mention them.... )


    EDIT: Actually, it turns out I can think of a very messy way of doing protected. But it would probably involve passing all calls through the AUTOLOAD sub. (!!)

    0 讨论(0)
  • 2020-12-23 21:15

    In File for your package: Define private methods as CODE-Ref, i.e.:

    my $private_methode = sub{};
    
    0 讨论(0)
提交回复
热议问题