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. :-)


---
demerphq

    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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.