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

Just a curiosity.... Given:
local $/; # Added in an update while( <IN> ) { while( /$regex/gm ) { ... } }
and:
local $/; # Added in an update my $file = <IN>; while( $file=~/$regex/gm ) { ... }
Is there any significant advantage to using the top method over the bottom one? Or is the only benefit just being able to use $_ implicitly? Just curious if there was more to the story.

UPDATE: Sorry, forget to mention that I did local/undef $/ so I could slurp the file into one big string (have 8000+ substitutions to make and plenty of memory -- file is about 1.3MB, system has 12GB). That would make the two roughly equivalent, right?

Elda Taluta; Sarks Sark; Ark Arks

Replies are listed 'Best First'.
Re: Advantage of while(<>){while()}?
by davido (Cardinal) on May 18, 2011 at 21:21 UTC

    Well, the first method reads a file line by line, while your second method reads one line of the file and stops. Now if you set $/ to undef then your second method would slurp in the entire file. If you're dealing with a small file that will never become large, in an environment where your process will not be running more than a single instance, the second method would be functionally ok. But it will not scale well as file size or number if instances grows.


    Dave

Re: Advantage of while(<>){while()}?
by ikegami (Patriarch) on May 18, 2011 at 21:37 UTC

    I think you meant

    local $/; my $file = <IN>; while( $file=~/$regex/gm ) { ... }

    There are three differences:

    • The first snippet reads the file a line at a time, whereas the above snippet reads the whole file at once. The latter uses more memory, but it might be a bit faster. In practice, this difference isn't usually noticeable.

    • The first snippet will only find matches that occur within the span of one line, whereas the above snippet might find matches that span multiple lines.

    • Using the first snippet, you can group matches by line. You cannot do so with the above snippet.

    Update: Added third difference.

      Yes, you're correct about $/. My fault for leaving too much out (wasn't going to include the open statements, etc).

      Elda Taluta; Sarks Sark; Ark Arks

Re: Advantage of while(<>){while()}?
by ikegami (Patriarch) on May 18, 2011 at 22:14 UTC

    That would make the two roughly equivalent, right?

    Again, it can result in drastically different matches. Consider a poor man's CSV parser, «$regex = qr/[^,]*/;». A file with lines foo,far and boo,bar has four fields (over two lines) using one and three fields using the other.

      Even if I'm slurping the entire file in in both cases (local $/)?

      Elda Taluta; Sarks Sark; Ark Arks

        Oh, I didn't notice that you added local $/; to the first snippet too!

        local $/; while (<>) provides error checking (empty file, read error) that local $/; $_ = <>; doesn't.

Re: Advantage of while(<>){while()}?
by JavaFan (Canon) on May 18, 2011 at 21:33 UTC
    They are not equivalent.