in reply to Testing private methods a no-no?

FYI: Did you know you can create truly private subs in Perl?

# create a block to isolate the sub { local *foo = sub { print "foo\n"; }; # this works foo(); } # this does not work foo();

Replies are listed 'Best First'.
Re^2: Testing private methods a no-no?
by Athanasius (Cardinal) on May 22, 2016 at 05:50 UTC

    Hello shawnhcorey,

    This is nice, but it’s probably cleaner to assign the subroutine reference to a lexical variable:

    { my $foo = sub { my ($n) = @_; print "foo($n)\n"; }; $foo->(1); # valid call } $foo->(2); # invalid call

    This has the advantage of changing the invalid call from a runtime error into a compiletime error.

    From Perl v5.18 on you can also use the experimental Lexical Subroutines feature.

    Update: Fixed typo in the code snippet.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^2: Testing private methods a no-no?
by Anonymous Monk on May 20, 2016 at 21:01 UTC
    that was random