srchulo has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I have a question about some code I saw in Mojo::File. In the slurp method, you see this:
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; }
Inside the while, you see my $buffer used as the first argument to sysread, and then used in the body of the while 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 while loop able to access $buffer because the while loop's scope includes anything in the parentheses or the body of the loop?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Declare my variable in sysread - Mojo::File::slurp
by choroba (Cardinal) on Dec 21, 2018 at 16:59 UTC | |
by ikegami (Patriarch) on Dec 22, 2018 at 00:43 UTC | |
by choroba (Cardinal) on Dec 22, 2018 at 00:46 UTC | |
by ikegami (Patriarch) on Dec 22, 2018 at 00:56 UTC | |
by ikegami (Patriarch) on Dec 22, 2018 at 00:52 UTC | |
Re: Declare my variable in sysread - Mojo::File::slurp
by jwkrahn (Abbot) on Dec 21, 2018 at 19:35 UTC | |
Re: Declare my variable in sysread - Mojo::File::slurp
by ikegami (Patriarch) on Dec 22, 2018 at 00:39 UTC | |
Re: Declare my variable in sysread - Mojo::File::slurp
by Anonymous Monk on Dec 21, 2018 at 23:26 UTC |
Back to
Seekers of Perl Wisdom