Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • What's the point of using function prototypes if you use the sigil for calling them? (Yeah, I do think the ampersand is a very nearly an abomination.)

    • by wickline (135) on 2002.08.07 5:53 (#11496) Journal
      To be honest, I use both for documentation more than anything else.
      I've benefited from this use, and have yet to be bit by it, so I just
      keep on doing it...

      I've used ampersands as a sigil since I started writing perl because
      it makes it plain that we're talking about a subroutine. It also reads

          &load_data; &collate_records;
          "...and load data and collate records"

      It also allows me to have more control when doing a s///g on my own
      code. &foo is never confused with @foo{'foo',$foo,$foo{$foo},@foo}.
      If I want to rename a subroutine to something more appropriate, or
      even just find every place where it's used, I can do so without a
      bunch of false positives. I also like to use parens around my args
      when I don't strictly need them... it's free documentation, and also
      lets me easily select the whole list of args to copy/paste/replace
      them (BBEdit lets you double click on a paren/bracket/brace to select
      the whole block, or command-B from within the block).

      I tend not to use ampersands for functions imported from modules,
      because I'd rather allow any type checking to proceed and I'm not very
      likely to be re-naming them. This also makes them look more like built
      in functions, which is how I'd rather think of those imported subs.

      I don't always use prototypes. When I do use them, I do it more as a
      way of telling someone reading the code "hey, this sub takes these args" rather than to tell perl anything about the sub. The prototype
      plus the initial assignment from @_ can tell someone immediately that
      you're not using @_ any further, or that you're going to be doing
      something with "everything left in @_" later on in the subroutine.

      Yes, I know I'm missing out on some handy error-checking by using the
      ampersand as a sigil, but I personally find the sigil more useful than
      the error-checking. I'm much much more likely to be greatful for an
      unambiguous string to identify calls to my subroutines than I am to be
      greatful for some arg-checking by perl.

      In production code, I do document my subs, but sometimes when you're
      debugging it's easy to just look at the code and ignore the POD. The
      prototype is just a bonus bit of documentation.

      Bad habit? Maybe. I work with some folks who are learning perl and so
      far it seems to work well for them. They don't tend to have problems
      figuring out what sorts of args to use for a function. They do like
      that they can see at a glance what is a subroutine call and what is
      a call to a built-in function (or imported sub).