D
D
Denis Menshikov2014-09-06 16:47:17
Perl
Denis Menshikov, 2014-09-06 16:47:17

Perl protected and private methods?

Please tell me how to arrange protected and private methods in OOP Perl. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Ptktysq, 2014-10-03
@Denisrf

The OOP of Perl has nothing to do with the OOP of C++-like languages.
If you really want to, you can use an anonymous function as a private method and pass $self to it as the first parameter:

package MyClass;

my $private_method = sub {
    my $self = shift;
    # ...
  };

sub public_method {
  my $self = shift;

  $private_method->($self);
}

By the way, such a call is faster than $object->name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question