You are executing the shell command
wc -l $file
The shell has no variable $file
defined, so that's the same as
wc -l
This causes the shell to execute wc
with the lone arg -l
. With no file name provided, wc
in turn reads from STDIN until you kill it with SIGINT from Ctrl-C.
You were perhaps aiming for
system("wc -l $file"); # XXX
but that's wrong too. That doesn't pass the args -l
and the value of $file
to wc
. Consider what would happen if a file name with a space in it was provided.
To build a shell literal that results in the correct file name, you could use
use String::ShellQuote qw( shell_quote );
system(shell_quote('wc', '-l', $file));
But a better option is to avoid the shell and execute wc
directly, passing to it the values you want without having to build a shell command.
system('wc', '-l', $file);