Gossamer Forum
Home : General : Perl Programming :

What did i do wrong? New to Perl

Quote Reply
What did i do wrong? New to Perl
Hi all,

I have a flat file database, I have one field is startdate, if startdate is greater than 90 days it will be black, between 60 and 90 is red, between 30 and 60 days is yellow and less than 30 days is green.

Here is my code
<code>
#!/opt/WWW/tools/perl5/perl



$updatetime=`cat lastupdate`;

use Time::Local;
$today = timelocal(localtime);
$Total=`cat pa.TXT | wc -l`;


sub date_to_unix {

my $date = @_[0];
my ($time);

my ($mon, $day, $year) = split(/\//, $_[0]);
my ($hour, $min, $sec) = split(/:/, $_[0]);
unless ($day and $mon and $year) { return undef; }
use Time::Local;
eval {
$mon = int($mon) - 1; $day = int($day); $year = int($year) - 1900;
$time = timelocal($hour, $min, $sec, $day, $mon, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}





my $count = 0;
my $countgt30 = 0;
my $countlt60 = 0;
my $countgt90 = 0;


open (DATA,"pa.TXT") || die ("Can't Open data File \n");
@data=<DATA>;
close DATA;

&header_response;
$x=0;
foreach $line (@data) {

$x++;
($Router, $LogOp, $SFC , $Priority, $site, $itemgroup, $itemid, $nc, $shoporder, $status, $starttime)=split(/\|/,$line);

my $epdate = &date_to_unix($starttime);

if ($epdate >= ($today - 2592000)) { # within than 30 days

print "<TR><TD>$x</TD><TD>$Router</TD><TD>$LogOp</TD><TD>$SFC</TD><TD>$itemgroup&nbsp;</TD><TD>$itemid&nbsp;</TD><TD>$nc&nbsp;</TD><TD>Less 30 days</TD><TD>$epdate</TD><TD bgcolor=00ff00>$starttime&nbsp;</TD></TR> \n";
$count++;
}
elsif (($epdate >= ($today - 5184000)) && ($epdate < ($today - 2592000))) { # more than 30 days
print "<TR><TD>$x</TD><TD>$Router</TD><TD>$LogOp</TD><TD>$SFC</TD><TD>$itemgroup&nbsp;</TD><TD>$itemid&nbsp;</TD><TD>$nc&nbsp;</TD><TD>30 and 60</TD><TD>$epdate&nbsp;</TD><TD bgcolor=yellow>$starttime&nbsp;</TD></TR> \n";
$countgt30++;
}
elsif (($epdate >= ($today - 7776000)) && ($epdate < ($today - 5184000))) { # less than 60 days
print "<TR><TD>$x</TD><TD>$Router</TD><TD>$LogOp</TD><TD>$SFC</TD><TD>$itemgroup&nbsp;</TD><TD>$itemid&nbsp;</TD><TD>$nc&nbsp;</TD><TD>60 and 90</TD><TD>$epdate</TD><TD bgcolor=red><font color=white>$starttime&nbsp;</font></TD></TR> \n";
$countlt60++;
}
elsif ($epdate < ($today - 7776000)) { # more than 90 days
print "<TR><TD>$x</TD><TD>$Router</TD><TD>$LogOp</TD><TD>$SFC</TD><TD>$itemgroup&nbsp;</TD><TD>$itemid&nbsp;</TD><TD>$nc&nbsp;</TD><TD>more than 90</TD><TD>$epdate</TD><TD bgcolor=black><font color=yellow>$starttime&nbsp;</font></TD></TR> \n";
$countgt90++;
}
}
&footer_response;

####################################################################################################

sub split_decode {
# --------------------------------------------------------
# Takes one line of the database as input and returns an
# array of all the values. It replaces special mark up that
# join_encode makes such as replacing the '``' symbol with a
# newline and the '~~' symbol with a database delimeter.

my ($input) = shift;
$input =~ s/\Q$db_delim\E$/$db_delim /o; # Add a space if we have delimiter new line.
my (@array) = split (/\Q$db_delim\E/o, $input);
for ($i = 0; $i <= $#array; $i++) {
$array[$i] =~ s/~~/$db_delim/og; # Retrieve Delimiter..
$array[$i] =~ s/``/\n/g; # Change '' back to newlines..
}
return @array;
}

sub header_response
{
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>test </TITLE>\n";

print "</HEAD>\n";
print qq^<BODY BGCOLOR="#ffffff" TEXT="blue" LINK="#FF6633" VLINK="#FF9933" ALINK="#336699">\n^;
print "$updatetime \n";


print "<BR>\n";
print "<CENTER><a href=#colorcode><IMG SRC='../images/color.gif' border=0></a></CENTER>\n";
print "<BR>\n";


print "<BR><center><FONT size=+1><TABLE BORDER=1><TR bgcolor=#666699><TD><P align=right><font color=white>Qty</P></TD><TD><center><font color=white>Router</center></TD><TD><font color=white><center>LogOp</center></TD><TD><font color=white><center>SFC #</center></TD><TD><font color=white><center>Item <BR>Group</center></TD><TD><font color=white><center>Item ID</center></TD><TD><font color=white><center>No <BR>Of<BR>NCs </center></TD><TD><font color=white><center>Check Date</center></TD><TD><font color=white><center>Epochstartdate</center></TD><TD><font color=white><center>Start Time</center></TD></TR>\n";
return;
}

sub footer_response
{
print "</TABLE></font></CENTER>\n";
print "<BR><BR>\n";
print "<center><a name=colorcode><TABLE WIDTH=600 BORDER=0 CELLPADDING=4 CELLSPACING=3 ><TR VALIGN=center><font color=black size=+1><B> Total $Total PAs opened in SFE</font></TR><TBODY><TR VALIGN=BOTTOM><TD WIDTH=290 bgcolor=00ff00><P ALIGN=LEFT><font color=blue> $count PAs are opened within 30 days </B></font></P></TD><TD WIDTH=290 bgcolor=yellow><P ALIGN=LEFT><font color=blue>$countgt30 PAs are opened &gt; 30 and &lt; 60 days </font></P></TD></TR><TR><TD WIDTH=290 VALIGN=BOTTOM bgcolor=red><P ALIGN=LEFT><font color=white>$countlt60 PAs are opened &gt; 60 and &lt; 90 days </font></P></TD><TD WIDTH=290 VALIGN=TOP bgcolor=000000><P ALIGN=LEFT> <font color=yellow> $countgt90 PAs are opened &gt; 90 days</font></P></TD></TR></TBODY></TABLE></center>\n\n";

print "<BR><BR>\n";


print " </BODY></HTML>\n";
return;



}
</code>
and here is my flat file:
<code>
Para17537 |WYU |226H28EF | 999|BGH |AVALANCHE |595-6510-01 | 0|PA17537A4 |In |07/09/2002 03:10:14
Para17537 |WYU |226H28EC | 999|BGH |AVALANCHE |595-6507-01 | 0|PA17537A1 |In |07/09/2002 03:10:13
Para17537 |WYU |226H28EE | 999|BGH |AVALANCHE |595-6509-01 | 0|PA17537A3 |In |07/09/2002 03:10:14
Para17537 |WYU |226H28ED | 999|BGH |AVALANCHE |595-6508-01 | 0|PA17537A2 |In |07/09/2002 03:10:14
Sacra16044 |BFA |5015777012150 | 5|BGH |HFGD |501-5777-15_PA16044 | 0|52401-001 |In |05/30/2002 23:22:49
Sacra16044 |HJO |5015777012083 | 5|BGH |HFGD |501-5777-15_PA16044 | 2|52401-001 |In |05/30/2002 23:22:19
Sacra16179 |HJO |5015777008990 | 5|BGH |HFGD |501-5777-16_PA16179 | 2|61601-001 |In |09/30/2000 22:31:50
Sacra16179 |HJO |5015777008974 | 5|BGH |HFGD |501-5777-16_PA16179 | 2|61601-001 |In |06/22/2001 15:35:15
</code>

How can the color is black when it have blank $epdate and why it is blank?

Thanks for your help.
Quote Reply
Re: [britneythuyduong] What did i do wrong? New to Perl In reply to
Your unix time function was a bit broken.

So I had some fun. Smile See attached file argh.pl

Added test_output.html so you may see what it looks like.

I found it a little bit easier to use Date::Calc to find the days since today.

Hope you have Date::Calc installed.

Hope this helps.

Sorry , if this is not quite what you wished for.

Thanks

Kode

Last edited by:

kode: Jul 22, 2002, 7:51 PM
Quote Reply
Re: [kode] What did i do wrong? New to Perl In reply to
Version 2 :

New and Improved some what.

Read the attached argh2.pl and see test_output2.html

Thanks

Kode
Quote Reply
Re: [kode] What did i do wrong? New to Perl In reply to
Hi Kode,

Thank you very much, i will check it out.

I have no access to web server(Solaris) to install Calc.pm , any idea i can run your script but it still work ?

Code:
webhome: 2% colorviewpa.cgi
Can't locate Date/Calc.pm in @INC at colorviewpa.cgi line 5.
BEGIN failed--compilation aborted at colorviewpa.cgi line 5.

Thanks,
Quote Reply
Re: [britneythuyduong] What did i do wrong? New to Perl In reply to
You can install it locally into your cgi-bin. Just download it from cpan.org and upload Calc.pm into cgi-bin/Date, then add:

use lib '.';

...into the cgi-script.

cgi-bin/colorviewpa.cgi
cgi-bn/Date/Calc.pm

Last edited by:

Paul: Jul 23, 2002, 1:02 PM
Quote Reply
Re: [britneythuyduong] What did i do wrong? New to Perl In reply to
Hello britneythuduong , Hope you understand the scribble.

After a little sleep, I realised I missed a bit or 2.

for the conditions I should have used :

Code:
if (($epdate >= 0 ) && ($epdate <= 30 )) { # 0 to 30 days
$count++;
$msg ="Less 30 days"; # Message for displaying
$kolor1 ="lime"; # TD back ground color
$kolor2 ="black"; # TEXT color
}
elsif (($epdate >= 31 ) && ($epdate <= 60 )) { # 31 to 60 days
$countgt30++;
$msg ="30 and 60";
$kolor1 ="yellow";
$kolor2 ="black";
}
elsif (($epdate >= 61 ) && ($epdate <= 90 )) { # 61 to 90 days
$countlt60++;
$msg ="60 and 90";
$kolor1 ="red";
$kolor2 ="white";
}
elsif ( $epdate >= 91 ) { # 91 or more days
$countgt90++;
$msg ="more than 90";
$kolor1 ="black";
$kolor2 ="white";
}
else { # if no match use this default
$msg ="unknown";
$kolor1 ="purple";
$kolor2 ="yellow";
}


Just in case the date data changes/fails some time in the future.

Tested with 06/22/3304 15:35:15 and this is where the else works, a date in the future.

Should also consider opening data file with a file lock.

Writing perl code is like nailing Jello to a wall. So have fun and experiment.

Thanks

Kode

Last edited by:

kode: Jul 23, 2002, 1:48 PM
Quote Reply
Re: [Paul] What did i do wrong? New to Perl In reply to
Hi Paul and Kode,

I already down load Date-Calc-5.0.tar.gz and unzip it. Then I changed Date-Calc-5.0 to Date, but i can not install it.

Code:
webhome: 5% cd Date
webhome: 6% ls
Artistic.txt Calc.pod DateCalc.c GNU_LGPL.txt Makefile.PL examples tools
CHANGES.txt Calc.xs DateCalc.h INSTALL.txt README.txt lib typemap
CREDITS.txt Calendar.pm EXAMPLES.txt MANIFEST TOOLS.txt patchlevel.h
Calc.pm Calendar.pod GNU_GPL.txt Makefile ToolBox.h t
webhome: 7% perl Makefile.PL
Warning: prerequisite Bit::Vector 5.7 not found at (eval 1) line 220.
Writing Makefile for Date::Calc
Writing patchlevel.h for perl (5.00503)
webhome: 8% make
mkdir blib
mkdir blib/lib
mkdir blib/lib/Date
mkdir blib/arch
mkdir blib/arch/auto
mkdir blib/arch/auto/Date
mkdir blib/arch/auto/Date/Calc
mkdir blib/lib/auto
mkdir blib/lib/auto/Date
mkdir blib/lib/auto/Date/Calc
mkdir blib/man3
cp lib/Date/Calendar/Profiles.pod blib/lib/Date/Calendar/Profiles.pod
cp Calc.pod blib/lib/Date/Calc.pod
cp lib/Carp/Clan.pm blib/lib/Carp/Clan.pm
cp Calendar.pm blib/lib/Date/Calendar.pm
cp lib/Date/Calc/Object.pm blib/lib/Date/Calc/Object.pm
cp lib/Date/Calendar/Year.pm blib/lib/Date/Calendar/Year.pm
cp lib/Carp/Clan.pod blib/lib/Carp/Clan.pod
cp lib/Date/Calendar/Profiles.pm blib/lib/Date/Calendar/Profiles.pm
cp Calendar.pod blib/lib/Date/Calendar.pod
cp lib/Date/Calc/Object.pod blib/lib/Date/Calc/Object.pod
cp Calc.pm blib/lib/Date/Calc.pm
cp lib/Date/Calendar/Year.pod blib/lib/Date/Calendar/Year.pod
/usr/bin/perl -I/usr/perl5/5.00503/sun4-solaris -I/usr/perl5/5.00503 /usr/perl5/5.00503/ExtUtils/xsubpp -typemap /usr/perl5/5.00503/ExtUtils/typemap -typemap typemap Calc.xs >xstmp.c && mv xstmp.c Calc.c
cc -c -xO3 -xdepend -DVERSION=\"5.0\" -DXS_VERSION=\"5.0\" -KPIC -I/usr/perl5/5.00503/sun4-solaris/CORE Calc.c
/usr/ucb/cc: language optional software package not installed
make: *** [Calc.o] Error 1
webhome: 9% make test
cc -c -xO3 -xdepend -DVERSION=\"5.0\" -DXS_VERSION=\"5.0\" -KPIC -I/usr/perl5/5.00503/sun4-solaris/CORE Calc.c
/usr/ucb/cc: language optional software package not installed
make: *** [Calc.o] Error 1
webhome: 10% make install
cc -c -xO3 -xdepend -DVERSION=\"5.0\" -DXS_VERSION=\"5.0\" -KPIC -I/usr/perl5/5.00503/sun4-solaris/CORE Calc.c
/usr/ucb/cc: language optional software package not installed
make: *** [Calc.o] Error 1
webhome: 11% cd ..
webhome: 12% colorviewpa.cgi
Can't locate loadable object for module Date::Calc in @INC (@INC contains: . . /usr/local/lib/perl5/5.00503/sun4-solaris /usr/local/lib/perl5/5.00503 /usr/local/lib/perl5/site_perl/5.005/sun4-solaris /usr/local/lib/perl5/site_perl/5.005 .) at colorviewpa.cgi line 5
BEGIN failed--compilation aborted at colorviewpa.cgi line 5

What should i do ? Please help. Thanks.Blush
Quote Reply
Re: [britneythuyduong] What did i do wrong? New to Perl In reply to
Hello britneythuyduong,

To use the Date::Calc library as a local to your cgi-bin :

you need off your cgi-bin :

/date/calc.pm

/date/calc/object.pm

/date/calc/object.pod -- documentation ? optional

Code:
# to use this local library I added to the script :# ADDED this module to calculate difference in days
use lib 'date';
use Date::Calc qw(Today Delta_Days); # using 2 functions from Date::Calc


I tested this under WIN 98SE with Apache web server.

If you cannot extract the required files please provide an email where I can send a zip with

the required files. I am using Perl 5.6.0 by Active State. Date::Calc version 5.0.

Thanks

Kode

p.s. It is always better to see if your service provider will add a module then there may be no

perl version conflicts.

Last edited by:

kode: Jul 24, 2002, 2:20 PM
Quote Reply
Re: [kode] What did i do wrong? New to Perl In reply to
Are you sure that worked?

You have the case wrong, but also you should be using:

use lib '.';

..rather than:

use lib 'date';

...otherwise perl will be looking for:

date/Date/Calc.pm

...instead of:

Date/Calc.pm
Quote Reply
Re: [Paul] What did i do wrong? New to Perl In reply to
Hello Paul,

Yes Solaris is case sensitive , I should have mentioned that and used 'Date'. Good point.

But I did state I tested on WIN 98SE which is not case sensitive.

I tried using your use lib '.'; and it did work but since I was kinda thinking specific I used 'date'.

I should have used 'Date'.

I suggested asking his service provider to add the module as they may change / upgrade their

perl at some point in time and leave him with a broken script / library.

I did test this and it did work on my local machine.

I have lowered my Vogon force field and set Apache for my current IP so for the next hour

you can see this at http://142.173.103.96/cgi-bin/argh3.pl [ I will try to edit out this IP in 45 minutes ]

[ For people seeing this after July 24, 2002 at 14:45 + 1 hour the door is 127.0.0.1 ]

Thanks

Kode
Quote Reply
Re: [kode] What did i do wrong? New to Perl In reply to
>>
But I did state I tested on WIN 98SE which is not case sensitive.
<<

Hmm are you sure about that also?...I use XP and everything is case sensitive as on *nix. For example using:

use cgi;

...would not work but:

use CGI;

...would.
Quote Reply
Re: [Paul] What did i do wrong? New to Perl In reply to
Dear Paul, It works on my machine.

I have provided a link so you can see this.

What can I do to show you it works ?

Thanks

Kode


Pledge :

I will try in the future to use case sensitive words so that where it matters it should work.

Last edited by:

kode: Jul 24, 2002, 3:50 PM