in reply to Return Question in Subroutine

You need to consider what the alternatives are and how the calling code might handle them.

One way to handle it is to die. The calling code doesn't need to do anything special, but it is very likely that you don't want to kill the program at that point. You can set up a handler so that the die is caught and managed in some fashion, but that is way outside of anything we can advise on without knowing a lot more about what your code is doing.

You could return undef which can be detected as a special case and handled by the calling code. If it is not handled, but you have warnings turned on (you do use warnings; don't you?) then chances are the program will issue warnings when the returned value is undef and subsequently used.

Or you can use the current code which again can be detected as a special case by the calling code, but will not generate warnings in most code.

However, without knowing what the calling code is doing and how it might handle errors, it is rather hard to give a good definitive answer.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Return Question in Subroutine
by Anonymous Monk on Apr 28, 2006 at 13:05 UTC
    Hi,
    I do have warning, strict all that turned on, but someone here don't like to see in the error logs the use of unnitialized errors on it, this sub checks the input from a form like this, simply:
    my $test = filter(param( 'name' ));

    I still think that a sub like that shouldn't offer anymore options other than check for bad characters trying to make into the application.

      You can probably clean up your cleanup code a little like this:

      use strict; use warnings; my $str = "<some> text with';*()/? nasty chars. scripted should be ok, + but not script"; print cleanup($str); sub cleanup { $_[0]=~tr|<>;()"'?/*||d; $_[0]=~s/\bscript\b//g; return $_[0]; }

      Prints:

      some text with nasty chars. scripted should be ok, but not

      DWIM is Perl's answer to Gödel