Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^2: opening file for editing

by ramprasad27 (Sexton)
on Oct 09, 2011 at 08:08 UTC ( #930434=note: print w/replies, xml ) Need Help??

in reply to Re: opening file for editing
in thread opening file for editing

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

Replies are listed 'Best First'.
Re^3: opening file for editing
by Anonymous Monk on Oct 09, 2011 at 08:36 UTC

    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
        ok i got it, i just wanted to know any way i can open already existing file change it on the fly
Re^3: opening file for editing
by pvaldes (Chaplain) on Oct 09, 2011 at 08:48 UTC

    ">" 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 }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930434]
[shmem]: J.S.Bach said: "playing Clavier is darn easy. All you have to do is to push the right key with the right finger at the right time."
[shmem]: same for programming and creating bugs.
[choroba]: programming is easy once you know how to solve the problem
[shmem]: choroba: yeah, adjusting the screw 2 - knowing which one 42
[shmem]: and - on another side: a complete spec is identical to the programs source code

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2017-04-27 14:33 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (508 votes). Check out past polls.