[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] Fumbling with formats and IO:Handle.



Objective.
Read a list of records out of a database table and generate a series of roster 
files.  Each file should have an identical two line header. 

I had some help with this over on the beginners@perl.org mailing list, but I'm 
stuck.  I'll put the full current source after the post.  My first attempt 
using format for the rosters would only put the header lines on the first of 
the roster file.   

format RF =
@<<<<<<<<<<<  @< @<< @< @< @< @< @<
$name, $age, $nat, $st, $tk, $ps, $sh, $agg
.

format RF_TOP =
Name         Age Nat St Tk Ps Sh Ag KAb TAb PAb SAb Gam Sav Ktk Kps Sht Gls 
Ass  DP Inj Sus
-------------------------------------------------------------------------------------------
.

    open (RF, ">$roster_file") or die "Can't open roster file $roster_file";

    while ( ($name, $age, $nat, $st, $tk, $ps, $sh, $agg  ) = 
$sth->fetchrow_array() ) {
        write RF;
    }

    close RF;

It was suggested that I use IO:Handle and reset the position at zero everytime 
to make sure the RF_TOP was generated.  That led me to this (non-working) 
code.

format RF =
@<<<<<<<<<<<  @< @<< @< @< @< @< @<
$name, $age, $nat, $st, $tk, $ps, $sh, $agg
.

format RF_TOP =
Name         Age Nat St Tk Ps Sh Ag KAb TAb PAb SAb Gam Sav Ktk Kps Sht Gls 
Ass  DP Inj Sus
-------------------------------------------------------------------------------------------
.

    my $io = new IO::Handle;
    $io->fdopen(fileno(RF),"w");
    $io->format_lines_left(0);

    while ( ($name, $age, $nat, $st, $tk, $ps, $sh, $agg  ) = 
$sth->fetchrow_array() ) {
        write $io;
    }

    #close RF;
    undef $io;

}

So... what do I need to tweak to get the RF_TOP to get written at the 
beginning of every file?

Entire program below.   

-----------start-------------------
#!/usr/bin/perl

use strict;
use warnings;

use DBI;
use Getopt::Long;
use IO::Handle;
our ($opt_league, $opt_div);


&GetOptions("league=s", "div=s");

print "Working on the $opt_league league, division $opt_div\n";

#connect to database
my $dbh = DBI->connect("DBI:mysql:database=efl",
                      'root',
                      'Sournak0',
                      ) or die "Can't connect to database";

#set the root directory of the installation
my $rootdir= "/home/dthacker/efl/dev/";


#open teams.dir for reading
open( CLUB, "<$rootdir/teams.dir"  ) or die "Can't open teams.dir : $!";
while (<CLUB>) {
    print $_;
    my $roster_file=$_;
    my $club = substr($_, 0,3);
    my $strsql = <<EOT;

select name, age, nat, st, tk, ps, sh, agg
from players where players.club="$club"
EOT

    my $sth = $dbh->prepare($strsql);


    $sth->execute() or die "Couldn't execute statement: $DBI::errstr; 
stopped";

    my ($name, $age, $nat, $st, $tk, $ps, $sh, $agg);

format RF =
@<<<<<<<<<<<  @< @<< @< @< @< @< @<
$name, $age, $nat, $st, $tk, $ps, $sh, $agg
.

format RF_TOP =
Name         Age Nat St Tk Ps Sh Ag KAb TAb PAb SAb Gam Sav Ktk Kps Sht Gls 
Ass  DP Inj Sus
-------------------------------------------------------------------------------------------
.

    my $io = new IO::Handle;
    $io->fdopen(fileno(RF),"w");
    $io->format_lines_left(0);

    while ( ($name, $age, $nat, $st, $tk, $ps, $sh, $agg  ) = 
$sth->fetchrow_array() ) {
        write $io;
    }

    #close RF;
    undef $io;

}
$dbh->disconnect();
close CLUB;
------end-------------------

TIA Dave