insanity
insanity

Reputation: 1178

Subroutine Prototype in PERL

sub joins($ @);
print "\nEnter a letter \n\n";
$l = <STDIN>;
chomp $l;
print "\n Enter Five String \n\n";
@arr;

for($i=0;$i<5;$i++) {
    $arr[$i] = <STDIN>;
    chomp $arr[$i];
}  
@ar = joins($l, @arr);
print "\nCombined Stirng is :-  @ar\n";
sub joins {
    ($x, @str)=@_;
    @j = ($x, @str);
    return @j;
}

I am getting prototype mismatch error. How can i fix it ?

Upvotes: 1

Views: 149

Answers (2)

RobEarl
RobEarl

Reputation: 7912

The problem is you've predeclared joins with a prototype of $@, then implemented it without any prototype. As others have noted, you don't take advantage of the prototype. You also don't take advantage of the sub being predeclared (ability to call without parenthesis) so remove this line:

sub joins($ @);

On the subject of prototypes, Perl::Critic considers them to be a severity 5 (worst) violation:

Contrary to common belief, subroutine prototypes do not enable compile-time checks for proper arguments. Don't use them.

Upvotes: 5

blio
blio

Reputation: 483

well, In fact, what you need to do is to change the joins function definition. You can change "sub joins" to "sub joins($@) to agree with the declare of the joins function. The code is list like the below.

1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 sub joins($@);
6
7 print "\nEnter a letter \n\n";
8 my $l=<STDIN>;
9 chomp $l;
10 print "\n Enter Five String \n\n";
11 our @arr;
12
13 for(my $i=0;$i<5;$i++)
14 {
15     $arr[$i]=<STDIN>;
16     chomp $arr[$i];
17 }
18
19 my @ar=joins($l,@arr);
20 print "\nCombined Stirng is :-  @ar\n";
21
22 sub joins($@)
23 {
24     my ($x, @str)=@_;
25     my @j=($x,@str);
26     return @j;
27 }

well, by the way, you can read the documentation here about prototype(http://perldoc.perl.org/perlsub.html#Prototypes). Besides, I also recommend you to use strict and warnings, although in demo code.

Upvotes: 4

Related Questions