develooper Front page | perl.perl5.porters | Postings from December 2011

[perl #31422] can't write open() wrapper without affecting how the FH is parsed

Thread Previous | Thread Next
From:
James E Keenan via RT
Date:
December 6, 2011 19:08
Subject:
[perl #31422] can't write open() wrapper without affecting how the FH is parsed
Message ID:
rt-3.6.HEAD-5084-1323227291-1810.31422-15-0@perl.org
On Wed Sep 01 10:29:40 2004, andrew@pimlott.net wrote:
> 
>
> 
> I tried to write a wrapper for open:
> 
>     sub myopen (*$$) {
>         my ($fh, $mode, $file) = @_;
>         open($fh, $mode, $file)
>     }
> 
> However, using it has unexpected and subtle side effects on how the
>    opened
> filehandle is interpreted by the parser.
> 
>     myopen OUT, '>', 'out' or die $!;
>     print OUT "hey\n";
>     close OUT;
> 
> works fine.  But
> 
>     sub FOO::bar { "hey\n" }
>     myopen OUT, '>', 'out' or die $!;
>     print OUT FOO->bar;
>     close OUT;
> 
> gives Can't locate object method "OUT" via package "FOO", and with -w
>    also
> gives Name "main::OUT" used only once: possible typo.  This code works
>    fine
> with a normal open.
> [snip]

I believe the program attached, 31442.pl, reproduces your sample code. 
However, when I run it I don't reproduce either the failure or the
warning you cite.

(Of course, when I try to run this under 'use strict;', I start to
encounter problems.  But, then again, I don't understand the need to
write a wrapper for 'open'.  And it's been a long time since I wrote a
program with global filehandles rather than lexically scoped filehandles.)

Thank you very much.
Jim Keenan


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