Michael Lake was once rumoured to have said:
> 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.

[Snip]
> 
> 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\!";
>         }       
> }

Drop the $fhUser bit,  - this section is *all* wrong.  you use:

print $fh  ....

to print to the file, you don't mangle the object rej.

But given what it looks like you're trying to do, you probably want to
keep many filehandles open at once.. [one for each user], you probably
want a construct like:

my %userfh;
foreach my $user (@users) {
        $userfh[$user] = new FileHandle ">> $user"; # note, this is WRONG.
        if (defined $userfh[$user]) {
          ....
        }
}

you can then close all of these filehandles using:

foreach my $user (keys %userfh) {
        $userfh[$user]->close;
}



> 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......
> }

Looks fine... 'cept that $fhUser bit should be $userfh[$user] if I'm
understanding what you're trying to do.

> 
> and then close all those open files...
> foreach $user (@users) 
> {
>         print "Total logins: $users{$user}\n";
>         $fhUser->close;
> }

More crack.  $foo{blah} construcuts are valid for hashes, not arrays.

> It's just that I have not used indirect filehandles before and the
> Learning Perl book only has examples of direct filehandles.

You've got more than just that wrong.

I seriously suggest you brush up on Perl Operators, datatypes and
start using perldoc more.

C.
-- 
--==============================================--
  Crossfire      | This email was brought to you
  [EMAIL PROTECTED] | on 100% Recycled Electrons
--==============================================--

-- 
SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/
More Info: http://slug.org.au/lists/listinfo/slug

Reply via email to