[ic] Adding a system wide module

Mike Heins mike at perusion.com
Mon Jan 31 21:16:41 EST 2005


Quoting Jon Jensen (jon at endpoint.com):
> On Tue, 1 Feb 2005, Jeff Fearn wrote:
> 
> >>The you can call it in a GlobalSub or UserTag, and perhaps even in a 
> >>[perl global=1] block, provided that you have the catalog in the 
> >>AllowGlobal configuration string.
> >
> >Does this mean that I need to write a wrapper for every function I
> >wanted to use from Date::Calc?
> >
> >I was hoping to be able to bypass the Tag stuff entirely, just using
> >[perl] and writting perl code in that on html pages.
> 
> If all you want is to pass through arguments to various subroutines in 
> Date::Calc, you could create a global usertag wrapper like this:
> 
> 
>     UserTag date-calc addAttr
>     UserTag date-calc Routine <<EOR
> 
>     use Date::Calc;
> 
>     sub {
>         my ($opt) = @_;
>         my $out = eval "return Date::Calc::$opt->{op}(\@{\$opt->{args}})";
>             if ($@) {
>                     logError("tag date-calc error: $@");
>                     return;
>             }
>             return $out;
>     }
>     EOR
> 
> 
> You call the tag like this:
> 
>     [date-calc op=Day_of_Year args.0=2004 args.1=12 args.2=31]
> 
> I tested this code and it actually works. :)

The eval is just a tad dangerous with passed tag parameters, so I think
I would prefer:

UserTag date_calc Order op args
UserTag date_calc Order addAttr
UserTag date_calc Routine <<EOF
use Date::Calc;
sub {
   my ($op,$args,$opt) = @_;
   no strict 'refs';
   my @return;
   eval {
        @return = &{"Date::Calc::$op"}(@$args);
   };
   if($@) {
          logError("tag date-calc error: $@");
         return;
   }
   return wantarray ? @return : $return[0];
}
EOF

As Jon said, you can then do:

     [date-calc op=Day_of_Year args.0=2004 args.1=12 args.2=31]

or

     [date-calc op=Day_of_Year args=`[2004,12,31]`]

or

     [perl]
               my $result = $Tag->date_calc({
                                    op => 'Day_of_Year',
                                    args => [2004,12,31],
                                });
     [/perl]


-- 
Mike Heins
Perusion -- Expert Interchange Consulting    http://www.perusion.com/
phone +1.765.647.1295  tollfree 800-949-1889 <mike at perusion.com>

Being against torture ought to be sort of a bipartisan thing.
-- Karl Lehenbauer


More information about the interchange-users mailing list