[Mastering-perl] First Peek: Tied Variables

brian d foy brian.d.foy at gmail.com
Wed Feb 8 00:29:31 EST 2006


My first deadline for Mastering Perl is next week: I have to turn in
two chapters.  You can read the first of those (though not the first
chapter): It's still pretty rough, but I like the basic structure. I
think it does a lot more than the usual tie treatments.

http://www252.pair.com/comdog/mastering_perl/Chapters/tied_variables.html

I actually took way too much time dealing with this chapter because I
got off on a tangent investigating some problems in Devel::Size. In my
writing on tied arrays, I decided to pack DNA data into a long string
behind a tied array. Thinking that I'd be saving quite a bit of space
for really long sequences, I wanted to measure the difference. It
turns out that Devel::Size has problems here. I'm not sure what they
are, really, but I spent a lot of time trying to track it down because
I thought I might have done something stupid that negated my memory
savings. Dan Sugalski's on it already and maybe I'll have an answer
soon.

I also got sidetracked trying to figure out how to handle splice() for
a tied array. That's not an easy thing to do, even if the data on the
back end is simple. Have you ever looked at the number of different
ways that you can call splice()? Now think about implementing all that
in your tied array. No thanks. I punted and just skipped that part.

I wasted most of today trying to figure out how to make a tied scalar
act like a filehandle. For completeness, I covered tied filehandles,
but I also had to note that in Learning Perl and Intermediate Perl we
told people to get away from bareword filehandles. Well, if you try to
tie a scalar, you get scalar semantics and you don't get a chance to
call PRINT. I thought I'd be clever and create a class with both a
TIEHANDLE and TIESCALAR, but it just didn't work out. I thought about
tying just the {IO} portion of a scalar, but then I need a package
variable, which I think is worse than the bareword. Oh well.

So, now I know all that stuff, but none of it puts words on the page.

--
brian d foy <brian.d.foy at gmail.com>
http://www.pair.com/~comdog/


More information about the Mastering-perl mailing list