in reply to •Re: Seeking an idiom for localizing $/
in thread Seeking an idiom for localizing $/

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
•Re: Re: •Re: Seeking an idiom for localizing $/
by merlyn (Sage) on May 02, 2004 at 17:38 UTC
    sub next_record() { local $/ = "DELIM"; if (<FILE>) return 1; return 0; } while (next_record()) { ... }
    Well my code may "look horrible" to you, but at least it works. Your code, on the other hand, simply tosses away the record being read. No assignments to $_ there, as you would need.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Ack, you're right (of course) ... for some reason I always think of $_ as a global.

      Still I find even the goto version nicer and the obvious change for the sub version turns it into the form you posted, but with the do as a sub (which is, again, more readable IMO).

      --
      James Antill
        for some reason I always think of $_ as a global
        Well, $_ is a global. That's not the reason it's failing though. Perhaps you think that:
        if (<HANDLE>) { ... }
        assigns to $_? If so, that's your mistake. It doesn't. It reads a line, sees if it's true (not just defined, but true), and then tosses the line.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Re: •Re: Seeking an idiom for localizing $/
by Anomynous Monk (Scribe) on May 02, 2004 at 23:46 UTC
    Doesn't look horrible at all; it's a combination of the standard idiom for restricting something to the smallest possible scope (do { local whatever; expression } or do { no strict "refs"; $$symref }) combined with the stuff that a plain while (<FILE>) does implicitly for you (while (defined($_ = <FILE>)).