Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl skipping a print call? Sub-nested lexicals?

Status
Not open for further replies.

samgardner

Programmer
Sep 11, 2003
4
0
0
US
I'm going through the exercises in Llama, and have come on an odd phenomenon. The code seems to be completely skipping the first print call. However the first function call to &total succeeds (I know because my previous code hadn't set the value of $total to zero at the start of the sub, and it added all the values 1 to 1000 plus whatever the user had entered). Here's my code:

#!perl -w

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

print "type a list of numbers on separate lines\n";
chomp(@nums=<STDIN>);
$return = &total(@nums);
print &quot;the returned value is $return&quot;.&quot;\n&quot;;
$return=&total(1..1000);
print &quot;the sum of 1 to 1000 is $return&quot;.&quot;\n&quot;;


The output is like:
type a list of numberson separate lines
2
3
4
3
4
3
the sum of 1 to 1000 is 500500

If I don't set the value of $total to zero, the last line is &quot;the sum of 1 to 1000 is 500519&quot;

1: I don't understand why it's skipping the first print call. Does anyone?

2: I don't know how to properly get the $total variable to be treated as a lexical variable. If I use the my keyword, it will apply to the variable within the foreach loop and thus return undef. I had been under the impression that sub variables were lexical anyway, but that seems to be mistaken.

Appreciate any help/advice;

s

 
I copied and pasted your code exactly. It would return both print statements for me every time.

Then I add a my $total to the sub routine and it cleared up the cumulative addition in the second print statement. It did not return undef.

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

print &quot;type a list of numbers on separate lines\n&quot;;
chomp(@nums=<STDIN>);
$return = &total(@nums);
print &quot;the returned value is $return&quot;.&quot;\n&quot;;
$return=&total(1..1000);
print &quot;the sum of 1 to 1000 is $return&quot;.&quot;\n&quot;;
 
Thanks, at least I know I'm not going nuts. . . well, maybe I know that. . .

btw, I've also been having a smaller issue with XP not correctly recognizing the Ctrl-Z as the end-of-input -- it's printing out ^Z. It seems to do this sporadically; sometimes if I press Ctrl-C afterwards it proceeds with the program and sometimes it dumps with SIGINT. This seems more like an XP thing than a Perl thing, but do you know why, or any fix to get this to work? (of course, I COULD read the input for a bare newline and end processing programmatically there. . .)

Thanks again

Sam
 
Never mind, figured out that XP makes you hit the return key after Ctrl-Z
 
I have found that ctrl+z has to be on its own line followed by a <cr>.

Example:

Please enter numbers, one to a line: 1 <cr>
2 <cr>
3 <cr>
4 <cr>
5 <cr>
^Z <cr>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top