Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: Refactoring just to refactor?

by Lady_Aleena (Curate)
on Jun 28, 2019 at 22:40 UTC ( #11102104=note: print w/replies, xml ) Need Help??

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

Thank you for catching that I did not use $dir in the sub where I meant to use it.

I am not clear on what you mean by "hard-boiled style". I always either return or print at the end of a sub. (I have not gotten to use the feature say that much.) I do not know how else to end a subroutine.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^3: Refactoring just to refactor?
by stevieb (Canon) on Jun 28, 2019 at 23:51 UTC

    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();


    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?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2021-09-20 12:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found