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.

In reply to Re: Using closures to achieve data hiding in OO Perl by JavaFan
in thread Using closures to achieve data hiding in OO Perl by saurabh.hirani

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.