Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Question on the format statement

by perl_seeker (Scribe)
on Mar 31, 2010 at 12:40 UTC ( [id://832029]=perlquestion: print w/replies, xml ) Need Help??

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

Hello

Sorry if this is a silly question but is there a way to save simple reports generated by the format statement in a
file instead of it being displayed on STDOUT?

Thanks for any advice

perl_seeker

Replies are listed 'Best First'.
Re: Question on the format statement
by roboticus (Chancellor) on Mar 31, 2010 at 14:36 UTC
      Thanks, I read the second para.

      Thank you everyone else for your advice.

      Here is the code which I'm trying and it doesnt work. I modified some code i found online.
      open DR, ">ddailyreport.prn" or die "Cant open file"; format DATA = @<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<< @##### @############### + Rs@#####.## ~ $consname, $subdvn, $chequeno, $receiptno, $amount . format TOTAL = -------------------------------------------------------------------- +----------------------- + Rs@#####.## + $total . format TOP = @|||||||||||||||||||||||||||||||||||| Pg @< "Daily Deposit Works Report", $% Consumer name Subdivision Cheque no. Receipt no. + Amount -------------- -------------- ------------- ------------- +- ------- . sub dotize { my($width, $string) = @_; if (length($string) > $width) { return(substr($string, 0, $width - 3) . "..."); } else { return($string); } } $~ = "TOP"; write(DR); open(FILE, "<ddailyreport.txt"); @lines = <FILE>; close(FILE); $total = 0; foreach (@lines) { chop(); ($consname, $subdvn, $chequeno, $receiptno, $amount) = (split(/!/) +); $consname = "" if !defined($consname); $subdvn = "" if !defined($subdvn); $chequeno = 0 if !defined($chequeno); $receiptno = 0 if !defined($receiptno); $amount= 0 if !defined($amount); $~ = "DATA"; write(DR); $total += $amount; } $~ = "TOTAL"; write(DR); close(DR);
      What is wrong in this piece of code?

        perl_seeker:

        How doesn't it work? Specifically: What did you expect to see, what *did* you see, and what were the error messages (if any) that you received?

        Looking over your code, I'd change a few things, anyway:

        open DR, ">ddailyreport.prn" or die "Can't open file"; # Naming a format the same as a file handle automatically associates t +he format # to the data handle. format DR = @<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<< @##### @############### + Rs@#####.## ~ $consname, $subdvn, $chequeno, $receiptno, $amount . # Similarly, adding _TOP to the format name gives you automatic top-of +-form # handling. format DR_TOP = @|||||||||||||||||||||||||||||||||||| Pg @< "Daily Deposit Works Report", $% Consumer name Subdivision Cheque no. Receipt no. + Amount -------------- -------------- ------------- ------------- +- ------- . format TOTAL = -------------------------------------------------------------------- +----------------------- + Rs@#####.## + $total . sub dotize { my($width, $string) = @_; if (length($string) > $width) { return(substr($string, 0, $width - 3) . "..."); } else { return($string); } } # Two things: # (1) This is not needed, per notes above... # (2) The $~ variable associates a format FOR THE DEFAULT OUTPUT # STREAM, so you just changed it for stdout. You would really # use: # my $ofh = select(DR); # make DR the default output # $~ = "DR"; # set format for detail lines # $^ = "DR_TOP"; # set format for top-of-page # select($ofh); # Restore default output stream # #$~ = "TOP"; #write(DR); open(FILE, "<ddailyreport.txt"); @lines = <FILE>; close(FILE); $total = 0; foreach (@lines) { chop(); ($consname, $subdvn, $chequeno, $receiptno, $amount) = (split(/!/) +); $consname = "" if !defined($consname); $subdvn = "" if !defined($subdvn); $chequeno = 0 if !defined($chequeno); $receiptno = 0 if !defined($receiptno); $amount= 0 if !defined($amount); # You need only do the association of the format once. You only n +eed # to do it again if you're going to change it... #$~ = "DATA"; write(DR); $total += $amount; } # Now we change the output format for your summary my $ofh = select(DR); $~ = "TOTAL"; select($ofh); write(DR); close(DR);

        ...roboticus

Re: Question on the format statement
by cdarke (Prior) on Mar 31, 2010 at 13:31 UTC
    Of course, that is what it is designed for. Here is a simple example:
    open (REPORT, '>', 'report.txt') || die "Can't open report.txt: $!"; write REPORT; format REPORT = @<<<<<<< @>>> @<<<<<<<<<<<<<<<<<<<<<<<< $var1, $var2, $var3 .
    I have to admit that I have been unable to get format to work with lexical file handles.
      I have to admit that I have been unable to get format to work with lexical file handles.

      You have to localize the appropriate typeglob, then assign the lexical filehandle to the glob. It's a workaround, but formats don't work with lexical filehandles. They get bound to typeglobs.

Re: Question on the format statement
by moritz (Cardinal) on Mar 31, 2010 at 13:22 UTC
    I've never worked with format before, but the documentation mentions it together with write, which in turns takes an optional file handle as its sole argument... is that what you are looking for? Or is the matter somehow more complicated?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found