Thursday, July 8, 2010

Real private method in Perl

For a truly private method, you would usually write the following:


my $_private_method = sub { my ($self, ...) = @_; ... };
...
$self->$_private_method($some, $arguments);
...


which makes the method scoped to the block it was defined in, or the file if it’s not within braces.

This is Perl.

The reason is $_private_method is not special, it's just a "my" variable. So it will be "invisible" outside block or file scope.

No comments:

Post a Comment