Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Altering the value of "$/"

by greenhorn (Sexton)
on Jul 03, 2000 at 04:22 UTC ( #20837=perlquestion: print w/replies, xml ) Need Help??

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

It was here that I saw recently how $/ could be temporarily redefined within a block. For example:

{ local $/ = ''; chomp(@array = <FILEHANDLE>); }

I thought I would shorten it slightly:

{ local $/; # ( or: local undef $/; ) chomp(@array = <FILEHANDLE>); }

This made a considerable difference in the contents of "@array". In the first case<KBD> (local $/ = '';) </KBD>entire paragraphs were slurped into each element of the array. But in the second<KBD> (local $/;) </KBD> this didn't happen; each element within the array was a single line from the input file.

How is it that<kbd style=color:red> '' </kbd>is so different from no value (or so different from "undef")?

Replies are listed 'Best First'.
Re: Altering the value of
by plaid (Chaplain) on Jul 03, 2000 at 05:14 UTC
    From the perlman:perlvar page
    Works like awk's RS variable, including treating empty lines as delimiters if set to the null string. ... You may set it to a multi-character string to match a multi-character delimiter, or to undef to read to end of file.
    The values of '' and undef both have pre-defined meanings in the context of the $/ variable.
Re: Altering the value of
by davorg (Chancellor) on Jul 03, 2000 at 11:59 UTC

    The point is that all three behaviours (line-by-line, paragraph-at-a-time and all-in-one) all have their uses in solving different problems, so those nice Perl5 Porters have given us the flexibility to use all three (and a whole bunch more by setting $/ to the appropriate value).

    As plaid notes, this is all explained in perlman:perlvar.


    European Perl Conference - Sept 22/24 2000
      > the flexibility to use all three

      As usual, there's a ton more to this than I have imagined...thanks, genn'lmen.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://20837]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-10-06 20:20 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (28 votes). Check out past polls.