Re: run away ?
by kcott (Archbishop) on Nov 06, 2010 at 18:40 UTC
|
If your going to change $/ use local $/ = "new value";.
Your script won't loop forever, nor will a while loop. Your computer won't blow up either. :-)
Have a look at perlvar (search for $INPUT_RECORD_SEPARATOR) to get the full details.
Here's a test:
#!perl
use 5.12.0;
use warnings;
local $/ = "1313";
while (<DATA>) {
say qq{Found line: $_};
}
__DATA__
qwerty
qwerty
qwerty
qwerty
qwerty
qwerty
Which outputs:
$ strange_in_rec_sep.pl
Found line: qwerty
qwerty
qwerty
qwerty
qwerty
qwerty
Adding 1313 to the data:
__DATA__
qwerty
qwerty
qwerty
1313
qwerty
qwerty
qwerty
The output becomes:
$ strange_in_rec_sep.pl
Found line: qwerty
qwerty
qwerty
1313
Found line:
qwerty
qwerty
qwerty
Thanks for your amusing post. I had a good chuckle. :-)
| [reply] [d/l] [select] |
|
|
I like to make people laugh even if it
wasn't intended.
I have another strange piece of code to present sometime soon
You should like this one lol
| [reply] |
|
|
| [reply] |
|
|
If your going to change $/ use local $/ = "new value";.
Why?
I ask because this is one of those cases I struggle with. Sometimes I use local $INPUT_RECORD_SEPARATOR = "some string"; in Perl scripts (not modules), sometimes I don't. I feel like I should localize the variable, but doing so also seems inert. Why should I care about the scope that doesn't exist outside the universe of my standalone Perl script? Isn't local just needless noise here?
I should add that I usually set this built-in variable outside any blocks within my scripts unless I have a specific reason to do otherwise (and I usually don't).
| [reply] [d/l] [select] |
|
|
Because it's a useful default behaviour that doesn't cost a lot and doesn't add much noise when it's not required, but can save a huge amount of pain when it is required. Think of it as a good habit like using strictures, three parameter open and all those other good habits that just naturally flow off the ends of your fingers or are part of your standard boilerplate.
True laziness is hard work
| [reply] |
|
|
|
|
| [reply] |
Re: run away ?
by Anonymous Monk on Nov 06, 2010 at 18:34 UTC
|
If you are reading a filehandle with readline, and $/ is defined and contains a string, then the effect is like splitting the incoming data into chunks ending with that string. If that string is not encountered, then there is returned only one chunk. | [reply] [d/l] |
|
|
| [reply] |
Re: run away ?
by duelafn (Parson) on Nov 06, 2010 at 18:38 UTC
|
Why are you afraid of an infinite loop? You can always Control-c or close the window or whatever - inifinite loops are harmless. A fork bomb on the other hand is less fun (though you should try one once for the experience).
| [reply] |
|
|
When I was at Dalhousie ( 89-92 ), every year you can tell when the 3rd-year students have reached the fork-join assignment, as runaway processes hogged the undergrad shared machine. Nowadays, of course, it's people on their home machines, disrupting their torrent download and music server. But rebooting is quick.
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
| [reply] |
|
|
I guess a loop could be a good thing .
fork bombs could be fun also. I will try that next time I want to reboot my computer .
| [reply] |
Re: run away ?
by PeterPeiGuo (Hermit) on Nov 06, 2010 at 23:39 UTC
|
Well, it will go on forever, if your file is endless - in which case, it doesn't matter what the value of $/ is.
| [reply] |
|
|
Or at least until Sunday, December 23, 2012, at which point all record separators become one... :)
| [reply] |