Hi All,
OK this is a perl Q not Linux but it's Sunday night. :-)
Am having probs opening several files in a perl script using indirect
filehandles.
#!/usr/bin/perl -w
################################################################
# This script reads the ASF's Apache log files and extracts accesses by
specific users. These accesses are regularly emailed to the users by a
cron
script.
################################################################
use strict;
use FileHandle;
my $fileUsers="users"; # I use IN for the file handle for this.
my $fh;
my $fhUser; # File handle for files.
# Here is where I open a user list and read it in. Im used to this way.
# I am used to using direct filehandles like this ie IN.
open (IN, "$fileUsers") || die "Cannot open file $fileUsers for
reading\!";
while(<IN>) {
($users_login, $users_name, $users_email) = split(/:/, $_);
push(@users, $users_login);
}
close (IN) || die "Cannot close file $fileUsers\!";
All the above is OK.
Now here I have probs. For each user I want to open a file with the
filename being the same as the name of the user.
I want to have this file open for the duratiion of parsing an Apache
log file and write to each one so I need a filehandle for each user
file.
If I try this below:
foreach $user (@users)
{
$fhUser = "handle".$user;
open ($shUser, ">> $user") || die "Cannot open file $user for
reading\!";
print $fhUser "Hi $user\n";
}
I get the error upon running of:
"Can't use string ("handlemike") as a symbol ref while "strict refs" in
use at ./process line 90."
So I tried this from the Programming Perl book, chap 7 using the
FileHandle method:
foreach $user (@users)
{
$fh = new FileHandle ">> $user";
$fhUser = $fh.$user;
if (defined $fhUser) {
print $fhUser "Hi $user\n";
print $fhUser $subject;
print $fhUser "Date File from IP Address\n";
}
else {
print "Cannot open file $user for writing\!";
}
}
and I get
"Can't use string ("FileHandle=GLOB(0x1202e1600)mike") as a symbol ref
while "strict refs" in use at ./process line 82."
Then all I want to do is the processing....
while (<>)
{
Here is where I do processing and writing stuff to those open
files....
print $fhUser "$words[3] $words[4] $words[6] from $words[0]\n";
etc......
}
and then close all those open files...
foreach $user (@users)
{
print "Total logins: $users{$user}\n";
$fhUser->close;
}
It's just that I have not used indirect filehandles before and the
Learning Perl book only has examples of direct filehandles.
Mike
--
--------------------------------------------------------------------
Michael Lake
Active caver, Linux enthusiast and interested in anything technical.
Safety Convenor, Australian Speleological Federation
Owner, Speleonics (Australia)
--------------------------------------------------------------------
--
SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/
More Info: http://slug.org.au/lists/listinfo/slug