Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: sub argument passing? (TIMTOWTDI)

by protist (Monk)
on Sep 16, 2012 at 15:54 UTC ( #993926=note: print w/ replies, xml ) Need Help??

in reply to sub argument passing? (TIMTOWTDI)

There is another approach to this problem: prototyping.

#!/usr/bin/perl use warnings; use strict; #prototyped to explicitly state acceptable arguments sub addstuff($$$); my $D=4; my $E=3; my $F=2; #gives error if too few or too many arguments #(or arguements of wrong type) my $result = addstuff $D, $E, $F; print $result."\n"; #prototyped to match sub addstuff($$$){ my $A=shift; my $B=shift; my $C=shift; return $A+$B+$C; }

Comment on Re: sub argument passing? (TIMTOWTDI)
Download Code
Re^2: sub argument passing? (TIMTOWTDI)
by temporal (Pilgrim) on Sep 17, 2012 at 14:58 UTC

    Cool, didn't know that Perl supported function prototyping. Looks a bit quicker and dirtier than using Params::Check - fewer filtering options, no default values, etc.

    I am gravitating towards creating a separate file which would contain all of my function prototypes and then I would require that in my module. My module has quite a few functions so this might be the easiest way to keep track of them all and have a single point of change rather than having to track down all that template-building legwork within the subroutines themselves that Params::Check requires.

    Not quite as legible, though. And to heed the docs warning, I could get into trouble writing prototypes for already written functions. So I'll have to do a little checking to make sure I'm not doing anything too silly with how arguments are being passed and unpacked.

    Anyway, thanks for pointing this out protist =)

    Strange things are afoot at the Circle-K.

        This should provide some enjoyable light reading. :P Thank you choroba.
      You are welcome. :)
      Cool, didn't know that Perl supported function prototyping.

      It doesn't. It's more like 'function argument context prototyping'. Please read the highly recommended Far More Than...

Re^2: sub argument passing? (TIMTOWTDI)
by AnomalousMonk (Abbot) on Sep 17, 2012 at 21:04 UTC

      Thanks AnomalousMonk, I am following your advice and already seeing some issues that I'll run into with using Perl's prototyping such as it is.

      'Enjoyable light reading' indeed, protist! Maybe just enjoyable ;)

      Strange things are afoot at the Circle-K.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2015-07-03 03:07 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (47 votes), past polls