http://www.perlmonks.org?node_id=910787


in reply to Re^4: The $_ Special Variable
in thread The $_ Special Variable

If you're talking about this section of the code:

$_ = $line; s/$ARGV[0]/($ARGV[0])/g; print $line;

It's because $line and $_ are two different variables. your s/// is changing $_ there, so that's what you'd need to print out if you wanted to see the change.

Note: I'd change a lot of things about your script, but starting with adding use strict and use warnings to the top. Here is a cleaned up version with strictures in place:

use strict; use warnings; my $string = shift; my $file = 'electricity.txt'; open my $fh, '<', $file or die $! my $counter = 0; while (<$fh>) { if (s/\Q$string\E/($string)/g) { $counter++; print "$counter "; } print; } close $fh;