develooper Front page | perl.perl5.porters | Postings from June 2009

Re: improving $! usability (using autodie and readline)

Thread Previous | Thread Next
From:
Paul Fenwick
Date:
June 23, 2009 00:53
Subject:
Re: improving $! usability (using autodie and readline)
Message ID:
4A4089FE.6040903@perltraining.com.au
G'day Schwern and friends,

Michael G Schwern wrote:

> This might be something autodie can do.  Hmm.

I would *love* to have autodie wrap readline()/<>, but my previous attempts
to do so have been unsuccessful.  It has a prototype, so this *should* work,
but maybe I'm just not trying hard enough.

Having said that, I just tried:

	perl -MO=Deparse -Mautodie=readline -e'while (<STDIN>) { print };'

and got:

	use autodie (split(/,/, 'readline', 0));
	BEGIN {
	    $^H{'guard Fatal'} = q(ARRAY(0x12d4a60));
	}
	while (&readline(STDIN)) {
	    print $_;
	}
	-e syntax OK

That reveals that it's able to catch readline (so clearly I haven't been
trying hard enough), but it loses its magic ability of setting $_ inside a
while loop.  I would have expected the deparsed output to be:

	while( defined($_ = &readline(STDIN) ) ) {
	    print $_;
	}

While one could argue that's a bug, fixing for newer versions of Perl won't
help the existing 5.8/5.10 install base upon which autodie presently works.
 If someone can give me pure-perl magic that allows me to detect if
readline() is being called inside a while loop, then I'd appreciate it, as I
can't think of any way off the top of my head.

Cheerio,

	Paul

-- 
Paul Fenwick <pjf@perltraining.com.au> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681

Thread Previous | 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