Steve Dawson wrote:
> I hear everybody constantly chanting the use strict and use
> warnings mantra in this and every other perl group. No
> argument there, but what's the position on prototypes in
> subs? I prefer to use them but that's probably because I was
> taught Pascal way back when and now I compulsively want to
> type all my variables and predeclare my subroutines, unlike
> my C trained workmates who put their main() at the top of the code.
> 
> One nagging thing with prototypes is when I use them
> recursively warnings carps about not being able to test the
> prototype so I end up wrapping it in a no warnings (example
> below). Am I being too pedantic about my subs and should I
> just trust myself not to call the subs incorrectly?
> 
> Steve
> 
> sub descend($$){
> #map out the
> endpoints of an unknown record structure
>     my ($name,$ref) = @_;
> #accumulated
> name of var and scalar or ref to the next level
>     if(ref($ref) eq ''){
> #map scalar
> endpoints
>         print $name.": [$ref]\n";
> 
> 
>     }
>     if(ref($ref) eq 'HASH'){
> #explore the
> keys of a hash
>         print "$name: [undef]\n" unless(scalar(keys %$ref));
>         for my $element (keys %$ref){
>             no warnings;                                    #disable
>             warning for recursion
>     descend($name.".$element",$ref->{$element});         } }
>     if(ref($ref) eq 'ARRAY'){
> #enumerate
> the elements of an array
>         print "$name: [undef]\n" unless(scalar(@$ref));         my
>         $index = 0; for my $element (@$ref){
>             no warnings;                                    #disable
>             warning for recursion descend($name.".[$index]",$element);
>             $index ++;
>         }
>     }
> }

Prototypes in Perl are not the same as prototypes in other languages.
According to 'perldoc perlsub' "intent of this feature is primarily to
let you define subroutines that work like built-in functions". For that
reason I have rarely (if ever? - not sure) used them. My advice would be
not to use them unles you actually need your subs to look like builtin
functions.

BTW, for recursion to work with prototypes you need to pre-declare the
subrouting. For example:

use strict;
use warnings;

sub test_sub ($$);

sub test_sub ($$) {
    my $str = shift;
    my $num = shift;
    print "str=$str num=$num\n";
    test_sub $str, $num - 1 unless $num <= 0;
}

test_sub "Hello", 3;


HTH

-- 
Brian Raven
 


-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely 
for the intended addressee(s). Unauthorised reproduction, disclosure, 
modification, and/or distribution of this email may be unlawful. If you 
have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message 
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary 
companies.
-----------------------------------------------------------------------


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to