Skip to content

Instantly share code, notes, and snippets.

@dolmen
Created February 14, 2014 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolmen/8999556 to your computer and use it in GitHub Desktop.
Save dolmen/8999556 to your computer and use it in GitHub Desktop.
Exemple de méthode vraiment privée en Perl.
use strict;
use warnings;
no warnings 'experimental';
use feature 'lexical_subs'; # my sub, experimental in perl 5.18
use feature 'say';
use utf8;
package AA;
sub new
{
bless \(my $nom = $_[1]), ref($_[0]) || $_[0]
}
my sub privée_5_18
{
my $self = shift;
say "Méthode privée_5_18 appellée depuis ${$self} !";
}
my $privée = sub
{
my $self = shift;
say "Méthode privée appellée depuis ${$self} !";
};
sub public
{
my $self = shift;
say 'Méthode publique appellée !';
$self->$privée();
# $self->privée_5_18(); => ne marche pas
privée_5_18($self);
}
1;
#!/usr/bin/env perl
use strict;
use warnings;
use Term::Encoding;
use open ':std', ':encoding(' . Term::Encoding::term_encoding() . ')';
use AA;
AA->new('Toto')->public;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment