in reply to Using closures to achieve data hiding in OO Perl

There are big holes in your implementation. Sure, $selfmirage->{'washhands'} contains a closure, but all the closure does is call a global subroutine with the private data as argument.

All $dirtyeater needs to do is to replace Dinnerclosure::washhands with a sub of his own, and $dirtyeater can modify handsclean all he wants. See the following test code:

#!/usr/bin/perl use strict; use warnings; use Dinnerclosure; my $dirtyeater = Dinnerclosure->new(); { no strict 'refs'; no warnings 'redefine'; local *{"Dinnerclosure::washhands"} = sub { my $obj = shift; $obj->{'handsclean'} = 1; print "I didn't really wash my hands\n"; return 1; }; $dirtyeater->{'washhands'}->(); $dirtyeater->{'eatfood'}->(); } __END__ I didn't really wash my hands You washed your hands -eat all you want
$dirtyeater didn't wash his hands - he just pretended to. But he still was allowed to eat.

Replies are listed 'Best First'.
Re^2: Using closures to achieve data hiding in OO Perl
by saurabh.hirani (Beadle) on Jun 13, 2009 at 05:06 UTC
    Thanks for pointing that out. I hadn't thought of that. But using closures at least thwarts out the easier way to manipulate the object.