summaryrefslogtreecommitdiff
path: root/posts/2009-06-30-private-and-protected-methods-with-moose.org
diff options
context:
space:
mode:
authorFranck Cuny <franckcuny@gmail.com>2016-08-04 11:12:37 -0700
committerFranck Cuny <franckcuny@gmail.com>2016-08-04 11:12:37 -0700
commit2d2a43f200b88627253f2906fbae87cef7c1e8ce (patch)
treec65377350d12bd1e62e0bdd58458c1044541c27b /posts/2009-06-30-private-and-protected-methods-with-moose.org
parentUse Bullet list for the index. (diff)
downloadlumberjaph-2d2a43f200b88627253f2906fbae87cef7c1e8ce.tar.gz
Mass convert all posts from markdown to org.
Diffstat (limited to 'posts/2009-06-30-private-and-protected-methods-with-moose.org')
-rw-r--r--posts/2009-06-30-private-and-protected-methods-with-moose.org51
1 files changed, 51 insertions, 0 deletions
diff --git a/posts/2009-06-30-private-and-protected-methods-with-moose.org b/posts/2009-06-30-private-and-protected-methods-with-moose.org
new file mode 100644
index 0000000..bda5479
--- /dev/null
+++ b/posts/2009-06-30-private-and-protected-methods-with-moose.org
@@ -0,0 +1,51 @@
+Yesterday, one of our interns asked me a question about private method
+in Moose. I told him that for Moose as for Perl, there is no such things
+as private method. By convention, methods prefixed with '\_' are
+considered private.
+
+But I was curious to see if it would be something complicated to
+implement in Moose. First, I've started to look at how the 'augment'
+keyword is done. I've then hacked Moose directly to add the private
+keyword. After asking advice to nothingmuch, he recommended me that I
+implement this in a MooseX::* module instead. The result is here.
+
+From the synopsis, MooseX::MethodPrivate do:
+
+#+BEGIN_SRC perl
+ package Foo;
+ use MooseX::MethodPrivate;
+
+ private 'foo' => sub {
+ };
+
+ protected 'bar' => sub {
+ };
+
+
+ my $foo = Foo->new;
+ $foo->foo; # die, can't call foo because it's a private method
+ $foo->bar; # die, can't call bar because it's a protected method
+
+ package Bar;
+ use MooseX::MethodPrivate;
+ extends qw/Foo/;
+
+ sub baz {
+ my $self = shift;
+ $self->foo; #die, can't call foo because it's a private method
+ $self->bar; # ok, can call this method because we extends Foo and
+ # it's a protected method
+ }
+#+END_SRC
+
+I was surprised to see how easy it's to extend Moose syntax. All I've
+done was this:
+
+#+BEGIN_SRC perl
+ Moose::Exporter->setup_import_methods(
+ with_caller => [qw( private protected )],);
+#+END_SRC
+
+and write the =private= and =protected= sub. I'm sure there is some
+stuff I can do to improve this, but for a first test, I'm happy with the
+result and still amazed how easy it was to add this two keywords.