Skip Menu |

This queue is for tickets about the Tie-Watch CPAN distribution.

Report information
The Basics
Id: 32548
Status: open
Priority: 0/
Queue: Tie-Watch

People
Owner: Nobody in particular
Requestors: mschwern [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: (no value)
Fixed in: 1.2



Subject: Doesn't work with objects
The following code demonstrates that Tie::Watch does not work with objects. use Tie::Watch; $obj = bless { foo => 23 }, "Bar"; $w = Tie::Watch->new( -variable => $obj, -store => sub { warn "@_"; } ); It results in "Tie::Watch::new() - not a variable reference." This is because Tie::Watch is using ref() to check the reference type. To get the underlying reference type of an object use Scalar::Util::reftype().
Here's a patch. I took the liberty of converting the tests to Test::More and restructuring things into t and lib directories. You're welcome to ignore that part. All it really took was using reftype() instead of ref(). watch.patch is the full thing. small.patch is just the bug fix and test.

Message body is not shown because it is too large.

--- Watch.pm (/vendor/Tie-Watch/Watch.pm) (revision 52314) +++ Watch.pm (/local/Tie-Watch/lib/Tie/Watch.pm) (local) @@ -239,6 +239,7 @@ use 5.004_57;; use Carp; +use Scalar::Util qw(reftype); use strict; use subs qw/normalize_callbacks/; use vars qw/@array_callbacks @hash_callbacks @scalar_callbacks/; @@ -263,7 +264,7 @@ my $variable = $args{-variable}; croak "Tie::Watch::new(): -variable is required." if not defined $variable; - my($type, $watch_obj) = (ref $variable, undef); + my($type, $watch_obj) = (reftype $variable, undef); if ($type =~ /(SCALAR|REF)/) { @arg_defaults{@scalar_callbacks} = ( [\&Tie::Watch::Scalar::Destroy], [\&Tie::Watch::Scalar::Fetch], --- /dev/null 2008-01-22 19:23:58.000000000 -0800 +++ t/object.t 2008-01-22 19:20:55.000000000 -0800 @@ -0,0 +1,33 @@ +#!/usr/bin/perl -w + +use Test::More 'no_plan'; + +use_ok 'Tie::Watch'; + +{ + package Bar; + + sub method { 99 } +} + +my %watcher; +my $obj = bless { foo => 23 }, 'Bar'; +isa_ok $obj, 'Bar'; + +my $watch = Tie::Watch->new( + -variable => $obj, + -store => sub { + my($self, $key, $val) = @_; + $watcher{$key} = $val; + $self->Store($key, $val); + }, +); + +is_deeply $obj, { foo => 23 }; + +isa_ok $obj, 'Bar'; +$obj->{foo} = 42; +is $obj->{foo}, 42; +is $watcher{foo}, 42; + +is $obj->method, 99; --- Makefile.PL (/vendor/Tie-Watch/Makefile.PL) (revision 52314) +++ Makefile.PL (/local/Tie-Watch/Makefile.PL) (local) @@ -3,7 +3,11 @@ WriteMakefile( 'NAME' => 'Tie::Watch', 'VERSION_FROM' => 'Watch.pm', + 'PREREQ_PM' => { + 'Test::More' => 0.47, + 'Scalar::Util' => 0, + }, ($] >= 5.005 ? (ABSTRACT => 'Place watchpoints on Perl (and Tk) variables', AUTHOR => 'Steve Lidie (Stephen.O.Lidie@Lehigh.EDU)') : ()),
Subject: Re: [rt.cpan.org #32548] Doesn't work with objects
Date: Fri, 25 Jan 2008 08:27:13 -0500
To: bug-Tie-Watch [...] rt.cpan.org
From: Steve Lidie <sol0 [...] Lehigh.EDU>
On Jan 22, 2008, at 10:26 PM, Michael G Schwern via RT wrote: Show quoted text
> > Queue: Tie-Watch > Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=32548 > > > Here's a patch. I took the liberty of converting the tests to > Test::More and restructuring things into t and lib directories. > You're > welcome to ignore that part. All it really took was using reftype() > instead of ref(). > > watch.patch is the full thing. small.patch is just the bug fix and > test. > <watch.patch><small.patch>
Many thanks for your work on my old crufty code - apologies that I cannot spend much time on Perl these days. Steve
On Fri Jan 25 08:27:34 2008, sol0@Lehigh.EDU wrote: Show quoted text
> Many thanks for your work on my old crufty code - apologies that I > cannot spend much time on Perl these days.
You're welcome. With that little patch it works like a charm.
On 2008-01-25 18:01:08, MSCHWERN wrote: Show quoted text
> On Fri Jan 25 08:27:34 2008, sol0@Lehigh.EDU wrote:
> > Many thanks for your work on my old crufty code - apologies that I > > cannot spend much time on Perl these days.
> > You're welcome. With that little patch it works like a charm. > >
See branch rt32548 in git://github.com/eserte/Tie-Watch.git I have to check if it still works with Tk, then I can release it. Regards, Slaven