Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

typglobs and filehandles

by Anonymous Monk
on Jul 23, 2002 at 03:04 UTC ( [id://184312]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a hard time successfully writing to filehandles stores in variables. here's how it goes:
# # open report files for printing my $filepath = "/usr/openv/scripts"; open *NTOUT, ">>$filepath/nt.html" or die "cannot open nt file for +writing: $!\n"; open *UNIXOUT, ">>$filepath/unix.html" or die "cannot open unix file +for writing: $!\n"; open *MAILOUT, ">>$filepath/mail.html" or die "cannot open mail file +for writing: $!\n"; # # some unrelated code ...... # foreach my $class ( sort keys %classHash ) { if ( $classHash{$class} = "NT" ) { $outhandle = *NTOUT; } if ( $classHash{$class} = "UNIX" ) { $outhandle = *UNIXOUT; } if ( $classHash{$class} = "MAIL" ) { $outhandle = *MAILOUT; } foreach my $status ( sort keys %{ $classHash{$class} } ) { if ( $status eq "success" ) { print $outhandle <<SUC; <table border=1 width="100%"> <tr> <td colspan="12" align="left" valign="center" bgcolor="#33cc +33"> <b><font size="+1">$class: Successful Jobs</b></font> </td> </tr> SUC
...the print statement print $outhandle (as well as subsequent others) don't actually print to the filehandle. The problem is not with %classHash; I've checked that it is being populated as I expect. But, if I do this:
@fh = ( *NTOUT,*UNIXOUT, *MAILOUT ); foreach (@fh) { print $_ <<EOC; <html> <head> <title>[Backup report] $humanstart - $humanstop</title> </head> <body bgcolor="#ffffff"> <h2 align="left">Daily Backup Report</h2> <h3 align="left">From $humanstart to $humanstop</h3> EOC }
It does print to each filehandle. What am I doing wrong, here?

Replies are listed 'Best First'.
Re: typglobs and filehandles
by panix (Monk) on Jul 23, 2002 at 03:28 UTC
    Are you sure it's not the contents of %classHash? Your code seems to work just fine (eg, the snippet below works).
    open(TEST, ">>test.html") || die; my $outhandle = undef; $outhandle = *TEST; print $outhandle "test\n"; close($outhandle);
    If you replace the consecutive classHash tests with an if/elsif/else die, you should catch this.
Re: typglobs and filehandles
by zejames (Hermit) on Jul 23, 2002 at 03:28 UTC
    Hi

    Your mistake must be here :
    foreach my $class ( sort keys %classHash ) { if ( $classHash{$class} = "NT" ) { $outhandle = *NTOUT; } if ( $classHash{$class} = "UNIX" ) { $outhandle = *UNIXOUT; } if ( $classHash{$class} = "MAIL" ) { $outhandle = *MAILOUT; } foreach my $status ( sort keys %{ $classHash{$class} } ) {

    $classHash{$class} cannot be a string, as you seem to consider, for example, in

     if  ( $classHash{$class} = "MAIL" )

    AND a hash reference, like it is used in

    foreach my $status ( sort keys %{ $classHash{$class} } ) {

    So $status is never success, and you never write anything.

    Moreover, you must use eq to compare two strings. And == (and not =) to compare to numbers.

    BTW, you use strict; use warnings; do you ?

    HTH

    --
    zejames
Re: typglobs and filehandles
by Zaxo (Archbishop) on Jul 23, 2002 at 03:30 UTC

    In your $outhandle assignments, use references to typeglobs as filehandles, as in:

    perl -e 'my $foo = \*STDOUT; print $foo "Foo!$/"'
    Why don't you leave the files closed till you know which you need? Then you could pick the correct filename instead and open only that.
    my %platfile = ( NT => 'nt.html', UNIX => 'unix.html', MAIL => 'mail.html' ); #... { open my $out, '>>', "$filepath/$platfile{$classHash{$class}}" or die $!; # ... }
    At that point, we run into another problem with your code. What is in %classHash? You iterate through all the keys twice - nested. Surely that's not what you should be doing.

    After Compline,
    Zaxo

Re: typglobs and filehandles
by Ionitor (Scribe) on Jul 23, 2002 at 03:18 UTC
    Well, are you sure that print is ever being run? And it it is, does it print to MAILOUT each time? Here's your problem:
    if ( $classHash{$class} = "NT" )
    You're missing an equals sign, as right now you assign to $classHash{$class} three times. I suspect that fixing that problem will help you a lot.

    To avoid this in the future, you might want to write:

    if ( "NT" == $classHash{$class})
    since you can't assign to a constant string, so only having one equals sign gives you an error.

    BTW, your syntax for printing to a filehandle assigned to a scalar appears correct--at least, it works for me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://184312]
Approved by IlyaM
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found