develooper Front page | perl.perl5.porters | Postings from August 2008

Proof of concept code for doing magic ARGV processing in perl

Thread Next
From:
Ed Avis
Date:
August 2, 2008 07:13
Subject:
Proof of concept code for doing magic ARGV processing in perl
Message ID:
loom.20080802T135557-446@post.gmane.org
I suggested that instead of continuing to use perl's builtin special handling of
the ARGV filehandle, it would be better to implement magical arg processing in
perl, for the following reasons:

- Even if you do want magic, the existing behaviour of <> is not ideal.  It
shouldn't end up writing files instead of reading them.  A reimplementation in
perl code can more easily catch and warn about these cases.

- It will be easy to add more magic, such as automatically fetching URIs, which
many people want.

- The current implementation of <ARGV> is just an override of readline.  You
cannot pass the ARGV filehandle to subroutines.  A real tied filehandle will
remove this limitation.

- It would be nice to construct a magic-reader filehandle from an arbitrary list
of files, not just @ARGV.

I have implemented a simple tied filehandle and package that lets you say

% perl -Mdiamond -e 'foreach (<MAGIC_ARGV>) { print }' file1 '<file2' 'echo
hello |' http://perl.com/

Compared with builtin <ARGV> it has the advantages listed above, and also
incorporates tchrist's suggestion to check for file existence when deciding
whether to use magic or not.  So while it is still not completely correct when
passed filenames that contain special characters (consider race conditions), it
will be good enough for casual use in pretty much all cases.

You can see the code at <http://membled.com/work/perl/diamond.pm>.  I have not
yet tested all the filehandle functions, just READLINE.

-- 
Ed Avis <eda@waniasset.com>


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About