[ic] help with safe code in a UserTag
Jim Boyer
interchange-users@icdevgroup.org
Tue Apr 22 17:44:00 2003
I am trying to write a UserTag to calculate shipping and am getting a safe
module error when I reconfigure the catalog. The error I get is as follows:
UserTag 'calc_shipping' subroutine failed safe check: syntax error at (eval
276) line 79, near "}
sub PubCharge
"
syntax error at (eval 276) line 103, near ";
}"
Here is my UserTag
------------------------------------
UserTag calc_shipping
UserTag calc_shipping Routine <<EOF
sub
{
# this routine is used to calculate shipping cost
# here is the business rule
# shipping is based only on price if no cd or video ordered
# if cd/video ordered with pubs then
# shipping is calculated on pubs subtotal price + a fixed
# value for each video or cd ordered
# if only cd/video ordered then
# shipping is calculated as fixed price for the first cd/video
# and a lower fixed price for the remaining cd/videos
#
# example (cd and pubs)
# pubs price total $25 and 2 cds price total $50
# shipping is $6 for the pubs + 2($1) for each cd
# giving a total of $8 shipping
#
# example (1 video and 2 cd)
# price for first video/cd = $4 + $1 for each additional
# shipping is $4 + 2($1) = $6
# fixed charges for cd/video
my $firstCDPrice = 4;
my $moreCDPrice = 1;
# temp variables
my $totalItems = 0;
my $exceptions = 0;
my $subTot = 0;
my $shippingCharge = 0;
# loop through the cart items
for (@$Items)
{
$totalItems++;
# if sku starts with VT (video) or CD then it is an exception
if ( ( $_->{code} ~= /^VT/ ) || ( $_->{code} ~= /^CD/ ) )
{
$exceptions++;
}
else # sub total without the video or cd costs
{
$subTot += $_->{price};
}
}
# check for cd or videos
if ( $exceptions > 0 )
{
# are there pubs as well as videos
if ( $exceptions < $totalItems )
{
# calculate shipping on sub total without video's or cds
$shippingCharge = PubCharge($subTot) + ($exceptions *
$moreCDPrice);
}
else # calculate using only videos and cds
{
# first one has a different price
$exceptions--;
$shippingCharge = $firstCDPrice + ($exceptions * $moreCDPrice);
}
}
else
{
# no videos, calculate shipping on cost
$shippingCharge = PubCharge($subTot)
}
# return the shipping cost
return $shippingCharge;
}
sub PubCharge
{
my $amount = $_[0];
my $shipping = 0;
# this is a small table to check
ShipBlock:
{
if ( $amount <= 3 ) { $shipping = 1; last ShipBlock; }
if ( $amount > 3 && $amount <= 5) { $shipping = 2; last ShipBlock; }
if ( $amount > 5 && $amount <= 10) { $shipping = 5; last
ShipBlock; }
if ( $amount > 10 && $amount <= 25) { $shipping = 6; last
ShipBlock; }
if ( $amount > 25 && $amount <= 40) { $shipping = 7; last
ShipBlock; }
if ( $amount > 40 && $amount <= 75) { $shipping = 8; last
ShipBlock; }
if ( $amount > 75 && $amount <= 100) { $shipping = 9; last
ShipBlock; }
if ( $amount > 100 ) { $shipping = ($amount * 0.11); last
ShipBlock; }
$shipping = 9999;
}
return $shipping;
}
EOF
--------------------------------------
I have run the code (slightly modified) directly under perl and it
works. What do I need to change to make it work with Interchange and the
Safe.pm module.
Thanks for the help,
---------------------------------------------------------------
Jim Boyer
Systems Programmer
Information Dept
College of Agriculture and Home Economics
Washington State University