in reply to Avoid using local $/=undef?
In being tasked with modifying an ancient parser I soon noticed that it was using local $/=undef. Which I gathered is generally frowned upon.
It is not. It is frowned upon if it is used in a nonsensical way, like in the code snippet you posted.
Used at toplevel local has no effect, since there is no outer scope: you could just as well modify the global $/ instead. And if readline (or the diamond operator <>) isn't, but sysread used instead, setting $/ doesn't do anything useful.
sysread reads bytes. A subsequent chomp on the result only removes $/ (in your case: nothing, since $/ is undef) if $/ happens to be at the end of the resulting string - which will only be the case if your input file has fixed record length.
Instead of avoiding to use local $/ use it right - inside a BLOCK, so after leaving that block (at runtime) it gets its previous value:
{ local $/; # undef by default if localized # file slurp here } # after the block, original $/ is restored
Try e.g. perl -le "{ local $/ = '@'; print ord $/,': ',$/ } print ord $/,': ',$/"
Result:
64: @ 10:
Read up chomp. That line
push(@records, chomp($record));
doesn't do what you expect. with $/ set to undef your array @records will contain only empty (undef) elements.
See also my/local, space/time (was: Re: The difference between my and local).
|
|---|