[Date Prev][Date Next][Thread Prev][Thread Next][Minivend by date
][Minivend by thread
]
Re: [mv] Trouble getting piece of perl script working HELP!
See below...
On Fri, 24 Sep 1999, Jarno Niemela wrote:
> ****** message to minivend-users from Jarno Niemela <jargon@iki.fi> ******
>
> Hello.
>
> I have following piece of perl code in my page, which I use
> to calculate bank reference numbers. But for the life of me I just cant
> get it working!
>
> [perl scratch]
> $receipt_nr = '123456';
>
> @multipliers = (7,3,1);
>
> if ($receipt_nr =~ /([0-9]*)/)
> {
> $receipt_nr = $1;
> }
Careful here. I think you really want:
if($receipt_nr =~ m/^([0-9]+)$/)...
What you have will match all the following:
TTTTT
1AAAA
a45564g
The '*' will let the expression match null. Because you haven't anchored
the regular expression, it is free to find a match anywhere within the
$receipt_nr string. Also, because you haven't anchored it to the end
(with the $) character, it will simply stop when it runs out of numeric
characters. Thus '1AAA' will match and $1 will be '1'.
This regex stuff is very hard to get right. I am always finding ways to
break my regexes :-(
If you think that the receipt number will contain characters other than
numbers, you can use tr/// to get rid of them:
$receipt_nr =~ tr/[0-9]//cs;
Check that before you try it, I can't remember if you need the brackets or
if the 'cs' modifier is correct. After that call, $receipt_nr will only
contain numeric characters.
>
> $i = (length $receipt_nr) - 1;
Length in Perl does not include a null termination character, so why the
-1?
> if ($i < 0 or $i > 20)
> {
> exit;
> }
>
> @merkit = split //,$receipt_nr;
>
> $k = 0;
> $sum = 0;
>
> for ($j=$i; $j > -1;$j--)
>
> {
> if ($k == 3) { $k=0 };
>
> $sum = @merkit[$j] * @multipliers[$k] + $sum;
> $k++;
> }
>
> $tens = 0;
>
> while ($tens <= $sum)
> {
> $tens = $tens + 10;
> }
>
> $checksum = $tens - $sum;
>
> $bank_ref = join '',$receipt_nr,$checksum;
> $Safe{'scratch'}{'ref'}=$bank_ref;
>
> [/perl]
>
> When I run the same code as a normal perl script it works fine.
>
> But on minivend it just doesn't work!
>
> I try to output it using following tag
> Reference : [scratch ref]
>
> I'm using
> Minivend 3.14-3
> Linux (Redhat 6.0)
> Perl 5.005_03
> Apache 1.39
>
> If I try just some simple test routine, everything works fine.
> What I'm doing that minivend cant digest?
>
> And does anyone know any alternative way of doing some calculating in
> minivend?
> For example calling external java program (I prefer java over perl)
>
>
>
> Things I've tried
>
> [perl args=sratch]
>
> Following user tag in minivend.cfg
>
> UserTag realperl HasEndTag
> UserTag realperl CanNest
> UserTag realperl
> Interpolate
> UserTag realperl Routine <<EOF
> sub {
> my $str = eval {
>
> eval $_[0] or die;
> };
> }
> EOF
>
> and then
> [realperl]
> [perl scracth]
> ....
Try making this a UserTag:
[checksum account='[value account]' ... ]
UserTag checksum ORDER account ...
UserTag checksum ROUTINE <<EOR
sub {
...
do calculations here
...
set scratch variable.
}
EOR
Within the UserTag, I think you use something like:
$Vend->{'scratch'}->{'ref'} = $result;
You might have to look around in the code a little to get that correct...
Some of the Perl seems like it might not be doing what you want. It may
work for you hard-coded value, but it might not for other things.
I have never had much luck with scratch variables. Probably I am just
missing some important point. However, I have used 'value' variables with
much success.
Best,
Kyle (KH)