Created
June 28, 2013 08:09
-
-
Save ozuma/5883219 to your computer and use it in GitHub Desktop.
How to create a private method in Perl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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