comp.lang.perl.misc-recommends_io::filehandle_mod

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



From: "Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de>
Newsgroups: comp.lang.perl.misc
Subject: Re: What is the best way for file/stream IO in Perl?
Date: 3 Oct 2002 13:48:03 GMT

Also sprach Paul Dortman:

> I have a questions for Perl's guru.
> 
> What is the difference amoung IO::File, IO::Handle, FileHandle modules and
> standard operators and filehandles to access a file?

When dealing with files, the only two things you should have to worry
about is IO::File and using bare file handles. IO::Handle is one of the
base classes for IO::File. It defines common methods for all
file-handleish things. Even IO::Socket is actually derived from
IO::Handle.
So, IO::File just adds those things being unique to handles relating to
actual files.

As for FileHandle, I don't really see any benefits in using it. It is a
very thin wrapper around IO::File (also inheriting from it).

And there are file handles. One cool thing about IO::File and its
derivates is that they can be used like an ordinary filehandle:

    my $h = IO::File->new;
    $h->open("<file");
    while (<$h>) {
        ...
    }

That's one of Perl's trickeries that relies on the fact that HANDLE and
\*HANDLE basically behave the same in regards to some typical file
handle operations.

> What is the best way to access file and to deal with file?
> Where can I find more information on IO in Perl?

'the best' depends on the task. Most of the time I have been able to get
away using just ordinary plain handles. Also, since Perl 5.6.0 you no
longer have to worry about localizing your handles since you can do:

    open my $file, "file";

So a handle can behave very much like a lexical variable.

There are a number of spots where you find the desired information.

    perlop      ("I/O Operators")
    perlopentut (how to abuse open() to do funny things)

and the manpages for IO::File, IO::Handle and IO::Seekable. The methods
described therein usually exist for bare handles as Perl functions in
which case you would need to look up those in 'perlfunc'.

In case you are already using Perl 5.8.0, there is 'perldoc PerlIO::via'
that describes how to write IO layers that can act as filters for your
handles.

Tassilo
-- 
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;

Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!193.190.198.17!syros.belnet.be!news.belnet.be!ossa.telenet-ops.be!afrodite.telenet-ops.be.POSTED!not-for-mail
From: Bart Lateur <bart.lateur@pandora.be>
Newsgroups: comp.lang.perl.misc
Subject: Re: What is the best way for file/stream IO in Perl?
Organization: MediaMind
Message-ID: <qsuopuk9f3rgicelma79lmnen710lbncje@4ax.com>
References: <anh79e$gai$1@news.itfs.nsk.su> <anhhqj$oj3$1@nets3.rz.RWTH-Aachen.DE>
X-Newsreader: Forte Agent 1.92/32.572
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 10
Date: Thu, 03 Oct 2002 17:18:41 GMT
NNTP-Posting-Host: 213.118.242.134
X-Complaints-To: abuse@telenet.be
X-Trace: afrodite.telenet-ops.be 1033665521 213.118.242.134 (Thu, 03 Oct 2002 19:18:41 MET DST)
NNTP-Posting-Date: Thu, 03 Oct 2002 19:18:41 MET DST
Xref: shelby.stanford.edu comp.lang.perl.misc:459250

Tassilo v. Parseval wrote:

>As for FileHandle, I don't really see any benefits in using it. It is a
>very thin wrapper around IO::File (also inheriting from it).

I think FileHandle was there first. So its importance is most of all of
historical nature.

-- 
	Bart.

Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!128.39.3.168!uninett.no!newsfeed1.ulv.nextra.no!nextra.com!news2.ulv.nextra.no.POSTED!53ab2750!not-for-mail
Newsgroups: comp.lang.perl.misc
Subject: Re: What is the best way for file/stream IO in Perl?
References: <anh79e$gai$1@news.itfs.nsk.su> <anhhqj$oj3$1@nets3.rz.RWTH-Aachen.DE>
Organization: Private
From: pjacklam@online.no (Peter J. Acklam)
Message-ID: <d6qrbn7o.fsf@online.no>
User-Agent: Gnus/5.090008 (Oort Gnus v0.08) Emacs/21.2
 (i386-msvc-nt5.0.2195)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Lines: 20
Cancel-Lock: sha1:+xAHR7nASLhFgBdzW7qEPvoGlFE=
NNTP-Posting-Host: 130.67.129.152
X-Complaints-To: news-abuse@nextra.no
NNTP-Posting-Date: Thu, 03 Oct 2002 20:19:27 MEST
X-Trace: news2.ulv.nextra.no 1033669167 130.67.129.152
Date: Thu, 03 Oct 2002 18:19:27 GMT
Xref: shelby.stanford.edu comp.lang.perl.misc:459266

"Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de> wrote:

> One cool thing about IO::File and its derivates is that they can
> be used like an ordinary filehandle:
>
>     my $h = IO::File->new;
>     $h->open("<file");
>     while (<$h>) {
>         ...
>     }

No check on the output from open() is a bad thing, regardless of
whether IO::File, FileHandle, or the standard open() is used.  :-)

Peter

-- 
#!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
# matlab comment stripper (strips comments from Matlab m-files)
s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;

Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!195.54.122.107!newsfeed1.bredband.com!bredband!uio.no!193.213.112.26.MISMATCH!newsfeed1.ulv.nextra.no!nextra.com!news2.ulv.nextra.no.POSTED!53ab2750!not-for-mail
Newsgroups: comp.lang.perl.misc
Subject: Re: What is the best way for file/stream IO in Perl?
References: <anh79e$gai$1@news.itfs.nsk.su> <anhhqj$oj3$1@nets3.rz.RWTH-Aachen.DE>
 <d6qrbn7o.fsf@online.no> <ani8gn$che$1@nets3.rz.RWTH-Aachen.DE>
Organization: Private
From: pjacklam@online.no (Peter J. Acklam)
Message-ID: <lm5f6yih.fsf@online.no>
User-Agent: Gnus/5.090008 (Oort Gnus v0.08) Emacs/21.2
 (i386-msvc-nt5.0.2195)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Lines: 32
Cancel-Lock: sha1:TnShc4XTRicddzHahJT7RrvIzCI=
NNTP-Posting-Host: 130.67.129.152
X-Complaints-To: news-abuse@nextra.no
NNTP-Posting-Date: Thu, 03 Oct 2002 22:47:50 MEST
X-Trace: news2.ulv.nextra.no 1033678070 130.67.129.152
Date: Thu, 03 Oct 2002 20:47:50 GMT
Xref: shelby.stanford.edu comp.lang.perl.misc:459284

"Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de> wrote:

> Also sprach Peter J. Acklam:
> 
> > "Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de> wrote:
> > 
> > > my $h = IO::File->new;
> > > $h->open("<file");
> > > while (<$h> ) {
> > > ...
> > > }
> > 
> > No check on the output from open() is a bad thing, regardless of
> > whether IO::File, FileHandle, or the standard open() is used.  :-)
> 
> Grrr. See, each time I have to decide whether I bloat my skelleton
> examples with error-checking (and thus looking foolish, in a way) or
> avoid it for brevity and for focus on the significant stuff and thusly
> earn some rants. Dilemma, ain't it? :-)

I see your point.  :-)  I tend to do something like

    $h->open("<file") or die "...";

just to indicate that the value should be checked.

Peter

-- 
#!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
# matlab comment stripper (strips comments from Matlab m-files)
s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu