Hear us Roar
Article:
 |
|
Perl Program Repair Shop and Red Flags
|
| Subject: |
|
return undef |
| Date: |
|
2001-05-18 14:36:42 |
| From: |
|
mjd
|
|
|
|
This is a nice one that I forgot to put in the class.
People like to write return undef
to make a function that returns a false value,
but this is often wrong, because in list context,
return undef returns true.
(It returns a list with a single undefined element.
But only an empty list is false.)
Some programmers are aware of this problem, and write
return wantarray ? () : undef; which works,
but is unnecessarily complicated.
The best answer here is to simply write return;
which returns an undefined scalar value in scalar context,
and an empty list in list context.
|
|
| |