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

Could you tell me why this code doesn't work then? I thought I had the right syntax.
$file = 'electricity.txt'; open(INFO, $file); @lines = <INFO>; close(INFO); $counter = 1; foreach $line (@lines) { if ($line =~ /$ARGV[0]/) { print "$counter "; $_ = $line; s/$ARGV[0]/($ARGV[0])/g; print $line; $counter++; } else { print $line; } }

Replies are listed 'Best First'.
Re^5: The $_ Special Variable
by wind (Priest) on Jun 21, 2011 at 18:20 UTC

    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;
      What do the strict and warnings commands do? And what is the purpose of my before the objects? What does it do? Also, what is the purpose of my $string = shift;? I don't understand the if (s/\Q$string\E/($string)/g) { conditional either. Thank you for all of your help
        What do the strict and warnings commands do?

        use strict requires that all variables be declared with either my or our. This is the number one thing you can do to ensure you're code is well made and without errors. use warnings is essentially an extension that monitors your code for unintended errors.

        And what is the purpose of my before the objects? What does it do?

        First of all, those aren't objects, they're simple variables. my is used to declare your variables and is useful for limiting their scope. If you were to mistype a variable or use the wrong type, use strict would alert you to the error

        Also, what is the purpose of my $string = shift;?

        If you read the documentation for shift, you'll see that in this instance it's equivalent to:

        my $string = shift @ARGV;

        Previously, you hardcoded $ARGV[0] in multiple places. It's much better to assign your script's parameters to variables as a way of documenting your code.

        I don't understand the if (s/\Q$string\E/($string)/g) { conditional either.

        \Q...\E is a shortcut syntax for quotemeta. This escaped any regex special characters in your $string, so that it would be treated as a literal value.

        With questions like these, perhaps the most general answer is...
        Learning Perl

        readily available (USD 30, +//-) from vendors who include the publisher, O'Reilly, and such esteemed booksellers as Amazon, Walmart, and -- very likely -- your local independent bookstore.

        Update/afterthought: your OP refers to the symbol pair 'questionmark''underscore'... and so do your further responses when Monks offering help plainly refer to the pair 'dollarsign''underscore'... which is not at all the same thing.
        Does your local keyboard omit the "$" sign?

      Oh, duh. Thanks. I can't believe I overlooked that.