scratchpad
ikkon
I am having problem trying to write user info to a file then be able to retrieve that info in a hash.
here is the pl file:
<code>
#!/usr/bin/perl -w
use lib 'modules';
use ChatUtils;
use strict;
use CGI qw(fatalsToBrowser);
use warnings;
use diagnostics;
my $q = new CGI;
print $q->header( "text/plain" );#C:\WEB_ROOT\PerlChat\userdir
my $UtilObj = new ChatUtils({
UserDirectory => '/userdir'
});
my %userProps = (
LoginName => 'Ikkon',
LoginPass => 'Chaos',
UserName => 'Ben'
);
my $userDir = $UtilObj->{'UserDirectory'};
$UtilObj->RegisterUser(\%userProps);
my %getUserInfo = $UtilObj->getUserInfo($userDir);
while ( my ($key, $value) = each(%getUserInfo) ) {
print "$key => $value\n";
}
</code>
#### ChatUtil.pm
<code>
package ChatUtils;
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = '1.0';
sub new
{
my ($class, $prop) = @_;
my $self = {
'UserDirectory' => $prop->{'UserDirectory'} || undef
};
bless($self);
return $self;
}
sub RegisterUser{
my $self = shift;
#--> Hash Reference to User Properties
#-------------------------------------->
my $userProps = shift;
my $userFileName = q{\$userProps->{'LoginName'}.usr};
$self->{'UserFile'} = $userFileName;
#--> Make a fuul path to the user file for creation
#--------------------------------------------------->
my $userFile = $self->{UserDirectory}.$userFileName;
#--> Print User information to a file, this becomes the user file
#------------------------------------------------------------------>
$self->write_file_Hash($userFile, %$userProps);
}
sub getUserInfo {
my $self = shift;
my $f = shift;
my %userInfo;
my $fullFilePath = $f.$self->{'UserFile'};
open (FILE, "< $fullFilePath");
while (<FILE>){
chomp; # no newline
s/#.*//; # no comments
s/^\s+//; # no leading white
s/\s+$//; # no trailing white
next unless length; # anything left?
my ($var, $value) = split(/\s*=\s*/, $_, 2);
$userInfo{$var} = $value;
}
close FILE;
return %userInfo;
}
sub write_file_Hash {
my $self = shift;
#--> Grab incoming Arguments
#---------------------------->
my ($file,%data) = @_;
#--> Make Data hash empty unless something is in it, don't want any undefines
#------------------------------------------------------------------------------>
%data = () unless %data;
#--> Open file and print hash in the format: Key=Value
#------------------------------------------------------->
open (FH, ">> $file");
while ( my ($key, $value) = each(%data) ) {
print FH "$key=$value\n";
}
close FH;
}
1;
__END__
</code>
I am not sure what I am doing wrong here is the error i am getting:
readline() on closed filehandle FILE at modules/ChatUtils.pm line 45 (#2)
(W closed) The filehandle you're reading from got itself closed sometime
before now. Check your control flow.
Content-Type: text/plain; charset=ISO-8859-1