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.

Reply via email to