Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: What to test in a new module

by swl (Parson)
on Jul 07, 2023 at 02:50 UTC ( #11153305=note: print w/replies, xml ) Need Help??


in reply to Re: What to test in a new module
in thread What to test in a new module

It's a case of TIMTOTWDI, and I have not looked at the original code, but two comments:

No need for this: $self->{'error'} = '';. It'll be autovivified if needed.

It will autovivify as undef if it is not assigned to before it is accessed. That could cause issues later on with other code.

There's no need for a return; at the end of a sub if nothing's being returned (unless you intentionally are returning undef.

It's safer to explicitly return as otherwise the value of the last expression is returned. FWIW, this is covered in PBP on p197 under Implicit Returns, with a follow-on under Returning Failure on p199 (although that also encourages the Contextual::Return module which I don't use).

Replies are listed 'Best First'.
Re^3: What to test in a new module
by jdporter (Chancellor) on Jul 07, 2023 at 13:50 UTC
    It's safer to explicitly return as otherwise the value of the last expression is returned.

    He did say "unless you intentionally are returning undef". I agree with stevieb on that point; however, in my code if I am intentionally returning undef I make that explicit with return(); I never use return; in my code unless I am returning early from a sub which is not expected to return a value. (If the caller insists on doing something with the returned value from such a sub, TNMP.)

      Thanks for the clarification. Two other points, which you are probably well aware of but some readers might not be:

      return; and return(); are the same, although the brackets do make it more visually distinct and perhaps that's your point.

      return; in list context returns the empty list. This means it is effectively the same as return wantarray ? () : undef; unless it is called in void context. Your use of it when not expecting a return value implies void context, though, so perhaps this point is moot.

        the brackets do make it more visually distinct and perhaps that's your point.

        That is precisely my point.

        It isn't obvious that return; returns undef, so I don't depend on that. That is, I don't use that construction when that's what I want to do.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2023-11-30 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?