I have been reading my latest version of the Camel Book, and I wonder to myself... what good are tied variables?

Hear me out before anyone gets upset. First... one of people's complaints about perl is that it's far too hard to understand and read. While I don't agree, I think tied hashes add an unnecessary layer of complexity that doesn't make much sense when you already have objects.

Imagine a novice looking at the following two code snippets:

<newbiecommenting>

# tie tie $var, "classXYZ"; $var = 5; print $var; # yields 25... ??? # objects my $var = new Squareable(5); $var->square(); print $var; # oh... yields 25... neato!
</newbiecommenting>

Granted, this is a bit of a juvenile example, but do you see what I'm getting at? How is a novice supposed to be able to discern this type of behavior? In the second example, it's easy to tell what happened. Objects are more self-documenting even without POD.

I know modules like the Ordered Hash Tie module are helpful, but even that could have just been an object.

Perhaps I'm missing something, but I just don't understand Tied variables. Even tied filehandles are probably better just as an object or an extension of FileHandle. You formally declare your behavior instead of hiding it behind the way that perl NORMALLY behaves.

If someone could enlighten me as to some really good uses of tied hashes that make more sense than using a similarly coded object, please do. Perhaps I'm just looking at it wrong.

--
perl: code of the samurai


In reply to Tied Variables - why? by samurai

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.