What's this error? Use of uninitialized value in concatenation (.) or string

Can anyone tell me why I get the following error message when running this script?

Use of uninitialized value in concatenation (.) or string at line 15.

#!/usr/bin/perl -w
  
  use strict;
  
  sub total
  {
  	foreach(@_)
  	{
  		my $total += $_;
  	}
  	return;
  }
  
  my $grand_total = &total(1..1000);
  print "$grand_total \
";

the warning is refering to this line:

print "$grand_total
";

$grand_total has no value (it’s uninitialized) . Your code is not written correctly, as you may have noticed since it prints nothing when run.

this line is properly coded:

my $grand_total = &total(1…1000);

you can leave off the & sign though for cleaner looking code:

my $grand_total = total(1…1000);

the problem begins in sub total(). You are using strict (a good thing!) so $total is not visible outside the sub and you never pass it back to the calling routine, you just have “return;” at the end of the sub, which returns nothing (sort of). But you also have:

my $total += $_;

inside the foreach loop. That is also not correct. $total can’t be seen outside the foreach loop, which is a block:


foreach() {
   block of code here
}

this is a scoping problem. Variables only are scoped (visible) to the block they are in when use strict is in place (as it should be).

so you have a scoping problem as well as a problem I’m not sure how it’s called. You can’t say:

my $total += $_;

at least not the way you have it. $total will be redefined each time the loop is run and end up with the last value of $_.

Pass your variable to the sub routine (your list of numbers in this case), initiate $total before the foreach block so it’s scope will also be outside the foreach block but not outside the sub rotuine block, and return the value back to the calling routine:

  sub total
  {
   my $total = 0;
  	foreach(@_)
  	{
  		$total += $_;
  	}
  	return($total);
  }

  my $grand_total = total(1..1000);
  print "$grand_total \
";

or more succintly:

  sub total
  {
   my $total = 0;
   $total+=$_ for @_;
   return($total);
  }

  my $grand_total = total(1..1000);
  print "$grand_total \
";  

#!/usr/local/bin/perl -l

use warnings;
use strict;

sub total { eval eval join ‘+’ => @_ }

print total (1…5)

that is niffty code but you have to admit to the casual perl user it’s probably going to be incomprehensible. If that was for my benefit I aprreciate it, if it’s for the benefit of theGWS I have a feeling it’s just voodo to him/her at this point, they are still battling with passing arguments and variable scoping.

hi there im getting a error kinda like that also

use warnings;
use strict;
use CGI;

my $script_URL = “http://zephir.seattlecentral.edu/~ajohns30”;
my $q= new CGI;
print<<END_OF_HTML;
Content-type:text/html
<html>
<head>
<title>
</title>
</head>
<body>
END_OF_HTML
my $name = $q->param(“Name”);
my $phone = $q->param(“Phone”);
my $zip = $q->param(“Zip”);
print<<END_OF_HTML;
<p>
The following information has been submitted:
<br/>
The name is $name </p>
<p>
The phone number is $phone </p>
<p>
The zip code is $zip </p>
</body>
</html>
END_OF_HTML

sub showform {
print "<form action=$script_URL method=\“get\”>
“;
print “Name: <INPUT TYPE=text name=name size=25 value=\”$name\”>
“;
print “Phone: <INPUT TYPE=text name=phone size=12 value=\”$phone\”>
“;
print “Zip: <INPUT TYPE=text name=zip size=5 value=\”$zip\”>
";
print "<INPUT TYPE=submit value=\“SUBMIT\”>
";
print "<INPUT TYPE=reset value=\“RESET\”>
";
if ($name =~ /[1]\w$/i) {
print "All of the data has been formatted correctly.
"
}else {
print "The data in the name field is not formatted correctly.
"
}
if ($phone =~ /[2]\w$/i) {
print "All of the data has been formatted correctly.
"
}else {
print "The data in the phone field is not formatted correctly.
"
}
if ($zip =~ /[3]\w$/i) {
print "All of the data has been formatted correctly.
"
}else {
print "The data in the zip code field is not formatted correctly…
"
}

}

im getting the error at line 22…can someone pls help me pls…thanks
you can email me...maynard48@comcast.net

great board


  1. A-Z_ ↩︎

  2. 0-9_ ↩︎

  3. 0-9_ ↩︎

As for maynard49’s code, you need another newline after the content type, like this:

print<<END_OF_HTML;
Content-type:text/html

<html>
<head>
<title>
</title>
</head>
<body>
END_OF_HTML