Mailing List Archive

Re: perl 5 problem (try again appending script)
you should refer to a perl book on using format (Larry Wall and
Randal Schwartz's book is a good one to start). in the book i have
it is page 106. but basically, $~ will set the format for the
currently selected filehandle, which defaults to STDOUT .

in the below script you are resetting $~ but not doing a select(REPORT)
beforehand thus affecting STDOUT's format. and this is why perl
complains that format REPORT is missing, because the default format
is the same name as the filename to write.

as the book suggests, do something like:

select((select(REPORT), $~ = "rprt")[0]);
write (REPORT);

or,

select((select(REPORT), $~ = "rprt2")[0]);
write (REPORT);

as the case may be.

> From etter@tik.ee.ethz.ch Thu Sep 14 09:48 MDT 1995
>
> .. this time the script is attached!
>
> Felix
>
>
> > >
> > >
> > > Hi perl porters,
> > >
> > > I'm having some trouble to run a perl script which worked fine with perl 4.
> > > Myself and some friends can't figure out why. Always get some error message
> > > like "Undefined format "main::REPORT" called at ..". I attached the script
> > > below.
> > >
> > > Thanks a lot for any hints and have a nice day.
> > >
> > > Bye
> > >
> > > Felix
> > >
> > >
> > > P.S.: We are running Perl 5.0001 on SUN Sparcs.
> > >
> >
> >
> > --
> > Chip Capelik
> > chip@gdwest.gd.com
> >
>
> #!/tik1opt1/bin/perl
> # --------------------------------------------------------------------------
> # file : print_elog
> # short description: print elog data of a specific time period
> # project : tik_infra_I
> # author : etter@tik.ee.ethz.ch
> # created on : 14.09.95
> # history :
> # comments (or man page)
> # synopsis : see man page
> # descriptions :
> # options :
> # examples :
> # environment :
> # files :
> # see also :
> # crontab :
> # installation :
> #
> #
> # --------------------------------------------------------------------------
> #
> require "bigfloat.pl";
> #
> $account = (getpwuid($<))[0]; # get userid from /etc/passwd
> $real_name = (getpwuid($<))[6];
> $home = (getpwuid($<))[7]; # get homedir from /etc/passwd
> if (!-d "$home/.Wingz") {
> mkdir("$home/.Wingz", 0755);
> }
> $printdet_flag = $ENV{'PRINT_ELOG_ALL'};
>
> &Get_user_configuration();
>
> #
> # /* -------------------------- subfunctions ------------------------------ */
> #
> # /* --- Read configuration file --- */
> sub Get_user_configuration
> {
> open(CFGFILE, "$home/.elog.cfg") || die "\nCan't find $home/.elog.cfg, start elog first!\n\n";
> while (<CFGFILE>) {
> if (!($_ =~ /\#/)) {
> @dummy = split(/\:/,$_);
> if ($dummy[0] eq "logdir") {
> chop ($logdir = $dummy[1]);
> $logfile = $logdir."/".$account.".logs";
> }
> elsif ($dummy[0] eq "projects.dat") {
> chop ($projdir = $dummy[1]);
> $projects = "$projdir/projects.dat";
> }
> }
> }
> close(CFGFILE);
> return $logfile, $projects;
> }
>
> # /* --- Complete date --- */
> sub Complete_date
> {
> local ($in_parameter) = @_; # local -> lokal Sub.
> $in_parameter =~ tr/0-9\.//cd; # entfernt Buchstaben
> local (@in_parameter) = split (/\./, $in_parameter);
>
> for ($i = 0; $i < 3; $i++)
> {
> $param_length = length (@in_parameter[$i]);
>
> if ($param_length > 2)
> {
> @in_parameter[$i] = substr ((@in_parameter[$i]), 0, 2);
> }
> elsif ($param_length == 1)
> {
> @in_parameter[$i] = "0".@in_parameter[$i];
> }
> elsif ($param_length == 0)
> {
> @in_parameter[$i] = @today[$i];
> }
> }
> return @in_parameter[2].@in_parameter[1].@in_parameter[0];
> }
> # /* --- Create report --- */
> sub Create_report
> {
> $worktotal = 0.00;
>
> format rprt =
> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>>
> @projarr[1],$prtotal
> .
>
>
> # titles for the different reportfiles
>
> open(PROJECTS, $projects) || die "\nCan't open $projects\n\n";
> open(EXTRACT, "/tmp/$extract_file") || die "\nCan't open /tmp/$extract_file\n\n";
> open(REPORT, "> /tmp/$report_file") || die "\nCan't open /tmp/$report_file\n";
> open(TMPFILE, "> /tmp/$detail_file") || die "\nCan't open /tmp/$detail_file\n";
> open(WINGZIMP, "> $wingz_import") || die "\nCan't open $wingz_import\n";
>
> $tmpvar = sprintf("Detail project time for %s, %s - %s", $real_name,$date_1,$date_2);
> print (TMPFILE "\n$tmpvar\n");
> $file = TMPFILE; $counter = length($tmpvar); $char = "="; &Under_line($counter,$char,$file);
> $tmpvar = sprintf("Time spent report, %s, %s - %s", $real_name,$date_1,$date_2);
> print (REPORT "\n$tmpvar\n");
> $file = REPORT; $counter = length($tmpvar); $char = "="; &Under_line($counter,$char,$file);
> print (REPORT "\n");
> write(REPORT);
> print (WINGZIMP "TIK time spent reports");
> printf(WINGZIMP "\n%s %s - %s",$real_name, $date_1, $date_2);
>
> # Begin of calculation
>
> while (<PROJECTS>) {
> @projarr = split(/\:/, $_);
> # # Check if projtotal > 0
> # $projtotal = 0.0;
> # seek(EXTRACT,0,0);
> # while (<EXTRACT>) {
> # @extarr = split(/\:/, $_);
> # if (@projarr[0] == @extarr[3] ) {
> # $projtotal = &fadd($projtotal, @extarr[4]);
> # } # endif
> # }
> #
> # if ($projtotal > 0) {
> $tmpvar = sprintf ("Project: %s", @projarr[1]);
> print (TMPFILE "\n$tmpvar");
> $file = TMPFILE; $counter = length($tmpvar)-1; $char = "-"; &Under_line($counter,$char,$file);
> printf (TMPFILE "date time t-sp work");
> printf (TMPFILE "\n---- ---- ---- ----\n");
> $projtotal = 0.00;
> seek(EXTRACT,0,0);
> while (<EXTRACT>) {
> @extarr = split(/\:/, $_);
> if (@projarr[0] == @extarr[3] ) {
>
> printf (TMPFILE "%s %s %.2f %s",@extarr[0],@extarr[1],@extarr[4],@extarr[5]);
> $projtotal = &fadd($projtotal, @extarr[4]);
> } # endif
> } # end while (<EXTRACT>)
> print (TMPFILE "\n");
> $worktotal = &fadd($worktotal, $projtotal);
> $prtotal = sprintf("%.2f", $projtotal);
> # } # endif projtotal > 0
> if ($projtotal > 0 ) {
> $~ = 'rprt';
> write(REPORT);
> }
>
> if ($projtotal > 0 ) {
> chop($tmpproj = @projarr[1]);
> $tmprow = sprintf ("\n%s\t%.2f", $tmpproj, $projtotal);
> print (WINGZIMP $tmprow);
> }
>
> } # end while (<PROJECTS>)
>
> $ftotal = sprintf ("%.2f", $worktotal);
> format rprt2 =
>
> Total work time: @>>>>>>
> $ftotal
> .
>
> $~ = 'rprt2';
> write(REPORT);
>
> close(TMPFILE);
> close(PROJECTS);
> close(EXTRACT);
> close(REPORT);
> close(WINGZIMP);
> }
>
> # /* --- Under line --- */
> sub Under_line
> {
> while ($counter > 0) {
> print ($file $char);
> $counter--;
> }
> print ($file "\n");
> }
> #
> # /* ----------------------------- main () ------------------------------- */
> #
> # Input time period
> #
> open (DATE, "date +%d.%m.%y |");
> chop ($today = <DATE>);
> @today = split (/\./,$today);
>
> # print "\nProject time spent report\n";
> # print "-------------------------\n\n";
> if (@ARGV[0] eq "") {
> print ("First date \(for example 15.2\): ");
> chop ($date_1 = <STDIN>); # chop to remove <CR>
> } elsif ( @ARGV[0] eq "-d") {
> if ( @ARGV[1] eq "" ) {
> die | "\noption -d requires a date as second argument for daily report!\n\n";
> } else {
> $date_1 = @ARGV[1];
> $date_2 = @ARGV[2];
> }
> } else {
> $date_1 = @ARGV[0];
> }
>
> if (@ARGV[1] eq "") {
> print "Last date [current date]: "; # Filehandler STDIN
> chop ($date_2 = <STDIN>);
> } else {
> $date_2 = @ARGV[1];
> }
>
> #
> $date_1_numeric = &Complete_date ($date_1);
> $date_2_numeric = &Complete_date ($date_2);
> #
> $extract_file =
> substr ($date_1_numeric,4,2).".".
> substr ($date_1_numeric,2,2).".".
> substr ($date_1_numeric,0,2)."-".
> substr ($date_2_numeric,4,2).".".
> substr ($date_2_numeric,2,2).".".
> substr ($date_2_numeric,0,2); # Stringconcatenation with .
> #
> $extract_file = $account.".".extr.".".$extract_file;
> system ("rm -f /tmp/$extract_file"); # Unix system-
> #
> open (EXTRACT, ">>/tmp/$extract_file") || "\nCan't open /tmp/$extract_file\n\n";
> open (LOGFILE, $logfile) || die "\nCan't open $logfile\n";
> #
> # Convert date to integer
> #
> $found = "false";
> #
> while (<LOGFILE>)
> {
> $dummy = substr ($_,0,8);
> @dummy = split (/\./, $dummy);
> $actual_date_numeric = @dummy[2].@dummy[1].@dummy[0];
> #
> #
> if ($actual_date_numeric >= $date_1_numeric && $actual_date_numeric <=
> $date_2_numeric)
> {
> print (EXTRACT $_);
> $found = "true";
> }
> }
> #
> close (LOGFILE);
> close (EXTRACT);
> #
> if ($found eq "false") {
> print "No corresponding entries in your logfile\n";
> print "--> Quit!\n\n";
> system ("rm /tmp/$extract_file");
> die;
> }
> if (@ARGV[0] eq "" && @ARGV[1] eq "") {
> print "Title? [TIK project time report]:";
> chop ($title = <STDIN>);
> }
> if ($title eq "") {
> $title = "TIK project time report";
> }
> $title = $title." ".(getpwuid($<))[6]; # $< -> real user id
> #
> if (@ARGV[0] eq "" && @ARGV[1] eq "" ) {
> print "Print report? [y]:";
> chop ($answer = <STDIN>);
> }
> #
> if ($answer eq "y" || $answer eq "") {
> $detail_file = "$account.$extract_file.det";
> $report_file = "$account.$extract_file";
> $wingz_import = "$home/.Wingz/print-elog.wingz";
> &Create_report($extract_file,$detail_file,$report_file,$wingz_import,$date_1,$date_2);
> if (@ARGV[0] eq "-d") {
> # print ("\nok, this was option -d");
> open (CATREP, "/tmp/$report_file") || die "\nCan't open /tmp/$report_file\n\n";
> while(<CATREP>) {
> print $_;
> }
> close(CATREP);
> # open (CATDET, "/tmp/$detail_file") || die "\nCan't open /tmp/$detail_file\n\n";
> # while (<CATDET>) {
> # print $_;
> # }
> # close(CATDET);
> } else {
> if (@ARGV[0] eq "" && @ARGV[1] eq "" ) {
> print "Printer? <enter> = default-printer: ";
> chop ($default_printer = <STDIN>);
> print "\n";
> }
> if ($default_printer eq "") {
> $cmd1 = "nenscript -fCourier8 -Grb \'".$title."\' /tmp/".$report_file;
> if ($printdet_flag eq "" || $printdet_flag eq "YES") {
> $cmd2 = "nenscript -fCourier8 -Grb \'".$title."\' /tmp/".$detail_file;
> }
> } else {
> $cmd1 = "nenscript -P$default_printer -fCourier8 -Grb \'".$title."\' /tmp/".$report_file;
> if ($printdet_flag eq "" || $printdet_flag eq "YES") {
> $cmd2 = "nenscript -P$default_printer -fCourier8 -Grb \'".$title."\' /tmp/".$detail_file;
> }
> }
> system ("$cmd1");
> system ("$cmd2");
> print "\n";
> } # endif option -d
> } else {
> print "\n";
> }
> if (@ARGV[0] eq "" && @ARGV[1] eq "" ) {
> print ("Remove reportfiles [y]: ");
> chop ($delete = <STDIN>);
> print "\n";
> }
> #
> if ($delete eq "y" || $delete eq '') {
> system ("rm /tmp/$extract_file");
> system ("rm /tmp/$report_file");
> system ("rm /tmp/$detail_file");
> } else {
> if (@ARGV[0] ne "-d") {
> printf ("Extractfile:/tmp/%s\n", $extract_file);
> printf ("Reportfile: /tmp/%s\n", $report_file);
> printf ("Detail: /tmp/%s\n\n", $detail_file);
> }
> }
>
>
>
>
>


--
Chip Capelik
chip@gdwest.gd.com