Popcorn Dave has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks - I'm currently in and advanced Perl class and we've been handed an assignment using ties. It can be anything we want, is supposed to be simple, and that is where I'm stumped!

I am at a loss where and why I would use a tie beyond perhaps the DBM usage. Can anybody offer some uses of where ties would be appropriate -- beyond fancy restaurants of course ;)

I'm currently playing with some code to show how many seconds have elapsed around a section of code, but localtime and comparing starting and ending times is usually returning 0 seconds as the code is so small. Any suggestions?

Replies are listed 'Best First'.
Re: Real world uses of the tie function.
by demerphq (Chancellor) on Apr 17, 2002 at 08:58 UTC
    Hi.

    Just thought that you (hopefully your teacher is already)should be aware there are some subtle bugs with the tie implementation.

    Tie's FETCH called twice?
    warning: tied vars may evaluate twice in logical expressions
    warning: bug with tie (5.6.1)

    But you ask why anyone would want to write a Tie? Well, outside of the many ties on CPAN and their usefullnes I would say the answer would in general be one of two reasons:

    A) A programmer has a class whose behaviour conceptually is equivelent (or a superset) of the behaviour of a base type. So they decide to provide a Tie interface so that their users can stay within a familiar paradigm. Tie::IniFile

    B) A programmer wants to intercept accesses to a base type to add some kind of extra processing. Tie::Refhash

    C) The programmer wants to do something funky and unexpected Tie::Cycle

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      In addition to those problems, there's also my very first post here on PM. It was about a problem with tied hashes...to which I never really got a satisfactory response: Whither the truth of a tied hash?

      In short, a populated hash will respond truthfully in a scalar context:

      if (%h) { ...do something... }
      whereas a populated tied hash will do no such thing. It will always evaluate to false.

      So much for drop-in hash sniffers.

      Matt

(MeowChow) Re: Real world uses of the tie function.
by MeowChow (Vicar) on Apr 17, 2002 at 06:07 UTC
    Just peruse CPAN's Tie:: modules. That should give you plenty of ideas.

    To get more clock accuracy, use Time::HiRes.

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Real world uses of the tie function.
by JayBonci (Curate) on Apr 17, 2002 at 06:44 UTC
    Also, if you're looking at timing code (for efficiency), consider using Benchmark and running your code 10K times. As far as for real-world uses of the tie function, think of it any sort of easy interface into perl's hash functionality...anything that you want to hide from the developer and just allow magic to happen.

    Databases come to mind right off the bat, but also the Cache::Cache modules practically scream "tie me to a hash". It's an interface simplifier when you look at it for basic usage, but for more advanced usage, it can also be a legacy API emulator, and many many other powerful uses. Check around some of the uses, but also try Cache::Cache if you're looking for something to implement.

        --jb
Re: Real world uses of the tie function.
by tachyon (Chancellor) on Apr 17, 2002 at 11:32 UTC

    File::ReadBackwards has a nice tie interface that lets you say:

    use File::ReadBackwards; tie *LOG, File::ReadBackwards, $log_file or die "can't read '$log_file' $!" ; # now read the file in the usual way EXCEPT we # read the last line first, first line last :-) # how's that for an interesting use of tie? while(<LOG>) { print; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Real world uses of the tie function.
by Popcorn Dave (Abbot) on Apr 17, 2002 at 06:59 UTC
    Thanks to you both! This is starting to make some sense now :)

    As far as my timer code, I'm not really worried about accuracy but rather a small program that demonstrates a tied variable

Re: Real world uses of the tie function.
by tjh (Curate) on Apr 17, 2002 at 13:23 UTC

    Out of curiosity, where is your Advanced Perl class being offered?

      Canada College, Redwood City, CA.

      Unfortunately, according to the teacher, it's only offered about once every two years. We got lucky and had enough people to convince the dean to let it run.