CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
undefined subroutine of sum?

 



rockcone01
Novice

Jun 5, 2015, 3:22 PM

Post #1 of 11 (3678 views)
undefined subroutine of sum? Can't Post

I want to ask that how to deal with this error? I thought sum() is a built-in function of perl. Thanks for your help.

@array1 = (1,2,3,4,5);
$sum1 = sum( @array1 );

Undefined subroutine &main::sum called at script.pl line xx.


aaron_baugher
Novice

Jun 5, 2015, 5:40 PM

Post #2 of 11 (3674 views)
Re: [rockcone01] undefined subroutine of sum? [In reply to] Can't Post

That error message is telling you that sum() is not a built-in function. You can write it yourself, or use a module which provides it, such as List::Util.


Code
sub sum { 
my $t=0;
$t+=$_ for @_;
return $t;
}



rockcone01
Novice

Jun 6, 2015, 8:58 AM

Post #3 of 11 (3666 views)
Re: [aaron_baugher] undefined subroutine of sum? [In reply to] Can't Post

Thanks for your reply, but it still has problems.
I typed the code you wrote, and it has the warnings:
Use of uninitialized value $_ in addition (+) at PATH.pl line 10.
What is the problem?


Laurent_R
Veteran / Moderator

Jun 6, 2015, 10:43 AM

Post #4 of 11 (3663 views)
Re: [rockcone01] undefined subroutine of sum? [In reply to] Can't Post

The code posted by aaron_baugher should not give this warning. I guess you made a mistake copying it. Please post the code that you now have (not only the sub, but also the code calling that sub.


(This post was edited by Laurent_R on Jun 6, 2015, 10:44 AM)


rockcone01
Novice

Jun 7, 2015, 7:42 PM

Post #5 of 11 (3643 views)
Re: [Laurent_R] undefined subroutine of sum? [In reply to] Can't Post

I insert the code above to a longer code/process. I think it may be of a bug in the other code.


Laurent_R
Veteran / Moderator

Jun 7, 2015, 11:15 PM

Post #6 of 11 (3641 views)
Re: [rockcone01] undefined subroutine of sum? [In reply to] Can't Post


In Reply To
I insert the code above to a longer code/process. I think it may be of a bug in the other code.


Where?


aaron_baugher
Novice

Jun 8, 2015, 8:34 AM

Post #7 of 11 (3631 views)
Re: [rockcone01] undefined subroutine of sum? [In reply to] Can't Post

It would be rather strange for code outside that subroutine to cause such an error inside it, since all the variables are lexically scoped to the subroutine, meaning that they can't be affected by global variables of the same name defined elsewhere.

I suppose it's possible that a syntax error elsewhere could cascade down to cause it, but I'd think it's more likely to be a typo in copying it over.


rockcone01
Novice

Jun 16, 2015, 12:56 PM

Post #8 of 11 (3383 views)
Re: [aaron_baugher] undefined subroutine of sum? [In reply to] Can't Post

I typed this code to let the sum function work:


Code
sub sum { 
my $num = 0;
$num += $_ for @_;
return $num;
}

But it gives the error:

"my" variable $num masks earlier declaration in same statement at ./test.pl line 50.
Use of my $_ is experimental at ./test.pl line 50.
Syntax error...

What is the problem?

Whenever there is an addition (+) or a subtraction (-) in my code, it says uninitialized, is there some function recall I need to do to let them work? Thanks for your help.


Laurent_R
Veteran / Moderator

Jun 16, 2015, 3:26 PM

Post #9 of 11 (3376 views)
Re: [rockcone01] undefined subroutine of sum? [In reply to] Can't Post

This error is typical of an error having taken place earlier in your code (sometimes much earlier).

It is usually the absence of a closing parenthesis, closing curly, closing square bracket, closing quote mark or apostrophe, earlier in the code, or sometimes also forgetting the lack of a ";" at the end of a code line.

Either you find out by yourself, or you need to provide the code before.


rockcone01
Novice

Jun 16, 2015, 3:40 PM

Post #10 of 11 (3374 views)
Re: [Laurent_R] undefined subroutine of sum? [In reply to] Can't Post

Thanks for your reply. I want to ask that does spaces after a function very important?


Laurent_R
Veteran / Moderator

Jun 17, 2015, 4:25 AM

Post #11 of 11 (3356 views)
Re: [rockcone01] undefined subroutine of sum? [In reply to] Can't Post

Not sure of what exactly yoiu mean, but spaces are usually not important in Perl.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives