For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > Re: Changing the String of the file at one shot.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Changing the String of the file at one shot.
David Storrs

2005-03-08, 8:56 am

On Mon, Mar 07, 2005 at 11:00:49AM -0500, Wiggins d'Anconia wrote:
> Charles K. Clarkson wrote:
>
> or Tie::File,
>
> perldoc Tie::File
>
> http://danconia.org


The literal answer to your question would be this:

open FILE, '/path/to/the/file', '+<'
or die "Couldn't open: $!";

or, preferably (as it will manage some safety issues for you):

use IO::File;
my $file = new IO::File('/path/to/the/file', '+<')
or die "Couldn't open: $!";

This isn't a great solution for updating text files, however, since
they have variable-length records (e.g., you overwrite a 10-character
sentence with 12 characters and you have just stomped on the next 2
characters).

One of the above solutions (-i or Tie::File) will stand you in better
stead.

--Dks

--
dstorrs@dstorrs.com
John Doe

2005-03-08, 8:56 am

Hello

see inline comment

Am Dienstag, 8. M=E4rz 2005 02.04 schrieb David Storrs:
> On Mon, Mar 07, 2005 at 11:00:49AM -0500, Wiggins d'Anconia wrote:
>
> The literal answer to your question would be this:
>
> open FILE, '/path/to/the/file', '+<'
> or die "Couldn't open: $!";


open FILE, '+<', '/path/to/the/file' or die "Couldn't open: $!";

(see order of arguments)

perldoc -f open

> or, preferably (as it will manage some safety issues for you):
>
> use IO::File;
> my $file =3D new IO::File('/path/to/the/file', '+<')
> or die "Couldn't open: $!";
>
> This isn't a great solution for updating text files, however, since
> they have variable-length records (e.g., you overwrite a 10-character
> sentence with 12 characters and you have just stomped on the next 2
> characters).
>
> One of the above solutions (-i or Tie::File) will stand you in better
> stead.
>
> --Dks
>
> --
> dstorrs@dstorrs.com

David Storrs

2005-03-08, 3:57 pm

On Tue, Mar 08, 2005 at 12:02:09PM +0100, John Doe wrote:
> Am Dienstag, 8. März 2005 02.04 schrieb David Storrs:
[color=darkred]
>
> open FILE, '+<', '/path/to/the/file' or die "Couldn't open: $!";
>
> (see order of arguments)
>
> perldoc -f open


Sorry, yes. I had originally used the 'IO::File' version there, for
which that is the correct order. When I changed it to the bare open
call, I forgot to swap them. This difference in the order is
something that has tripped me a lot.
[color=darkred]


--Dks
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com