Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Format headers and filehandles

by bitman (Beadle)
on Mar 24, 2006 at 12:11 UTC ( #539004=perlquestion: print w/ replies, xml ) Need Help??
bitman has asked for the wisdom of the Perl Monks concerning the following question:

I have a problem with 'format'. If you have a Close for the filehandle before a re-open of the same filehandle into a different filestring, the headings don't work. The file test2 doesn't have the headings.

#!/usr/local/bin/perl use strict; use warnings; open(DM,">c:/test1.txt"); my ($r_fran,$r_par,$r_con,$r_pcode,$r_ser,$r_add1,$r_add2,$r_email,$r_ +acc,$r_name) =("aaa","123","321","bs11","o","14 street","town",'sp@here',"00123","s +cott"); my $edate=localtime(time); format DM_TOP = High Value Report for depot @<<<< on @<<<<<< +<<<<<<<<<<<<<< page @## ,$r_fran, $edate +, $% Parcel Number Account Consignment Name & Address + Postcode Srvc ------------- ------- ----------- -------------- + -------- ---- . format DM = @<<<<<<<<<<<< @<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< + @<<<<<<<< @<<<< $r_par, $r_acc, $r_con, $r_name, + $r_pcode, $r_ser @<<<<<<<<<<<<<<<<<<<< $r_add1 @<<<<<<<<<<<<<<<<<<<< $r_add2 Driver Signature Superviser Signature ----------------------------------- ---------------------------- +------- Driver Signature Superviser Signature ----------------------------------- ---------------------------- +------- . write DM; close DM; open(DM,">c:/test2.txt"); ($r_fran,$r_par,$r_con,$r_pcode,$r_ser,$r_add1,$r_add2,$r_email,$r_acc +,$r_name) =("zzz","123","321","bs11","o","14 street","town",'sp@here',"00123","s +cott"); $edate=localtime(time); write DM; close DM;

2006-03-24 Retitled by Arunbear, as per Monastery guidelines
Original title: 'Format'

Comment on Format headers and filehandles
Download Code
Re: Format headers and filehandles
by socketdave (Curate) on Mar 24, 2006 at 14:21 UTC
    Add this:
    select DM; $- = 0;
    after your second open. I'm not intimately familiar with format and write, but this does the trick for me. Maybe someone more experienced could explain the magic, but this basically forces a new page on the next write.

      Hi,

      Use this, because with the previous the selected output for any print, write,etc will be DM, not the desired, so:

      select( (select (DM), ($- = 0))[0] );

      Will select again the previous output you had before calling select, but assigning the $- variable for that filehandle

      But better is to use lexical variables as filehandles, in the code it seems like you have a register to asign each time, and write to a list of files, why not something like this:

      for my $file (@files) { open my $fh, '<', $file; select(( select($fh), $~ = "DM", $^ = "DM_TOP" )[0] ); # Data adquisition stuff here write $fh; }

      By the way take a look at Perl6::Form, it have a lot of improvements, and not these limitations that Perl 5 formats has.

      And the why this seems to happen, think it is because the DM is a global symbol, and it doesn't get reset the values automatically because when you close the filehandle the DM symbol is not erased...

      Regards,

      fmerges at irc.freenode.net

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-08-22 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls