Re: join() and split() to respect string overload operations
[email protected] (Paul LeoNerd Evans)
| Newsgroups | perl.perl5.porters |
|---|---|
| Message-ID | <[email protected]> |
On Mon, Jun 07, 2010 at 09:03:18AM -0700, Jan Dubois wrote:
> On Mon, 07 Jun 2010, Paul LeoNerd Evans wrote:
> > Now create a string-like object with well-behaved operator overload for
> > concat, and observe you cannot still preserve the following identities:
> >
> > my $str = StringLike->new("Foo");
> >
> > substr( $str . "x", 0, len $str ) eq $str
> > substr( "x" . $str, 0, 1 ) eq $str
> >
> > substr( $str, 0, 1 ) . substr( $str, 1 ) eq $str
>
> I don't quite get how this would not work as long as you also have a
> well-behaved overload for 'eq' and '""'.
Er, sure. I didn't state that too clearly; I should have said it
provided all the operators one currently can for string-alikes; namely,
'.', '""' and 'eq'.
> Or would these identities also be
> broken:
>
> substr( $str . "x", 0, len $str ) eq "$str"
> substr( "x" . $str, 0, 1 ) eq "$str"
>
> substr( $str, 0, 1 ) . substr( $str, 1 ) eq "$str"
>
> If these work, but yours don't, then I wouldn't consider the complete
> set of overloads to be self-consistent. If the these later identities
> are broken as well, could you give an example of how to do it?
But, these identities -are- broken, because of the "squashing to plain
string" that happens on the right-hand side.
Lets just take one identity, the first one. First we need a concrete
example of a string-like object with some interesting information in it.
my $str = String::Tagged->new( "hello world" );
$str->apply_tag( 0, 5, fg => 'red' );
$str->apply_tag( 6, 5, fg => 'green' );
We've now got a testing string, which applies some pretty colours to the
words. (I shall not consider how; those interested can read
String::Tagged on CPAN).
$str . "x"
Is now a 12-character string, representing the string "hello worldx",
where the first 11 characters are in pretty colours but the final "x" is
not.
_IF WE HAD_ substr() overloading, and String::Tagged did it, then
substr( $str . "x", 0, len $str )
then chops off those leading 11 characters, yielding yet again the
string "hello world" with pretty colours applied. String::Tagged's eq
operator can then compare the string value -and- the applied tags,
finding the identity
substr( $str . "x", 0, len $str ) eq $str
to be true.
However, it is not true that
substr( $str . "x", 0, len $str ) eq "$str"
because in this case, the right-hand side has been squashed down by
String::Tagged's '""' operator into a plain string. Because substr()
yielded a String::Tagged object, this object's 'eq' operator will be
used; and will discover the two strings do not have the same tags.
--
Paul "LeoNerd" Evans
[email protected]
ICQ# 4135350 | Registered Linux# 179460
http://www.leonerd.org.uk/
signature.asc
(application/pgp-signature, 190 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iD8DBQFMDiIivLS2TC8cBo0RAvejAJ4z+FK4QqC/stwEw+36DsDb6M+OVACg/TXA T/dU+a9rA/PpaaqmY8MoxTE= =yDYe -----END PGP SIGNATURE-----