Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl write to filehandle with two formats

Status
Not open for further replies.

mental

Technical User
May 30, 2000
2
US
I have the following script, from the Perl Cookbook. I am trying to output the table to a file, instead of STDOUT, in the sub write_report. Can someone suggest a way to do this?

======= sum#!/usr/bin/perl -w
# sum sumarize web server log activity
# From Perl Cookbook pp728-729

$lastdate = "";
daily_logs();
summary();
exit;

# read CLF files and tally hits from the hist and to the URL
sub daily_logs {
while (<>) {
($type, $what) = /"(GET|POST)\s+(\S+?) \S+"/ or next;
($host, undef, undef, $datetime) = split;
($bytes) = /\s(\d+)\s*$/ or next;
($date) = ($datetime =~ /\[([^:]*)/);
$posts += ($type eq POST);
$home++ if m, / ,;
if ($date ne $lastdate) {
if ($lastdate) { write_report() }
else { $lastdate = $date }
}
$count++;
$hosts{$host}++;
$what{$what}++;
$bytesum += $bytes;
}
write_report() if $count;
}

# use *typeglob aliasing of global variables for cheap copy
sub summary {
$lastdate = "Grand Total";
*count = *sumcount;
*bytesum = *bytesumsum;
*hosts = *allhosts;
*posts = *allposts;
*what = *allwhat;
*home = *allhome;
write;
}
# display the tallies of hosts and URLs, using formats
sub write_report {
open (STDOUT, ">temp.html");
open (STDOUT_TOP, ">>temp.html");
write STDOUT;
# add to summary data
$lastdate = $date;
$sumcount += $count;
$bytesumsum += $bytesum;
$allposts += $posts;
$allhome += $home;
# reset daily data
$posts = $count = $bytesum = $home = 0;
@allwhat{keys %what} =keys %what;
$allhosts{keys %hosts} =keys %hosts;
%hosts = %what = ();
}

format STDOUT_TOP =
@|||||||||||| @|||||| @||||||| @||||||| @|||||| @|||||| @||||||||||||
"Date", "Hosts", "Accesses", "Unidocs", "POST", "Home", "Bytes"
------------- ------- -------- -------- ------- ------- -------------

.

format STDOUT =
@>>>>>>>>>>> @>>>>>> @>>>>>>> @>>>>>>> @>>>>>> @>>>>>> @>>>>>>>>>>>>
$lastdate, scalar(keys %hosts), $count, scalar(keys %what), $posts, $home, $bytesum

.
 
not tested:
Code:
open FH, ">myfile.txt" or die "can not open file";
write FH;
...
format FH_TOP = 
...
 
Thank you uida1154:
A refresher at Roberts Perl Tutorial showed me the wisdom of your answer.

William Branson
Administrator, Systems Planning
Chicago Transit Authority
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top