Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.