in reply to Syntax error - "my" within foreach loop

And just in case your next question is something on the order of "Why doesn't this DWIM?, consider that you've provided no mechanism for your script to know what marks the end of your input.

chomp(my @words = <STDIN>);

...has no control mechanism to terminate the input and thus, as it stands, it runs out of control on the CLI or under debug.

As this smells like more homework (label it as such, if so), the solution of that (very basic) bit of logic is left to the student.

Replies are listed 'Best First'.
Re^2: Syntax error - "my" within foreach loop
by FunkyMonk (Bishop) on Apr 13, 2008 at 22:43 UTC
    has no control mechanism to terminate the input
    ^D worked for me :)

      True, but better for the student to learn the importance of prompting the user clearly, and explicitly... AND to learn first to code using explicit constructs, rather than relying on implicit (and OS-dependant) ones.

      For example, lusers could still screw this up, but at least the program's author would have tried:

      ... print "Type a series of words, separated by spaces, ending with a retu +rn:\n"; my $words = <STDIN>; chomp $words; my @words = split (/ /, $words); ...