PFR: read_file, write_file 
Author Message
 PFR: read_file, write_file

BTW i never saw the original announce of this. it think it is great.

here are 2 little favorites of mine which seem to get asked for by all
newbies. read a file as a list or a single string and write a file from
a list or string.

they are so simple i didn't comment them. also they need to use Carp but
that can be changed by the end user. they also don't use the IO::
modules so they are quicker and don't need to load lots of code.

enjoy,

uri

# utility sub to read a file in.
# it can return a single string or a list of lines

use Carp ;

sub read_file {



        local( *FH ) ;

        open( FH, $file_name ) || croak "can't open '$file_name' $!" ;

        if ( wantarray ) {


                close( FH ) ;


        }

        local( $/ ) ;

        $text = <FH> ;

        close( FH ) ;

        return( $text ) ;

Quote:
}

# utility sub to write a file file. takes a file name and
# single string or a list of lines/strings

sub write_file {

        my( $file_name ) = shift ;

        local( *FH ) ;

        open( FH, ">$file_name" ) || croak "can't create $file_name $!" ;


        close( FH ) ;

Quote:
}

--
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire  ----------------------  Perl, Internet, UNIX Consulting

The Best Search Engine on the Net -------------   http://www.*-*-*.com/


Tue, 31 Jul 2001 03:00:00 GMT  
 PFR: read_file, write_file
[A complimentary Cc of this posting was sent to Uri Guttman


Quote:
> sub read_file {

[...]

Quote:
>    local( $/ ) ;
>    $text = <FH> ;
>    close( FH ) ;
>    return( $text ) ;
> }

Due to a bug in <FH>, this returns undef on empty files.  While we are
waiting for this bug to be fixed, you may want to fix your function.

Ilya



Tue, 31 Jul 2001 03:00:00 GMT  
 PFR: read_file, write_file

Quote:

>[A complimentary Cc of this posting was sent to Uri Guttman


>> sub read_file {

>[...]

>>       local( $/ ) ;
>>       $text = <FH> ;
>>       close( FH ) ;
>>       return( $text ) ;
>> }

>Due to a bug in <FH>, this returns undef on empty files.  While we are
>waiting for this bug to be fixed, you may want to fix your function.

Isn't an undef on an empty file what is wanted?

--

%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])



Tue, 31 Jul 2001 03:00:00 GMT  
 PFR: read_file, write_file


Quote:


...
> >Due to a bug in <FH>, this returns undef on empty files.  While we are
> >waiting for this bug to be fixed, you may want to fix your function.

> Isn't an undef on an empty file what is wanted?

I would want undef on failure to open the file, and an empty string if
the file is empty.  

  return defined $text ? $text || "";

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/



Tue, 31 Jul 2001 03:00:00 GMT  
 PFR: read_file, write_file
[A complimentary Cc of this posting was sent to Chris Nandor


Quote:
> >Due to a bug in <FH>, this returns undef on empty files.  While we are
> >waiting for this bug to be fixed, you may want to fix your function.

> Isn't an undef on an empty file what is wanted?

Why?  The contents of an empty file is "", as any cat will tell you.

At least now we can apply the RULE ONE to cut off the discussion.

Ilya



Tue, 31 Jul 2001 03:00:00 GMT  
 PFR: read_file, write_file

Quote:


> >Due to a bug in <FH>, this returns undef on empty files.

> Isn't an undef on an empty file what is wanted?

To me the empty string is the Right Thing for the empty file.

--

http://pobox.com/~jdf



Tue, 31 Jul 2001 03:00:00 GMT  
 PFR: read_file, write_file
[posted and mailed]

Quote:

> they are so simple i didn't comment them.

I don't think I like this.  Regardless of how trivial the code
is, I'd still like to have documented what it takes as arguments,
what it returns, and any gotchas associated.

Quote:
>                                             also they need to use Carp but
> that can be changed by the end user.

This I don't mind at all.  Also, to address a point in another message,
I don't have any problem with including XS code as well as `pure' perl.
As far as I'm concerned XS _is_ part of perl- it's just a different
part with different capabilities.

dgris
--





Wed, 01 Aug 2001 03:00:00 GMT  
 PFR: read_file, write_file

Quote:



>> >Due to a bug in <FH>, this returns undef on empty files.

>> Isn't an undef on an empty file what is wanted?

>To me the empty string is the Right Thing for the empty file.

Says you.  I can accept that you believe that, but I think undef is good.
The contents of the file are undefined.  There are no contents.

--

%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])



