Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Replace commas with spaces between quotes, parsing CSV

by BigRedEO (Acolyte)
on Apr 15, 2016 at 20:02 UTC ( [id://1160584]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Replace commas with spaces between quotes, parsing CSV
in thread Replace commas with spaces between quotes, parsing CSV

Curious - what is the line with the pipe here doing - $|=1;
  • Comment on Re^3: Replace commas with spaces between quotes, parsing CSV

Replies are listed 'Best First'.
Re^4: Replace commas with spaces between quotes, parsing CSV
by Marshall (Canon) on Apr 15, 2016 at 20:21 UTC
    The line $|=1 turns off buffering on STDOUT. I always use that when debugging code. STDERR is unbuffered by default and STDOUT is buffered by default.

    What that means is that when writing to STDOUT, you may have to write say 4Kbytes of data before it actually gets output to the screen or the hard disk. This is what you want for the program to run fast. However, that means that the unbuffered STDERR messages come out immediately and "out of order" with the STDOUT printout. In other words, it can happen that the error message appears first and then much later the stuff associated with the error appears.

    There is a big performance penalty for using $|=1, especially to the HD. The disk system works most efficiently with "blocks". A typical set up will be 512 byte fundamental "writeable" units on the HD, these are grouped together 8 at a time, to make a 4K block. And that is what is used to write to the disk and what is tracked by the directory system.

    $|=1 will force a write for every "print" statement instead of have this buffered up to some larger number that the I/O system would rather deal with. You will find that even a one bye text file takes a minimum size unit on the disk.

    Update with a demo:

    #!/usr/bin/perl use warnings; use strict; my $x = undef; print "asdwf\n"; print "n,zxncv\n"; print "$x\n"; =Prints Use of uninitialized value $x in concatenation (.) or string at C:\Pro +jects_Perl\demounbuffer.pl line 8. asdwf n,zxncv =cut $|=1; $x = undef; print "asdwf\n"; print "n,zxncv\n"; print "$x\n"; =Prints asdwf n,zxncv Use of uninitialized value $x in concatenation (.) or string at C:\Pro +jects_Perl\demounbuffer.pl line 20. =cut
    In the first code, the error message comes out first although this is the third print statement!!! In the second code (the same code, but with $|=1;), the error comes out 3rd in the print order! That more realistically presents the time line order of what actually happened.

    I also show another "Perl trick", I used the PerlDoc feature to insert some explanation into the code. This is normally used to insert documentation at the beginning of the code, but I "cheated".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1160584]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found