[ic] My first usertag... see any glaring errors?

Kevin Walsh interchange-users@icdevgroup.org
Thu Feb 13 23:45:00 2003


DB [DB@M-and-D.com] wrote:
> 
> Here's my first usertag... I didn't write it from scratch but modified 
> one I found. It seems to work but sometimes I get error log entries like:
> 
>   Safe: syntax error at (eval 749) line 1, near ">"
>   >
>   > >0
>   >
> 
> I thought I had this solved before but spoke too soon. The tag totals 
> (price*qty) of all items that have an sku starting with "AT-" and writes 
> this total to a scratch variable used on the cart page (the error *may* 
> occur when the total is zero but I need more testing to confirm that). 
> If any ic/perl gurus can glance over the tag I'd very much like to have 
> feedback.
> 
> Here's the tag:
> 
> #---- begin tag------------
> UserTag atv Routine <<EOR
> sub {
>          my $atvtot = 0;
> foreach my $atvindex (0 .. $#$Vend::Items) {
>          my $atvcode = $Vend::Items->[$atvindex]{code};
>          my $atvbase = $Vend::Items->[$atvindex]{mv_ib};
>          my $atvqty = $Vend::Items->[$atvindex]{quantity};
>          my $atvprice = $Tag->price({code => $atvcode , quantity => 
> $atvqty , noformat => 1});
>          my $isatv = 1           if $atvcode =~ m/^AT-/;
>          $atvprice = 0         if !$isatv;
>          $atvtot = $atvtot + ($atvprice * $atvqty);
> 
> $Scratch->{'atvtotal'} = $atvtot;
> }}
> EOR
> #----  end tag ------------
> 
> I use the scratch variable in following code on the cart page and this 
> is what I think causes the error. But it doesn't happen all the time and 
> the calculations are made correctly.
> 
> [if type=explicit compare="[calc][scratch atvtotal]>0 [/calc]"]
> [and type=explicit compare="[calc][scratch atvtotal]<25 [/calc]"]
> [then]
> [discount ENTIRE_ORDER] $s + 4 [/discount]
> [/then]
> [/if]
>
Your Safe error is coming from the first [calc] and will be caused if
the [scratch atvtotal] is blank or undefined.  Basically, the
calculation ">0" is a syntax error, and as such is trapped by Safe.

Are you sure you're calling your [atv] UserTag?  Try displaying the
value of your [scratch atvtotal] on the page, prior to the calculation,
just to test.

Also consider the following code in place of your [if] and [and] pair:

    [if type=explicit compare=`
        $Scratch->{atvtotal} ||= 0;
        return 1 if $Scratch->{atvtotal} > 0 && $Scratch->{atvtotal} < 25;
        return 0;
    `]
    [discount ENTIRE_ORDER]$s + 4[/discount]
    [/if]

Note that the above code also defaults the 'atvtotal' scratch to zero
if blank or undefined.

Discount is "$s + 4"?  Are you sure?  Although perfectly valid, that's
not much of a "discount". :-)

By the way, and in my opinion, your UserTag would be clearer if it was
written like this:

    UserTag atv Routine <<EOR
    sub {
        my $atvtot = 0;

        foreach my $item (@$Vend::Items){
            if ($item->{code} =~ m/^AT-/){
                $atvtot += $item->{quantity} * $Tag->price({
                    code => $item->{code},
                    quantity => $item->{quantity},
                    noformat => 1,
                });
            }
        }
        $::Scratch->{atvtotal} = $atvtot;
        undef;
    }
    EOR

I could be misreading your code, of course, and I haven't tested that
version.

I hope this helps.

-- 
   _/   _/  _/_/_/_/  _/    _/  _/_/_/  _/    _/
  _/_/_/   _/_/      _/    _/    _/    _/_/  _/   K e v i n   W a l s h
 _/ _/    _/          _/ _/     _/    _/  _/_/    kevin@cursor.biz
_/   _/  _/_/_/_/      _/    _/_/_/  _/    _/