in reply to Re^3: I hate the leading underscores.
in thread I hate the leading underscores.

there are no built-in security measures in place to hide variables and methods
The way to hide variables and methods is not to document them. Programmers using a module are not expected to read the code, they're expected to read the documentation. Using undocumented features is always an At Your Own Risk activity.

If you prefer to make things programmatically inaccessible, scoping is the built-in tool for that, though I don't think that proper methods (automatically passing self) can be made this way. I'm not sure why you don't consider closures a built-in way to hide things.

More on this topic in Private Methods Meditation.


Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^5: I hate the leading underscores.
by RazorbladeBidet (Friar) on Feb 16, 2005 at 18:32 UTC
    The way to hide variables and methods is not to document them. Programmers using a module are not expected to read the code, they're expected to read the documentation. Using undocumented features is always an At Your Own Risk activity.

    Not quite - not documenting them is just one way to hide variables. Programmers may not be expected to read the code, but they don't always do as expected (do you?). I agree with your last statement

    If you prefer to make things programmatically inaccessible, scoping is the built-in tool for that, though I don't think that proper methods (automatically passing self) can be made this way. I'm not sure why you don't consider closures a built-in way to hide things.

    Ok, I can agree with you there. I've little experience with using scope to hide things from end users. I do consider closures a built-in way to hide things. I can see them in a scope sense being similar to private variables. However they seem to be more than that, and you have more rope to hang yourself with. I don't consider closures akin to the private/protected/public modifiers. That does not make it a bad thing, I just don't see it as being intended to provide that. I could be wrong, of course.

    What I would say (overall) is that it makes placing an underscore before a private variable a secondary security measure. If a private variable is undocumented, that may well be the first line of defense, I'll grant you that. However, if a premise of using visual cues (or lack thereof) to alert a developer to the accessibility of an attribute or method has already been established, then it makes more sense to use the leading underscore in that case.
      Not quite - not documenting them is just one way to hide variables.
      It is the official Perl way, as envisioned by Larry Wall. (I should have said "members" rather than "variables". Hiding variables is done by scoping.) Any other technique used to hide private methods and members constitutes an attempt to write C++ (or some other language) in Perl.
      it makes placing an underscore before a private variable a secondary security measure
      It's really just a flag to help the module author (and any developer with prying eyes) keep things straight. Nothing more. I agree that it's a good convention.

      Caution: Contents may have been coded under pressure.
        It is the official Perl way, as envisioned by Larry Wall.
        Larry Wall seems to have had a slight vision adjustment on this subject. See A12 and S12 for details.
        It is the official Perl way, as envisioned by Larry Wall. (I should have said "members" rather than "variables". Hiding variables is done by scoping.) Any other technique used to hide private methods and members constitutes an attempt to write C++ (or some other language) in Perl.


        Do you have a link to that? Also, while I agree that Mr. Wall has done some amazing things, people can be different. Using other techniques would not constitute the attempt to write another language in Perl, it would be an attempt to write OO, which is precisely what Perl is trying to emulate with classes.

        It really does just come down to a matter of preference, and the leading underscore really isn't the problem, it's the incomplete OO support. Although please note I have not delved in depth into Perl's OO support beyond simple class constructs, so please do correct me if I am wrong) People have issues in Java with underscores all the time.