Wednesday, April 13, 2011

Understanding Perl Input Record Separators ($/)

Separator ValueModeDescription
$/ = undef;Slurp modeReads the entire file into a single string all at once.
$/ = ""; (empty string)Paragraph modeReads text block-by-block, delimited by two or more newlines.
$/ = "\n"; (default)Line-by-line modeReads one standard line at a time.
$/ = " "; (space)Space-delimited modeReads characters up to the next space (sometimes referred to as "almost word-by-word").
$/ = "\0";Null-terminated modeReads up to a null character. Extremely useful for processing output from system commands like find -print0.
$/ = \32768; (scalar ref)Fixed-length modePassing a reference to an integer (e.g., \1024) reads exactly that many bytes at a time. Great for fixed-width data or binary files.

No comments: