Hi, I've asked a similar question an hour ago in the ChatterBox, but I need more room to really explain it: Is there a way (a cpan module using XS, or anything) to tie a variable to an existing instance of an object, rather than specify the classname and its constructor parameters?

Basically, assuming a scalar, you'd have a tying-class (say TyingClass) (the class defining sub TIESCALAR) and a scalar variable (say $variable). With these two, you'd normally write something like:
tie $variable, 'TyingClass', @optionalConstructorParameters
Then, tied $variable would return the instance (created running TyingClass->TIESCALAR(@optionalConstructorParameters)).

But say I created my own instance of the TyingClass, named $tyingInstance (passing any set of constructor parameters). And then, I've changed the instance calling a stateful method on $tyingInstance (a method which depends on the state of its object), and this method actually changed the $tyingInstance object, in a way that cannot be reproduced by its constructor. How can one (if they can) tie my $variable to $tyingInstance, assuming TyingClass can be any Perl class, not specifically designed for this purpose.

Maybe there's a good reason why tying is done only in this way, so if there is one, I'd appreciate if someone took the time to explain it. Thanks.

In reply to How to tie variables to objects by perldeveloper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.