Sunil
Sunil

Reputation: 864

Difference in behaviour between Perl versions

I have the following script

#!/usr/bin/perl -w
print "sample\n";
syswrite STDIN, "script";
my $input = <STDIN>;
print "$input";

This scripts executes properly in Perl 5.8.8 version giving the following output:

sample
script

However when executed in Perl 5.14.2 it gives the following error:

Filehandle STDIN opened only for input at ./sample.pl line 5.

What has been changed between the Perl versions?

Upvotes: 0

Views: 187

Answers (1)

ikegami
ikegami

Reputation: 385655

Presumably, you're asking to replicate the 5.8.8 behaviour. I'm not sure how much sense makes to do so, but you can create a Perl read-write handle attached to the same file descriptor as follows:

$ perl -e'
   open(my $fh, "+>&=", 0) or die $!;
   print($fh "foo\n") or die $!;
' >/dev/null
foo

or

$ perl -e'
   {
      open(my $fh, "+>&=", 0) or die $!;
      close(STDIN);
      open(STDIN, "+>&=", $fh) or die $!;
   }
   print(STDIN "foo\n") or die $!;
' >/dev/null
foo

Upvotes: 2

Related Questions