Warning when stringify reference and then -> it? 
Author Message
 Warning when stringify reference and then -> it?

I've just spent a couple of hours tracking down a mysterious error
message 'Can't locate object method "isDocumentFragmentNode" via
package "XML::DOM::Element=HASH(0x8555130)"'.  At first I suspected
some strangeness with XML::DOM, but it turned out to be my own code.
By accident I was using tr/\d// on a reference (don't ask me how!),
which doesn't change the stringified representation, but does lose the
magic.  Later on I was using this 'object' as normal and it gave the
strange error which I didn't understand.

I thought of suggesting a warning such as 'stringifying this
reference will lose the magic'.  That would also warn people who use
references as hash keys and expect to get them out again.  But OTOH,
there is lots of code which has legitimate reasons for stringifying
references (hashes, debugging).

Another way to do things is to have a 'used to be a reference' flag
for each scalar value.  If somebody then tried to use this scalar as
an object reference (or indeed any kind of reference), they'd get a
special warning rather than 'Can't locate object method via package
X'.  This is reasonable, because nobody would want to stringify a
reference and then use 'HASH(0x4183a4)' as a package name.  But there
is the overhead of that extra bit for all string scalars.

Or you could leave the stringification code alone and change the error
message.  Maybe it could consider that calling close() on
'IO::File=GLOB(0x80cb9c8)' indicates a stringification problem, rather
than a mistyped package or method name.  I tried 'use diagnostics',
but its error message was misleading too.

Here is some code to illustrate the problem:

#!/usr/bin/perl -w
use IO::File;
use strict;
use diagnostics;
my $t = IO::File->new_tmpfile();
if ($t =~ tr/\d//) {
        print "string $t contains digits\n";

Quote:
}

$t->close();

The error message produced is not helpful for the novice user.  This
example looks a bit silly, because nobody would create an object and
then do tr// on it, but such things can happen in larger programs.
(In my case it was using $a and $b in a subroutine called from inside
a sort - I had meant to type $_[0] and $_[1] instead.)

Do you people think that there is something worth fixing here?

--
Ed Avis



Wed, 26 Feb 2003 20:06:08 GMT  
 Warning when stringify reference and then -> it?

Quote:

> By accident I was using tr/\d// on a reference (don't ask me how!),
> which doesn't change the stringified representation, but does lose the
> magic.  Later on I was using this 'object' as normal and it gave the
> strange error which I didn't understand.
> a sort - I had meant to type $_[0] and $_[1] instead.)

> Do you people think that there is something worth fixing here?

s/// is broken, too.

Here's one way to fix tr///:
--- doop.c.orig Fri Sep  1 14:09:46 2000

     I32 hasutf = (PL_op->op_private &
                     (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));

-    if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
+    if ((SvREADONLY(sv) || SvROK(sv)) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
        Perl_croak(aTHX_ PL_no_modify);

     (void)SvPV(sv, len);
End of Patch.

but that would prevent doing anything useful with an overloaded object
(but that should be ok since it is broken as it is now).  And the
error message may need to be more specific.

Are there any cases where a non-readonly non-reference would have
get magic but not set magic?  If so, these would be broken too.

I also noticed that $x =~ tr/x// is calling get magic twice.



Thu, 27 Feb 2003 13:46:25 GMT  
 Warning when stringify reference and then -> it?

Quote:



> > By accident I was using tr/\d// on a reference (don't ask me how!),

> Here's one way to fix tr///:
> --- doop.c.orig    Fri Sep  1 14:09:46 2000
> +++ doop.c Sat Sep  9 22:37:38 2000

Sorry, I thought this was on p5p.  Here's a cplm-appropriate answer:

Looks like a bug to me.  Search on http://bugs.perl.org and see if
there is already a bug report and, if not, file one with perlbug.



Fri, 28 Feb 2003 04:18:46 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Database Desktop and the UPDATE command...

2. Q : SQL and INSERT and Multi-Records

3. Using b.c. Dates In Date fields

4. Reference stringify change under 5.6

5. Why does keys stringify hash references?

6. Why does keys stringify hash references?

7. references and warning messages

8. stringify function in Math::Complex module

9. PROPOSAL: Class::Stringify

10. Why does Perl stringify this number?

11. catch warn'ings in while (<>)...

12. "Error Creating Cursor Handle"

 

 
Powered by phpBB® Forum Software