PBP: 032 Utility Subroutines

Here’s where the PBP tells you to prefix “internal use only” subroutines with an underscore.  Why it calls them “utility subroutines” I don’t know.

This is a longstanding Perl practice that may even be in one of the many official docs somewhere, but is often unknown to new people. It’s not obvious what that means, and it allows a naive programmer to call them anyway and get themselves into trouble.  (This is why many people wish Perl had real private functions, to which I say feh.  Learn some care and don’t go where you aren’t wanted.)

The PBP suggests that “… and reserved (by ancient C/Unix convention) for non-public components of a system.”  This is probably true, but suffers the problem that anyone who didn’t get to Perl via C or by programming through the 1970’s may not have at hand.  It’s good to write it down and actually say it.

 

One Response to “PBP: 032 Utility Subroutines”

  1. I hate the convention of using leading underscores for internal features myself. If you want to advise people not to use a sub directly, you should document it that way, and ideally explain to them why they shouldn’t use it if they want to (usually, there actually is no reason).

    One of the things that bugs me about it is it’s always ambiguous where the boundary is between internal and external. Is it internal to the project, or just that module? Is it okay if children use it?

Leave a Reply