[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

ties and other filehandle bondage




Here, have some Perl:

  use strict;
  use warnings;
  use Data::Dumper;
  my @values = '';

  {
    package LogStr;
    sub TIESCALAR { bless {} }
    sub FETCH { $values[-1] }
    sub STORE { push @values, $_[1]; }
  }

  tie my $string, 'LogStr';
  $string = 'first';
  $string = 'second';

  open my $fh, '>', \$string or die "can't open ref to tied str: $!";
  print $fh "third" or die "can't print to handle on ref to tied str: $!";
  close $fh, or die "can't open ref to tied str: $!";

  print Dumper({ values => \@values, string => $string });

I would expect this to either die or end with:

  $VAR1 = {
    'string' => 'third',
    'values' => [ '', 'first', 'second', 'third' ]
  };

Instead, "third" ever happens.  The $fh handle seems to go nowhere.

I have no idea what's going on.  I realize that opening a filehandle on a
reference to a tied scalar is ... well, it's asking for trouble.  I just wish I
got a warning, fatal error, or core dump, rather than silent failure!

-- 
rjbs


Follow-Ups from:
Vincent Pit <perl@profvince.com>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]