Re: Testing Ties
Michael G Schwern wrote: tie() always returns an object. The object returned by the new method is also returned by the tie function, which would be useful if you want to access other methods in CLASSNAME. Your insight's and Kevin's were incorporated into Tie::Filehandle::Preempt::Stdin, uploaded to CPAN today. Thanks! jimk
Testing Ties
How do you test that a variable has been tied to a class? I looked through Test::More; the term 'tie' is conspicuous by its absence. I also searched the archives of this list and couldn't locate anything. I'm looking for something along the lines of Test::More::isa_ok that we could use like this where the 'tie' call does not (at least in normal practice) explicitly return an object: use Tie::File; tie @data, 'Tie::File', $file or die; is_tied(@data, $file, [EMAIL PROTECTED] is tied to \$file); Jim Keenan
Re: Testing Ties
James E Keenan wrote: How do you test that a variable has been tied to a class? I looked through Test::More; the term 'tie' is conspicuous by its absence. I also searched the archives of this list and couldn't locate anything. I'm looking for something along the lines of Test::More::isa_ok that we could use like this where the 'tie' call does not (at least in normal practice) explicitly return an object: use Tie::File; tie @data, 'Tie::File', $file or die; is_tied(@data, $file, [EMAIL PROTECTED] is tied to \$file); I partially answered my own question by looking at the tests MJD wrote for Tie::File. He has the 'tie' call explicitly return an object: # from 01_gen.t my $o = tie @a, 'Tie::File', $file, autochomp = 0, autodefer = 0; print $o ? ok $N\n : not ok $N\n; $N++; But he's not using *any* Perl test module here; everything is manual. So I'm still wondering if anybody has attempted something that says, in a self-documenting way, Here I'm testing whether X is tied to Y. Or perhaps this should be on the TODO list? Jim Keenan
Re: Testing Ties
On Tue, Apr 12, 2005 at 06:58:34PM -0400, James E Keenan wrote: How do you test that a variable has been tied to a class? $ perldoc -f tied tied VARIABLE Returns a reference to the object underlying VARIABLE (the same value that was originally returned by the tie call that bound the variable to a package.) Returns the undefined value if VARIABLE isn't tied to a package. ie. get the object from the tied variable and then treat it like any other object. isa_ok tied $var, A::Class; I'm looking for something along the lines of Test::More::isa_ok that we could use like this where the 'tie' call does not (at least in normal practice) explicitly return an object: tie() always returns an object. The object returned by the new method is also returned by the tie function, which would be useful if you want to access other methods in CLASSNAME. use Tie::File; tie @data, 'Tie::File', $file or die; is_tied(@data, $file, [EMAIL PROTECTED] is tied to \$file); That can't work as you can't tell what particular thing a variable is tied to in any sort of universal way... it could be tied to anything. You can only tell the class and get at the underlying object. To find out what file a Tie::File variable is tied to you have to use some Tie::File specific method. my $obj = tie @data, 'Tie::File', $file or die; is $obj-filename, $file, [EMAIL PROTECTED] is tied to $file; However, AFAIK, Tie::File has no such method. You can poke inside and get at it with $obj-{filename} but that's unsupported. Perhaps a patch is in order.
Re: Testing Ties
On Apr 12, 2005, at 3:58 PM, James E Keenan wrote: How do you test that a variable has been tied to a class? I looked through Test::More; the term 'tie' is conspicuous by its absence. I also searched the archives of this list and couldn't locate anything. I'm looking for something along the lines of Test::More::isa_ok that we could use like this where the 'tie' call does not (at least in normal practice) explicitly return an object: use Tie::File; tie @data, 'Tie::File', $file or die; is_tied(@data, $file, [EMAIL PROTECTED] is tied to \$file); in most cases, isa_ok(tied $data, 'Tie::File') should work, I think. There's no generic way to check the additional arguments like you are trying to do. -kevin