in reply to Re: Opening a file, editing, copying to another
in thread Opening a file, editing, copying to another

That line does nothing. If you want to write to your file you would do

Actually, no. Consider:

$_ = "HW"; s/HW/Hello World/; print STDOUT;

Prints:

Hello World

You perhaps need to learn about $_? See $ARG/$_ in perlvar.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^3: Opening a file, editing, copying to another
by toolic (Bishop) on Jun 20, 2007 at 23:37 UTC
    Just to be extremely explict, "print STDOUT;" is the same as "print STDOUT $_;". Both of these statements print the value of the special Perl variable, $_, to STDOUT. Similarly, "print OTHER;", which is the same as "print OTHER $_;", prints the value of $_ to the "OTHER" file.

      Actually

      print;

      suffices. STDOUT is assumed if no file handle is provided. However I used print STDOUT; in the sample code to maintain consistency with the OP's sample code.

      The important thing for Grundle is to be aware of $_ and where it is used as the default.


      DWIM is Perl's answer to Gödel
Re^3: Opening a file, editing, copying to another
by Grundle (Scribe) on Jun 21, 2007 at 13:57 UTC
    Ahh ++ I wasn't aware of that. That seems to be a sloppy way of doing things in my mind, even though it is a quicker shorthand.

    In any case I do not see where $_ is ever being set. Running the code myself it comes up empty for every iteration. The data that he wants to operate on is stored in his 2-D Array $list[][]. It makes complete sense that nothing is sent to his files because $_ is empty.

    Thanks for pointing out my mistake ;) Although I know about $_ I rarely use it. I find it more readable to explicitely name my variables, and it aids in maintainability for future changes.

      You have to know about and use $_ if you use map, grep or for (as a statement modifier) and for various common Perl idioms. If you are not using map, grep or statement modifiers then you are missing out on some major workhorses in Perl.

      In the op's situation I agree that using a variable is likely to make the intent clearer, but there are a multitude of problems with the code apart from that.


      DWIM is Perl's answer to Gödel