Skip to content

Commit

Permalink
article
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Dec 3, 2023
1 parent df29122 commit 5f8deaf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/implicit_return.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use strict;
use warnings;
use feature 'say';


sub function {
my ($x, $y) = @_;

if ($x < $y) {
my $total = $x + $y;
}
}

{
my $res = function(2, 3);
say defined $res ? "defined" : "undef";
say $res eq "" ? "empty string" : "NOT the empty string";
say $res;
say "-------------------";
}
{
my $res = function(3, 2);
say defined $res;
say $res eq "" ? "empty string" : "NOT the empty string";
say $res;
say "-------------------";
}
56 changes: 56 additions & 0 deletions sites/en/pages/curious-case-of-implicit-return.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
=title The curious case of implicit return
=timestamp 2023-12-03T07:30:01
=indexes sub, return
=status show
=author szabgab
=archive 1
=description If a function in Perl does not explicitly call return then it will implicitly return the result of the last statement evaluated.
=comments_disqus_enable 0
=show_related 1

=abstract start

My son works at a company where they analyze source code and report on potential <a href="https://privya.ai/">data privacy violations</a>.
For this they need to parse source code in various programming languages. He mentioned that one day they might need to support Perl too.
I thought about the difficulties in parsing Perl and one case came to my mind was the strange implicit return from a function that Perl has.

Then I also thought that <a href="https://www.rust-lang.org/">Rust</a> also has some strange ideas. BTW Do you know that I have a new web site called
<a href="https://rust.code-maven.com/">Rust Maven</a> where I write about Rust? Now you know.

Anyway, back to Perl:

=abstract end

What do you think this function will return and what will this code print?

<include file="examples/implicit_return.pl">

I know it is a very contrived example, but in the few minutes I spent on it I could not come up with a more realistic one. Send me a better example!


<h2>Spoiler Alert</h2>

If a function in Perl does not explicitly call <b>return</b> then it will implicitly return the result of the last statement evaluated.

In the above code, if the <b>if</b> condition inside the "function" evaluates to <b>true</b> then the code inside the block will be executed
and thus the assignment to <b>$total</b> will be the last executed statement and so the value of <b>$total</b> will be returned.

On the other hand if the <b>if</b> condition inside the "function" evaluates to <b>false</b> then this will be the last statement
in the function and thus this <b>false</b> value will be returned that will happen to be the <b>empty string</b>.

In the example I added a check to show whether the returned values is <a href="/defined">defined</a> or if it is <a href="/undef">undef</a>.
A separate check is included to show if it <b>equals</b> to the empty string or not using the <a href="/the-ternary-operator-in-perl">ternary operator</a>.


<code>
defined
NOT the empty string
5
-------------------
1
empty string

-------------------
</code>


0 comments on commit 5f8deaf

Please sign in to comment.