Thanks to all who replied.

|---------+---------------------------->
|         |           "Rob Dixon"      |
|         |           <[EMAIL PROTECTED]|
|         |           m.co.uk>         |
|         |                            |
|         |           02/07/2003 05:09 |
|         |           PM               |
|         |                            |
|---------+---------------------------->
  
>--------------------------------------------------------------------------------------------------------------|
  |                                                                                    
                          |
  |       To:       [EMAIL PROTECTED]                                                 
                          |
  |       cc:                                                                          
                          |
  |       Subject:  Re: calling a subroutine as an element of a hash table...          
                          |
  
>--------------------------------------------------------------------------------------------------------------|




Peter Farrar wrote:
> Hi All,
>
> I swear I've called subroutines this way before, but I can't find any
> examples.
> In this case, I'm looking to build some time/date functions.
> Regardless of what the actual code is supposed to do, the calling of
> subroutines as elements of a hash is causing me grief.  I am
> receiving the following message:
>
>       Undefined subroutine &main:: called at D:
> \perldev\workroom\dates\dates.pl line 16.
>
> Here is the code.  Can anyone see what I am doing wrong here???

I'm afraid this is mainly just another

    use strict;

rant. You have three different hashes in use. Look:

> %date_formats = (

1) %date_formats

> #     "ccyymmdd" => {now => \ccyymmdd_now()},
>       "ccyymmdd" => {now => sub {
>                         my @arr = localtime();
>                         1900 + $arr[5] . sprintf("%02d",$arr[4] + 1) .
> sprintf("%02d",$arr[3]);
>                   }
>             },
>
>       "yyyymmdd" => {now => ${$formats{"ccyymmdd"}}{now},},
> );

2) %formats

Also, you need a separate statement to defined a key value in terms
of another value in the same key. Otherwise you could define
recursive hashes! Like this:

$date_formats{yyyymmdd}{now} = $date_formats{ccyymmdd}{now};

> sub now {
>       my $format = shift;
>       $format =~ tr/A-Z/a-z/;
>
>       if (exists $date_formats{$format}){
>             # Not working...
>             &{${$date_format{$format}}{now}}();

3) %date_format

Can also be written:

    $date_formats{$format}{now}();

>       }else{
>             print STDERR "invalid format\n";
>       }
> }
>
[snip trailing code]

If you'd used strict, you would have had to declare all of those
hashes, and would certainly have realised that you were using
three independent ones!

Cheers,

Rob




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]









-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to