Hi Chanan,
On Monday 19 Jul 2010 09:26:31 Chanan Berler wrote:
> Hi All,
>
> As learning perl again, I came upon TIE.
> My idea on creating a class, monitoring access to temperture (by
> saving the modified date).
> Q: can i define other subroutines to class, uses tie ?
Perl Best Practices (a recommended read) recommends against creating tied
interfaces and I tend to agree most of the time. Use a plain object with
methods instead.
You can define other subroutines on the tied class (and even access them using
perldoc -f tied), but don't do that.
>
>
> Here is what I wrote:
> =================
>
> package Temperture;
That should be "Temperature".
> sub TIESCALAR
> {
> my ($pkg, $temp) = @_;
> my $cdate = time();
>
> my $ref_temp = { temp => undef, cdate => $cdate, mdate => $cdate };
> bless $ref_temp, "Temperture";
> return $ref_temp;
> }
You should have a one line space between subroutine calls.
> sub FETCH
> {
> my ($self) = @_;
> return $self->{temp};
> }
> sub STORE
> {
> my ($self, $new_temp) = @_;
> $self->{temp} = $new_temp;
> $self->{mdate} = time();
> return $self;
> }
> sub get_mdate
> {
> my ($self) = @_;
> return $self->{mdate};
> }
>
> package main;
> tie my $t, "Temperture";
> $t = 10;
> print $t->get_mdate();
You need to call tied(...) here.
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang
God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl