Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

No, you can use truncate and seek and if you put them together in a subroutine you only have to make one call each time to empty your file ready for the next print statement. Note that /usr/bin/hexdump run on an empty file produces no output.

$ perl -Mstrict -Mwarnings -e ' > sub truncZero > { > my $fh = shift; > truncate $fh, 0 or die $!; > seek $fh, 0, 0 or die $!; > } > > open my $fh, q{>}, q{rubbish} or die $!; > > print $fh qq{Mary had a little lamb\n}; > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > truncZero( $fh ); > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > print $fh qq{The boy stood on the burning deck\n}; > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > truncZero( $fh ); > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > print $fh qq{I met a traveller from an antique land\n}; > print qx{/usr/bin/hexdump -vC rubbish}, qq{-----\n}; > > close $fh or die $!;' 00000000 4d 61 72 79 20 68 61 64 20 61 20 6c 69 74 74 6c |Mary had +a littl| 00000010 65 20 6c 61 6d 62 0a |e lamb.| 00000017 ----- ----- 00000000 54 68 65 20 62 6f 79 20 73 74 6f 6f 64 20 6f 6e |The boy s +tood on| 00000010 20 74 68 65 20 62 75 72 6e 69 6e 67 20 64 65 63 | the burn +ing dec| 00000020 6b 0a |k.| 00000022 ----- ----- 00000000 49 20 6d 65 74 20 61 20 74 72 61 76 65 6c 6c 65 |I met a t +ravelle| 00000010 72 20 66 72 6f 6d 20 61 6e 20 61 6e 74 69 71 75 |r from an + antiqu| 00000020 65 20 6c 61 6e 64 0a |e land.| 00000027 ----- $ cat rubbish I met a traveller from an antique land $

I hope this is helpful.

Cheers,

JohnGG


In reply to Re^5: Empty File Contents before writing by johngg
in thread Empty File Contnts before writing by gautamparimoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2015-07-04 04:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (57 votes), past polls