in reply to RFC: Seconds2English

Given the brevity of many of your accessors I'd consider using AUTOLOAD and try to return the attribute of the same name as the sub that was called, if it exists.

--
I'm not belgian but I play one on TV.

Replies are listed 'Best First'.
Re: Re: RFC: Seconds2English
by chromatic (Archbishop) on Jul 18, 2003 at 20:33 UTC

    Why get that complex? I'd rather:

    { no strict 'refs'; for my $method (qw( seconds minutes hours days weeks months years +)) { *{ $method } = sub { return $_[0]->{ $method } }; *{ 'in_' . $method } = sub { return $_[0]->{ time } / $_[0]->{_table }{ $method } } +} } }

    Funny, that ended up quite a bit shorter than I expected.

Re: Re: RFC: Seconds2English
by Limbic~Region (Chancellor) on Jul 18, 2003 at 20:06 UTC
    belg4mit,
    I had considered AUTOLOAD for the trivial subs. I had also considered one of the modules that dynamically create your subs for you at compile time. I avoided both for the reason that if I decide to expand a single sub to do something unique, the dynamic code would get even more complicated.

    L~R

      If you expand the sub to do anything other than return a key you implement it as a named sub. FIN.

      --
      I'm not belgian but I play one on TV.

        belg4mit,
        Thanks for the distinction on when it is ok/not ok to use AUTOLOAD. I am sure some people would disagree - those people probably know what they are doing - I don't. I would assume that you would say it is ok to:
        no strict 'refs'; *{$AUTOLOAD} = sub { return $_[0]->{foobar} }; return $_[0]->{foobar};
        as a way to speed things up for subesequent calls as long as you know what you are doing.

        Cheers - L~R