Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: REALLY Private Methods in perl: Is Perl Flexible enough to be made Inflexible?

by dws (Chancellor)
on Sep 23, 2002 at 17:23 UTC ( #200176=note: print w/replies, xml ) Need Help??


in reply to REALLY Private Methods in perl: Is Perl Flexible enough to be made Inflexible?

Is there an eligant way to make methods in perl really private?

You're got some good answers above. Here's a different approach.

You can get the delayed effect of private methods by adopting a simple convention: Start all private methods with an underscore, and teach people not to invoke these methods from outside of the class hierarchy1. To enforce this, it's a simple matter to spider your code base looking for references to private methods that are not preceded by "$self->".

Having a script that traverses the code base looking for certain patterns can be very useful. In team settings, I've set up scripts to look for special code patterns (e.g., "TBD" or "FIXME" comments, correct copyright strings, etc.) The scripts ran every night, and produced a report (an HTML page) that we would check every morning. It also gave us an easy way to chart our code growth over time.

The question you'll need to ask yourself is whether you can live with the delay. I've found that the convention was good enough.


1 Purists will note that this really enforced "protected" methods.

  • Comment on Re: REALLY Private Methods in perl: Is Perl Flexible enough to be made Inflexible?

Replies are listed 'Best First'.
Re: Re: REALLY Private Methods in perl: Is Perl Flexible enough to be made Inflexible?
by samtregar (Abbot) on Sep 23, 2002 at 18:50 UTC
    Great idea! Care to share your script?

    -sam

      Care to share your script?

      My ex-employers own the last ones I wrote, but they're pretty simple. Use File::Find to traverse a hierarchy, then ignore file types that aren't interesting (like .doc or .exe), and finally processing files that are. Stripped of extraneous detail, they look something like:

      use File::Find; my $ignore = '(?:\.exe|\.doc)$'; find(\&consider, $root); emit_report(); sub consider { my $path = $File::Find::path; return if $path =~ m/$ignore/o; process($path); }
      Processing consists of slurping the file into memory and running a set of regexs against it, recording which files match which regex. Then sort the hashes and spit out HTML. It's just bookkeeping at this poing. Being a potential memory pig isn't a serious issue for a script that runs once a day at 2am.

        Sure, the basic structure is simple. The good bits are those helpful regexes that spot naughty coding. That's the hard part. Still, probably worth doing. Thanks for the tip.

        -sam

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://200176]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2020-03-29 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (171 votes). Check out past polls.

    Notices?