Wed, 01 Aug 2001 03:00:00 GMT  
 PFR: read_file, write_file


Quote:




> >> >Due to a bug in <FH>, this returns undef on empty files.

> >> Isn't an undef on an empty file what is wanted?

> >To me the empty string is the Right Thing for the empty file.

> Says you.  I can accept that you believe that, but I think undef is good.
> The contents of the file are undefined.  There are no contents.

#!/usr/local/bin/perl -w
use strict;

my $data = 'xyz';

open FH, '>foo' or die $!;
close FH;
open FH, '<foo' or die $!;
read FH, $data, 100;
close FH;
print "|$data|\n";
__END__

And $data is now the empty string, not undef.

The contents of the file *are* defined.  The contents are the empty
string.

It is true that, as Ilya states, <FH> returns undef on an empty file.  I
agree with him that that is a bug.

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 01 Aug 2001 03:00:00 GMT  
 PFR: read_file, write_file

Quote:

> >To me the empty string is the Right Thing for the empty file.

> Says you.  I can accept that you believe that, but I think undef is good.
> The contents of the file are undefined.  There are no contents.

I think I would have to disagree there. If the file did not exist then
I could accept undef, but if the file is empty then its contents are
defined to be nothing.

I would expect

open F,">out";
print F read_file("input");
close(F);

to basically perform a copy if the fine "in" as "out" and also be -w clean

--

Life would be so much easier if we could just look at the source code.



Wed, 01 Aug 2001 03:00:00 GMT  
 PFR: read_file, write_file

Quote:


> >To me the empty string is the Right Thing for the empty file.

> Says you.  I can accept that you believe that, but I think undef is good.
> The contents of the file are undefined.  There are no contents.

Yes, says me.  What part of "To me" did you fail to understand?

--

http://pobox.com/~jdf



Wed, 01 Aug 2001 03:00:00 GMT  
 PFR: read_file, write_file

Quote:

> Also, to address a point in another message,
> I don't have any problem with including XS code as well as `pure' perl.
> As far as I'm concerned XS _is_ part of perl- it's just a different
> part with different capabilities.

XS is a valuable tool, but remember the tens of millions of machines
(most PCs running Windows NT, 95 or 98 and probably others) that are
capable of running Perl but don't have C compilers.  It takes five
minutes to instal Perl on a Win32 box using ActiveState's Win32 port.
If a Win32 user has to find GNU C, download it over a modem, work out
how to drive it and then compile your XS code, it's a racing certainty
that he'll just reinvent the wheel instead.  That's why XS modules for
Win32 generally come precompiled.

And I don't suppose Daniel Grisinger wants to get into compiling and
packaging binaries for Win32.

Markus

--
Delete the 'delete this bit' bit of my address to reply



Wed, 01 Aug 2001 03:00:00 GMT  
 PFR: read_file, write_file

  DG> [posted and mailed]

  >> they are so simple i didn't comment them.

  DG> I don't think I like this.  Regardless of how trivial the code
  DG> is, I'd still like to have documented what it takes as arguments,
  DG> what it returns, and any gotchas associated.

well, i (or you) can comment them. they are very trivial.

do you wnat a new posting or do we have a repository place where i send
them? are you going to put that into cpan as some have suggested?

uri

--
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire  ----------------------  Perl, Internet, UNIX Consulting

The Best Search Engine on the Net -------------  http://www.northernlight.com



Fri, 03 Aug 2001 03:00:00 GMT  
 
 [ 78 post ]  Go to page: [1] [2] [3] [4] [5] [6]

 Relevant Pages 

1. PFR: read_file, write_file

2. PFR: Compute offset of local timezone from UTC

3. PFR: Comput offset of local time from UTC

4. PFR: Convert engineering notation to arbitrary fixed-point format

5. PFR gone?

6. Makebak: script for PFR

7. PFR: TZname_to_TZoffset and TZoffset_to_TZname

8. ANNOUNCE: PFR Reopened :-)

9. PFR submission: type-ahead

10. ANNOUNCE: PFR Reopened :-)

11. PFR: columns

12. Is clp.mod appropriate for PFR's?

 

 
Powered by phpBB® Forum Software