Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Re: use lib "/..." and sub parms..

by Vautrin (Hermit)
on Mar 15, 2004 at 18:29 UTC ( #336772=note: print w/replies, xml ) Need Help??

in reply to Re: use lib "/..." and sub parms..
in thread use lib "/..." and sub parms..

I'd like to expand on prototyping for a minute. Prototyping is putting a set of parentheses after the name of a sub to specify what types of variables it expects as arguments, and how many variables. Some examples:

#! /usr/bin/perl use strict; use warnings; # sub without prototypes # use unless you have a reason to prototype sub foobar { # do stuff; } # sub that expects one scalar sub foobaz ($) { # do stuff; } # sub that expects the first argument # to be an array, the second to be a # hash, and the third to be a scalar sub foobarbaz (@, %, $) { # do stuff; }

There are probably 3 reasons you'd want to prototype:

  1. Speed: If you prototype a function, it becomes a candidate for inlining at compile time. Basically, whenever a function is called, there is some time taken to switch from the current function to the function, and then come back. This time can be eliminated by replacing the code that calls the function with the function's code itself. Before you go out and prototype all your functions for speed, know that this gain is very small, and unless you're doing some really heavy computer work, it's just going to cause confusion.
  2. Mimic strongly typed languages: Languages like Java and C believe that you should have very strict rules for variables -- you declare variables as a certain type and unless you explicitely declare a conversion they are treated as whatever you declare them as. Some people coming over from other languages can find Perls "Do what you mean" style hard to deal with at first (I personally did a lot of silly things when I first learned). You should not consider this to be a Good Reason.
  3. Try to prevent your users from doing something dumb: If your user doesn't read the POD / Manifest and does something dumb, they will get an error (assuming they use strict; and preferably also use warnings;). A lot of times, this can be really unperlish. (For instance, all functions in accept an unlimited number of arguments, and will display each one in turn).

I hope that helps,


Want to support the EFF and FSF by buying cool stuff? Click here.

Replies are listed 'Best First'.
Re: Re: Re: use lib "/..." and sub parms..
by Aristotle (Chancellor) on Mar 15, 2004 at 20:34 UTC
Re: Re: Re: use lib "/..." and sub parms..
by mrpeabody (Friar) on Mar 16, 2004 at 00:46 UTC
    Vautrin: You are right about "sub foobar {...}" being the right way to code unless and until you completely understand what Perl calls "prototypes". However, otherwise your post is almost completely false. Specifically:

    • prototypes do not provide checking of argument number and type. They might look like they do, but they don't.

    • the "@" and "%" prototypes do not mean "array" and "hash".

    • the "\@" and "\%" prototypes do not mean "arrayref" and "hashref".

    Once more: prototypes do not provide checking of argument number and type.

    Why even have them then? Good question. Tom Christiansen's prototypes explanation explains the whole situation. It is detailed and crystal-clear.

    If you want to check number and type of arguments, the best way is to do it by hand.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2022-05-28 08:06 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (98 votes). Check out past polls.