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

[perl #50756] open(FILEHANDLE, "<&=$fd") + dupping in memory files

From:
Bram via RT
Date:
June 29, 2008 10:56
Subject:
[perl #50756] open(FILEHANDLE, "<&=$fd") + dupping in memory files
Message ID:
rt-3.6.HEAD-6656-1214730667-256.50756-15-0@perl.org
On Tue Feb 12 08:49:02 2008, KES wrote:
>
> This is a bug report for perl from Eugen.Konkov@aldec.com,
> generated with the help of perlbug 1.35 running under perl v5.8.8.
> 
> 
> open my $ff, ">&", STDOUT or die "1: $!";
> open FH, ">&", $ff or die "2: $!";
> open F2, ">&", FH or die "3: $!";
> print F2 'hello';
> 
> This code outputs 'hello' to STDOUT;
> but on WWW: http://perldoc.perl.org/functions/open.html
> was wrote that I can use $ff
> 
> >    # open for input, reusing the fileno of $fd
> >    open(FILEHANDLE, "<&=$fd")
> >or
> >    open(FILEHANDLE, "<&=", $fd)
> I have tried to change my code to next:
> 
> open my $ff, ">&", STDOUT or die "1: $!";
> open FH, ">&$ff" or die "2: $!";
> open F2, ">&", FH or die "3: $!";
> print F2 'hello';
> 
> and it DIED with message "2: Invalid argument at ..."

This appears to be a documentation bug...

open my $ff, ">&", STDOUT or die "1: $!";
open FH, ">&$ff" or die "2: $!";

does not work, but:

open my $ff, ">&", STDOUT or die "1: $!";
my $fd = fileno($ff);
open FH, ">&$fd" or die "2: $!";

and so does:

open FH, ">&", $ff or die "2: $!";


> 
> 2. Furthermore help does not explain situations when redirecting one
>    "in momory" file to an other
> my $mem1;
> open F1, ">", \$mem1;
> my $mem2;
> open F2, ">", \$mem2;
> print $mem1 'AAA';
> print $mem2 'BBB';
> 
> open F1, ">&", F2;
> print F1 'aaa';
> print F2 'bbb';
> 
> print $mem1;
> print $mem2;
> exit 0;
> 
> Expected results: AAABBBaaabbb
> Actual results: AAABBBbbb

I'm assuming you made an error copying the code since the code supplied 
won't work. (print $mem1 'AAA';)

#!/usr/bin/perl

my $mem1;
open F1, ">", \$mem1;
my $mem2;
open F2, ">", \$mem2;
print F1 'AAA';
print F2 'BBB';

open F1, ">&", F2;
print F1 'aaa';
print F2 'bbb';

print "$mem1\n";
print "$mem2\n";

__END__

Output:
AAA
BBBbbb


Chaning it to  open F1, ">&=", F2; 

#!/usr/bin/perl

my $mem1;
open F1, ">", \$mem1;
my $mem2;
open F2, ">", \$mem2;
print F1 'AAA';
print F2 'BBB';

open F1, ">&=", F2;
print F1 'aaa';
print F2 'bbb';

print "$mem1\n";
print "$mem2\n";

__END__

Output:
AAA
BBBaaa





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