Hello, Monks ...
I'm trying to turn my previous question into a learning exercise, but I'm not getting far. My goal is to create two modules: "Tie::Time::Scalar" and "Tie::Time::Array". You'll see from the code below, that I've been partway successful, but I've hit a couple of walls (not the larry kind).
(note that, when everything is working, I'll do the proper "h2xs" thing, and all of the proper module packaging and stuff, but for now, I just want to understand things.)
#!/usr/bin/perl -w
use strict;
package TieTimeScalar;
sub TIESCALAR { bless {}, shift }
sub FETCH { scalar localtime }
package main;
my $now; tie ($now, "TieTimeScalar");
while (1) { print "$now\n"; sleep 1 }
The above code works perfectly (and is basically no more than what merlyn spoon-fed me.)
However, when I try to do the same thing but tying the 9-element list output of localtime to an array with the following code ...
#!/usr/bin/perl -w
use strict;
package TieTimeArray;
sub TIEARRAY { bless {}, shift }
sub FETCH { my @self = localtime ; return @self }
sub FETCHSIZE { my $self = shift ; return scalar @{$self->{@self}} }
package main;
my @now; tie (@now, "TieTimeArray");
while (1) { print "@now\n"; sleep 1 }
... I get the error ...
Can't use an undefined value as an ARRAY reference at ./TieTimeArray l
+ine 8.
Obviously, I'm missing something fundamental to the concept of tying arrays (and probably just tying in general.)
Would someone like to try to explain to me what I'm doing wrong here (maybe in small words, so my little brain can understand)?
Also, man perltie talks about implementing the STORE, UNTIE and DESTROY methods (for scalars) and also STORESIZE for arrays. Are these needed in every class that implements tying? What would those methods do? And why would I want to do them?
Thanks, --Sandy (who is drowning in an ocean of camel poop)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.