in reply to opening file for editing

> is open for write
<HAN> is read a line from file
This opening is not for read-write mode.
consider a "+>" See: perldoc -f open

Replies are listed 'Best First'.
Re^2: opening file for editing
by marscld (Beadle) on Oct 09, 2011 at 07:29 UTC
    using ">>" to append context into a file.
Re^2: opening file for editing
by ramprasad27 (Sexton) on Oct 09, 2011 at 08:08 UTC
    well, why is the file going blank if i use > even though the file exists and has contents

      well, why is the file going blank if i use > even though the file exists and has contents

      Because that is what > means, see open

        you can understand in this way:
        The file name here is where you save your work. 
        ( you use close() to save your file )
        So it doesn't matter the file has data inside or not. 
        ONLY the data in the handle will write to your file.
        ie. This ">" is an overwrite, if you are opening an existed file
        
        So, since you are open a file for WRITE, so it couldn't READ, while you read
        nothing, so you write nothing. End up, you harvest an empty file
        

      ">" and ">>" are pipelines.

      For Perl and bash (one of the linux shells) ">" is the standard way to say: "redirect this stuff to the named file and overwrite (delete) its previous contents with this".

      And ">>" is the standard way to say: "add this material after the last line of the file (updating this contents) but preserve any previous contents that you found in this file". ">>" works also in perl and in bash

      If the file is missing both do basically the same. Create a "box" (the file) and dump all the "bricks" into this box.

      So, let's see what are you doing:

      open (HAN , ">" ,'D:\RAM\perl\test') or die "$!"; # DELETE the contents of this file and open it while (<HAN>){ s/e/\*\*/ig; # nothing here to mach, the file is empty, no survivor "e" chars print HAN ; # thus, nothing is changed and nothing left to print to the file }