Skip to content

Instantly share code, notes, and snippets.

@ozuma
Created June 28, 2013 08:09
Show Gist options
  • Save ozuma/5883219 to your computer and use it in GitHub Desktop.
Save ozuma/5883219 to your computer and use it in GitHub Desktop.
How to create a private method in Perl.
#!/usr/bin/perl
# It looks like magic, but in fact, it's mere random number.
package Magic;
use strict;
use warnings;
sub new {
my $pkg = shift;
bless {
x => shift,
}, $pkg;
}
# private Method, but everybody can access.
sub _castSpell {
return int(rand(10));
}
# private Method, and nobody can access.
my $secret_method = sub{
return int(rand(10));
};
# x is dummy value.
sub getX {
my $self = shift;
return $self->{x};
}
# Magic is mere random number.
sub getMagicNum {
my $self = shift;
# return $self->_castSpell();
return $self->$secret_method;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment