Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

RE: File Input and Output

by SYbeginner (Novice)
on Jun 17, 2000 at 07:48 UTC ( #18603=note: print w/ replies, xml ) Need Help??


in reply to File Input and Output

Also if a file doesn't exisit the following code will generate the file on the server although I can't figure out why:

open(file, ">$filename") flock file, 2; print file "Whatever you want to say"; close(file);
And after that it generates a file on the server, could someone explain it for me?


Comment on RE: File Input and Output
Download Code
RE: RE: File Input and Output
by merlyn (Sage) on Jun 17, 2000 at 08:09 UTC
    It doesn't do much good to flock a file that you've already erased. Perhaps you are mixing things from different stuff you've seen in some sort of "opcode soup" that you hope will taste good once you're done.

    To me, it's bad from the first taste. :)

    -- Randal L. Schwartz, Perl hacker

Re: RE: File Input and Output
by gpoduval (Initiate) on Jan 30, 2001 at 11:33 UTC
    of course it will create a new file. since you have opened it in write mode..it will open a new file and write data into it. if the file had already existed it would have removed the whole existing data and just put in data you just wrote. to prevent this open in >> mode
      It occurs to me that this fact might not be intuitively obvious to someone that isn't already used to that behavior, as for instance from regular use of bash. It's intuitively obvious to me, but I'm a penguinista. Those who write Perl but use Windows, for instance, wouldn't be used to that kind of behavior. Remember that in the Windows GUI environment files never get named until they're saved after editing. Habitual Windows users probably never realize that what's going on is that the file is actually only being "opened" for the first time when that "okay" button in the save dialog is clicked.

      So: For those of us used to that sort of behavior (like 'nix users), it's "Of course it will create a new file." For those who are not (like Windows users), it's "Oh. That's news to me."

      The fact that one must use >> instead of > to edit an already extant file without wiping out its contents is an important fact, though, and it's a good thing you brought it up. It should really be included in the text of the tutorial itself.

      - apotheon

      CopyWrite Chad Perrin
        Sorry it took me three years to make this note, but people who've used DOS or the Windows command line and worked with redirection have used the same > and >> that bash uses. It's just the GUI-only folks who wouldn't know this.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://18603]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-12-21 09:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls