in reply to sub that sets $_

In a while expression, the automatic setting of $_ happens only when the content of the parentheses after the while is an expression of the form <HANDLE>. You'll need something like

while ( $_ = $reader->chunk ) {

the lowliest monk

Replies are listed 'Best First'.
Re^2: sub that sets $_
by ikegami (Patriarch) on Jun 28, 2005 at 15:19 UTC

    Don't forget to use local $_ before while ( $_ = $reader->chunk ) {, or you might clobber something without even realizing it. For example, the following snippet replaces the content of @array with false values because $_ is (in turn) a reference to each element of @array:

    foreach (@array) { ... my $reader = ...; ... while ($_ = $reader->chunk) { ... } ... }

    The following dies with the run-time error "Modification of a read-only value attempted":

    for ('a', 'b') { ... my $reader = ...; ... while ($_ = $reader->chunk) { ... } ... }

      Yes. Actually, nobull recently made the case for localizing *_ instead of $_, although this added precaution is probably more important for functions and methods in modules than for top-level code.

      the lowliest monk

Re^2: sub that sets $_
by holli (Abbot) on Jun 28, 2005 at 10:45 UTC
    Well, that circumvents the whole convenience. Sad. I believed that would be possible.

    Thanks for your response.
      Well, it may be possible with a tied hash. Would it be worth, though? (except for fun, that is - I've never played with this "kinda things" myself, but I'm tempted!)

      UPDATE:as per revdiablo's remark, s/hash/filehandle/. Just a typo, and only because tied hashes are IMHO the kind of tied objects one most often can hear about...