VortexCortex
11-13-2005, 07:28 PM
I'm having a problem using the pack / unpack functions with fixed length records in a file. The appendUser function works fine. The error happens when I try to use the value returned by the unpack function.
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);
#File containing fixed length records.
my $logFile = 'allusers.log';
#array containing IP
my @ip = split(/[.]/,$ENV{'REMOTE_ADDR'});
#current time
my $time = time;
#IP[0], IP[1], IP[2], IP[3], Time, UserIDNumber, UserName, Access, PW, Posts, Votes, Score, Joined, Quote, PW Rest Question, PW Reset Ans
my $userFormat = 'C4L2a20Ca13L4a128a64a13';
my @user = ($ip[0],$ip[1],$ip[2],$ip[3],$time,1234,'VortexCortex',0x1,'password',15,5,30,$time,'Quote','Question','Answer');
my $recordLen = length(pack($userFormat));
sub appendUser{
my @ar = @{$_[0]};
open LOG, '>', $logFile; #clobers file for testing purposes. will change to +< later.
binmode LOG;
flock LOG, LOCK_NB;
seek LOG, 0, SEEK_END;
syswrite(LOG,pack($userFormat, @ar));
flock LOG, LOCK_UN;
close LOG;
}
appendUser(\@user);
sub showUser{
open LOG, '<', $logFile;
binmode LOG;
flock LOG, LOCK_NB;
seek LOG, 0, SEEK_SET;
my $data = '';
print "Bytes Read:".sysread(LOG,$data,$recordLen)."<br />";
my @ul = unpack($userFormat, $data);
#commenting out the next 3 lines will allow the code to run without error.
for (@ul){
print $_."<br />\n";
}
flock LOG, LOCK_UN;
close LOG;
}
print<<"endHtml";
Content-type:text/html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head></head><body>
endHtml
#print the user record.
showUser();
print "</body></html>";
Here's a link to the above code: example.cgi (http://cgi.freewebs.com/vortexcortex/cgi-bin/example.cgi)
My guess is that I'm not using the unpack function properly.
Thanks in advance for your help.
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);
#File containing fixed length records.
my $logFile = 'allusers.log';
#array containing IP
my @ip = split(/[.]/,$ENV{'REMOTE_ADDR'});
#current time
my $time = time;
#IP[0], IP[1], IP[2], IP[3], Time, UserIDNumber, UserName, Access, PW, Posts, Votes, Score, Joined, Quote, PW Rest Question, PW Reset Ans
my $userFormat = 'C4L2a20Ca13L4a128a64a13';
my @user = ($ip[0],$ip[1],$ip[2],$ip[3],$time,1234,'VortexCortex',0x1,'password',15,5,30,$time,'Quote','Question','Answer');
my $recordLen = length(pack($userFormat));
sub appendUser{
my @ar = @{$_[0]};
open LOG, '>', $logFile; #clobers file for testing purposes. will change to +< later.
binmode LOG;
flock LOG, LOCK_NB;
seek LOG, 0, SEEK_END;
syswrite(LOG,pack($userFormat, @ar));
flock LOG, LOCK_UN;
close LOG;
}
appendUser(\@user);
sub showUser{
open LOG, '<', $logFile;
binmode LOG;
flock LOG, LOCK_NB;
seek LOG, 0, SEEK_SET;
my $data = '';
print "Bytes Read:".sysread(LOG,$data,$recordLen)."<br />";
my @ul = unpack($userFormat, $data);
#commenting out the next 3 lines will allow the code to run without error.
for (@ul){
print $_."<br />\n";
}
flock LOG, LOCK_UN;
close LOG;
}
print<<"endHtml";
Content-type:text/html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head></head><body>
endHtml
#print the user record.
showUser();
print "</body></html>";
Here's a link to the above code: example.cgi (http://cgi.freewebs.com/vortexcortex/cgi-bin/example.cgi)
My guess is that I'm not using the unpack function properly.
Thanks in advance for your help.