perlquestion
srchulo
<p>Hi Monks,</p>
<p>I have a question about some code I saw in <a href="https://metacpan.org/pod/Mojo::File">Mojo::File</a>. In the <code>slurp</code> method, you see this:</p>
<p>
<code>
sub slurp {
my $self = shift;
CORE::open my $file, '<', $$self or croak qq{Can't open file "$$self": $!};
my $ret = my $content = '';
<b>while ($ret = $file->sysread(my $buffer, 131072, 0)) { $content .= $buffer }</b>
croak qq{Can't read from file "$$self": $!} unless defined $ret;
return $content;
}
</code>
</p>
<p>
Inside the <code>while</code>, you see <code>my $buffer</code> used as the first argument to <a href="https://perldoc.perl.org/functions/sysread.html"><code>sysread</code></a>, and then used in the body of the <code>while</code> loop. I've never seen this before, where you declare a variable as an argument and then access it later. Can anyone explain how this works? Is the body of the <code>while</code> loop able to access <code>$buffer</code> because the <code>while</code> loop's scope includes anything in the parentheses or the body of the loop?
</p>