Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

ikkon's scratchpad

by ikkon (Monk)
on Jan 17, 2007 at 17:18 UTC ( #595105=scratchpad: print w/replies, xml ) Need Help??

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:
#!/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"; }
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'} || unde +f }; 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 an +y 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__
I am not sure what I am doing wrong here is the error i am getting: readline() on closed filehandle FILE at modules/ 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
Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-02-19 22:08 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (293 votes). Check out past polls.