Subroutine return value
Daryl Lee <[email protected]> Wed, 14 May 2025 17:15:02 -0600
| Newsgroups | gmane.comp.lang.perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
--Apple-Mail=_FC66B4FA-4B56-4959-89B4-497C7214DFEA
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=utf-8
In =E2=80=9CLearning Perl=E2=80=9D, Pg. 116, is found this sentence: =
Whatever calculation is last performed in a subroutine is automatically =
also the return value.
I have a subroutine that (almost) ends with this loop:
foreach my $line (@lines) {
$sum +=3D evaluate($line);
}
What I want to return is the final value of $sum as calculated on the =
last pass of that loop, so I simply closed the subroutine block right =
there with }, making it look like this:
foreach my $line (@lines) {
$sum +=3D evaluate($line);
}
}
That was pointless, so I =E2=80=9Cworked around=E2=80=9D the issue by =
adding $sum at the end:
foreach my $line (@lines) {
$sum +=3D evaluate($line);
}
$sum;
}
I think I can remember this in the future, but I=E2=80=99d like to =
understand what=E2=80=99s actually happening. And I suspect I=E2=80=99m =
offending some veterans by not making Perl-ish use of #_. Sorry about =
that.
--Apple-Mail=_FC66B4FA-4B56-4959-89B4-497C7214DFEA
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
charset=utf-8
<html><head><meta http-equiv=3D"content-type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"overflow-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;"><div>In =
=E2=80=9CLearning Perl=E2=80=9D, Pg. 116, is found this sentence: =
Whatever calculation is last performed in a subroutine is automatically =
also the return value.</div><div><br></div><div>I have a subroutine that =
(almost) ends with this loop:</div><div><br></div><div><div> =
<font face=3D"Courier New">foreach my $line (@lines) =
{</font></div><div><font face=3D"Courier New"> =
$sum +=3D evaluate($line);</font></div><div><font face=3D"Courier =
New"> </font>}</div></div><div><br></div><div>What I want to =
return is the final value of $sum as calculated on the last pass of that =
loop, so I simply closed the subroutine block right there with }, making =
it look like this:</div><div><br></div><div><div><div> =
<font face=3D"Courier New">foreach my $line (@lines) =
{</font></div><div><font face=3D"Courier New"> =
$sum +=3D evaluate($line);</font></div><div><font face=3D"Courier =
New"> =
</font>}</div></div></div><div>}</div><div><br></div><div>That was =
pointless, so I =E2=80=9Cworked around=E2=80=9D the issue by adding $sum =
at the end:</div><div><br></div><div><div><div> <font =
face=3D"Courier New">foreach my $line (@lines) {</font></div><div><font =
face=3D"Courier New"> $sum +=3D =
evaluate($line);</font></div><div><font face=3D"Courier =
New"> </font>}</div></div><div> <font =
face=3D"Courier =
New">$sum;</font></div><div>}</div></div><div><br></div><div>I think I =
can remember this in the future, but I=E2=80=99d like to understand =
what=E2=80=99s actually happening. And I suspect I=E2=80=99m =
offending some veterans by not making Perl-ish use of #_. Sorry =
about that.</div><div><br></div></body></html>=
--Apple-Mail=_FC66B4FA-4B56-4959-89B4-497C7214DFEA--