In Perl Hacks, chromatic gives a very instructive solution that uses Attribute::Handlers. This is the example given:

package Class::HideMethods; use strict; use warnings; use Attribute::Handlers; sub import { my ($self,$ref) = @_; my $package = caller(); $prefixes{ $package } = $ref; } sub gen_prefix { my $invalid_chars = "\0\r\n\f\b"; my $prefix; for (1..5){ my $chars_pos = int (rand(length($invalid_chars))); $prefix .= substr ($invalid_chars, $char_pos, 1); } return $prefix; } package UNIVERSAL; sub Private :ATTR { my ($package, $symbol, $referent, $attr, $data, $phase) = @_; my $name = *{ $symbol }{NAME}; my $newname = Class::HideMethods::gen_prefix ($package) . $name; my @refs = map { *$symbol{$_}} qw (HASH SCALAR ARRAY GLOB); *$symbol = do {local *symbol}; no strict 'refs'; *{ $package . '::' . $newname } = $referent; *{ $package . '::' . $name } = $_ for @refs; $prefixes{ $package }{$name} = $newname; } 1;

To use this module:

package SecretClass; my %methods; use Class::HideMethods \%methods; sub new { bless {}, shift } sub hello :Private { return 'hello' } sub goodbye { return 'goodbye' } sub public_hello { my $self = shift; my $hello = $methods{hello} $self -> $hello(); } 1;

To prove that it works:

use Test::More tests => 6; my $sc = SecretClass->new(); isa_ok ($sc, 'SecretClass' ); ok (! $sc->can ('hello'), 'hello() should be hidden' ); ok ($sc->can('public_hello'), 'public_hello() should be available'); is ($sc->public_hello(),'hello', '... and should be able to call hello +()' ); ok ($sc->can('goodbye'), 'goodbye() should be available'); is ($sc->goodbye(), 'goodbye', '... and should be callable');

Hope this helps

citromatik


In reply to Re: Howto keep private methods private? by citromatik
in thread Howto keep private methods private? by perl-diddler

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.