2

I am trying to call subroutine having 8 parameters in another perl script(sample.pl) from my perl script(sample1.pl). i am getting this error. Error is shown as

"sum of 8 numbers is 0Illegal character in prototype for main::callng_prgm : $a,$b,$c,$d,$e,$f,$g,$h at D:/workspace/sdff/sample.pl line 4.
Use of uninitialized value $b in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $a in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $c in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $d in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $e in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $f in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $g in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $h in addition (+) at D:/workspace/sdff/sample.pl line 6.
"

can you please suggest me how to do this

sample1.pl

require 'sample.pl';
use strict;
use warnings;
my $sa = main_prgm();
sub main_prgm {
    eval(callng_prgm(10,12,15,14,16,12,12,12));
}

sample.pl

use strict;
use warnings;
our ($a,$b,$c,$d,$e,$f,$g,$h);
sub callng_prgm ($a, $b, $c, $d, $e, $f, $g, $h) {
    my $z = ($a+$b+$c+$d+$e+$f+$g+$h);
    print "sum of 8 numbers is $z";
}
1;
2
  • 3
    Prototypes in Perl are not required, and do not work the same way as other languages, so do not use them unless you know what they do. (Prototypes is what is contained in parens after the sub name). A sub declaration should look like this: sub NAME { CODE } (no parens) Commented Nov 21, 2013 at 10:40
  • 1
    You try to use named arguments that are not implemented in perl5 You can can achieve this another way perldesignpatterns.com/?NamedArguments Commented Nov 21, 2013 at 11:00

2 Answers 2

1

Just define your sub like:

sub callng_prgm
{
  my ($a,$b,$c,$d,$e,$f,$g,$h) = @_
  my $z= ($a+$b+$c+$d+$e+$f+$g+$h);
  print "sum of 8 numbers is $z";
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes i am using that code, it gives answer but having one warning as"Illegal character in prototype for main::callng_prgm : $a,$b,$c,$d,$e,$f,$g,$h at D:/workspace/sdff/sample.pl line 4.",how to solve that warning..
@santoshikumari No, you are not using that code, or you would not get that warning. It's quite impossible, so read the code that M42 posted more carefully.
1

Why restrict yourself to 8 parameters? This will work with any number of parameters.

#!/usr/bin/perl

use strict;
use warnings;
sub callng_prgm
{
   my $z;
   foreach (@_) {$z += $_};
   print $z;
}
callng_prgm(10,12,15,14,16,12,12,12);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.