Well I dont really think Schwern was thinking about people who might need the stringified version of the array, but rather for situations where having a real array would be a waste of space. I dont think you can argue that his implementation is buggy, it just doesn't do something that you think it should. (And frankly that I think it should too :-)
Anyway, I think your patch would be undesirable as it would cause all code using Tie::VecArray to break as they would be passing in non refs. But changing the TIEARRAY method resolves that problem.
sub TIEARRAY { my $class=shift; my $bits =shift; no strict 'refs'; my $self = bless [\%{$class.'::FIELDS'}], $class; $self->{bits} = $bits; if (@_ and eval { $_[0]=$_[0]; 1} ) { $self->{vec} = \$_[0]; } else { my $vec=@_ ? $_[0] : ""; $self->{vec} = \$vec; } $self->{size} = _BYTES2IDX($self, length ${$self->{vec}}); return $self; }
However on second thought that may be a problem as it could have disasterous results on code not expecting alias semantics on $vec, but rather the original copy semantics. So a variant to your approach would do nicely:
sub TIEARRAY { my($class, $bits, $vec) = @_; no strict 'refs'; my $self = bless [\%{$class.'::FIELDS'}], $class; $self->{bits} = $bits; $self->{vec} = ref $vec ? $vec : \$vec; $self->{size} = _BYTES2IDX($self, length ${$self->{vec}}); return $self; }
Now you would have both semantics. If you pass a ref its expected to a be a reference to a writeable scalar, if you dont then the value is copied. This would be a patch that shouldnt break anything that already exists, and adds useful functionality.
Which brings me to a different point. Instead of just writing a medition about how your version is better, why not work out a patch that shouldnt break existing code, but provides your desired behaviour and then give it to the author for the next release? Hint, hint. :-)
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
In reply to Re: A good lesson on Tie (through a negative example)
by demerphq
in thread A good lesson on Tie (through a negative example)
by pg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |