Re: perl

[prev] [thread] [next] [lurker] [Date index for 2006/12/22]

From: Abigail
Subject: Re: perl
Date: 13:27 on 22 Dec 2006
--3FyYKcuUbgqNYeqV
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Dec 22, 2006 at 07:09:44AM -0600, Peter da Silva wrote:
> >You know, in Perl, the equivalent of the C snippet would just be:
> >
> >    do_some_process;
> >
> >while in do_some_process, you'd use the '<>' to read input. Perl has=20
> >the
> >snippet build in - if there are no arguments, '<>' will read from the
> >file(s) given as arguments.
>=20
> Yep, Perl is full of rigidly defined areas of doubt and uncertainty=20
> like that. The fix to pointlessly obscure syntax is to introduce more=20
> obscure syntax that handles some common cases that the pointlessly=20
> obscure syntax makes difficult... without actually doing anything about=
=20
> the fact that you have to use
>=20
> do_some_process(STDIN);
>=20
> but
>=20
> do_some_process(*INPUT);
>=20
> when you suddenly realize that you need to handle files ending with=20
> ".o" differently from files ending in ".a" and by the way doesn't that=20
> mean that file boundaries actually matter now...?


You want to know whether you are at a file boundary? Use eof().
You want to know the name of the current file? Use $ARGV; perl will
set it for you. (And as a previous poster mentioned, $ARGV is only set
when reading from files. Which means that if it's not set, you aren't
reading from a file - but from standard input. And yes, if you use '-'
as a filename argument, Perl will read from STDIN, just like most of the
other Unix tools. Want to read from a pipe as well? Supply 'someprogram
|' as the file name, and Perl will take care of setting up the pipe for
you. I always thought that magical open was only hated by people not
understanding Unix, but appears it's not so).

And yes, it's hateful that filehandles aren't first class
objects. However, while can't get rid of the hateful syntax for backwards
compatability reasons, one doesn't need barefiles very often. See, in
the last century people agreed that the filehandle syntax is hateful,
so they made it that primitives that take filehandles are argument can
also take a reference to a filehandle as argument, and all will work well.

    open my $filehandle, "<", "/etc/passwd" or die;
    while (<$filehandle>) {print}
    close $filehandle;

works perfectly fine.

And if you don't like <>, use 'readline'.=20


Abigail

--3FyYKcuUbgqNYeqV
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFFi91bBOh7Ggo6rasRAux/AJ0QmL+LrvZH2xm9TVGdZkV/RDK4vACeJaNc
yOGB7kAcHUzj8zlMAK4+OV0=
=wYvh
-----END PGP SIGNATURE-----

--3FyYKcuUbgqNYeqV--
There's stuff above here

Generated at 14:01 on 28 Dec 2006 by mariachi 0.52