Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Refactoring just to refactor?

by stevieb (Canon)
on Jun 28, 2019 at 23:51 UTC ( [id://11102105]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Refactoring just to refactor?
in thread Refactoring just to refactor?

A subroutine will return to its caller the result of the last evaluated expression. I would recommend to use return() in all cases within all branches, unless you know why/when not to.

Here's an example of output of values. Note that say() is just a glorified print(), but it adds a newline at the end by default. Nothing special here...

use warnings; use strict; use feature 'say'; sub return_print { my $x = 254; print "$x\n"; } sub return_return { my $x = 255; return $x; } say "print: " . return_print(); say "return: " . return_return();

Output:

254 print: 1 return: 255

So, the first output (254) is the output from the print() call. The second output (print: 1) is the output of a successful call to print(), which is 1. The third value is the return value explicitly sent via return().

I would highly recommend you play around and get a grasp on what return values you're getting from and/or expecting from your function/method calls.

All of your tests should be checking against all possible and potential return values so there aren't any unexpected results. If there are any odd things found, add more tests, or fix your subroutines to ensure they only return very specific value(s).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11102105]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-19 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found