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??? > > TIA, > Peter > > P.S. you can see where I comment out the ccyymmdd_now function. That > approach works, but is not what I'm shooting for. > > %date_formats = (
Make sure you declare %date_formats before you assign to it so the 'yyyymmdd' key will see it. my %date_formats; %date_formats = ( > # "ccyymmdd" => {now => \ccyymmdd_now()}, # ccyymmdd => { now => \&ccyymmdd_now }, > "ccyymmdd" => {now => sub { > my @arr = localtime(); > 1900 + $arr[5] . sprintf("%02d",$arr[4] + 1) . > sprintf("%02d",$arr[3]); sprintf '%d%02d%02d', 1900 + $arr[5], $arr[4] + 1, $arr[3]; > } > }, > > "yyyymmdd" => {now => ${$formats{"ccyymmdd"}}{now},}, yyyymmdd => { now => $date_formats{ccyymmdd}{now} }, > ); > > sub now { > my $format = shift; > $format =~ tr/A-Z/a-z/; my $format = lc shift; > if (exists $date_formats{$format}){ > # Not working... > &{${$date_format{$format}}{now}}(); $date_formats{$format}{now}->(); > }else{ > print STDERR "invalid format\n"; > } > > } > > #print "|" . ccyymmdd_now() . "|\n"; > print "|" . now("ccyymmdd") . "|\n"; > <STDIN>; > > sub ccyymmdd_now{ > my @arr = localtime(); > 1900 + $arr[5] . sprintf("%02d",$arr[4] + 1) . sprintf > ("%02d",$arr[3]); > } > > __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]