git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Git.pm: Don't return 'undef' in vector context.

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 16, 2007, 06:39 UTC
Message-ID
<7vfxz61yox.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<473D3593.9080806@zwell.net>
Dan Zwell <dzwell@gmail.com> writes:
Show 8 quoted lines
> Previously, the Git->repository()->config('non-existent.key')
> evaluated to as true in a vector context. Return an empty list
> instead.
> ---
> I don't know whether this breaks anything, because I don't use most of
> the git perl scripts. I can't imagine that there is a script that
> relies on the fact that config('non-existent.key') actually returns
> (''), in an array context. Is this a reasonable change?

I did not examine the callers but my gut feeling is that it would be simpler and cleaner to always return () without checking the context. In scalar context:

	sub null {
        	...
                return ();
	}
	my $scalar = null();
would assign undef to $scalar anyway.

I generally try to stay away from functions that changes their return values depending on the context, because they tend to make reading the callers to find bugs more difficult. An exception is a function that tries to mimic a built-in operator, because reading the callers to such a function, as long as it is clear which built-in the function is imitating, can apply the same knowledge on how the callee would behave you already have by knowing Perl itself.

The same thing can be said about functions with prototypes to force certain context on the caller's side. Avoid it unless there is a good reason.

Maybe it is just me, but that's my reaction.
Previous: Dan ZwellNext: Dan Zwell
Message 2 of 8 in “Git.pm: Don't return 'undef' in vector context.”
  1. Git.pm: Don't return 'undef' in vector context.Dan Zwell, Nov 16, 2007
  2. Junio C HamanoNov 16, 2007
  3. Dan ZwellNov 16, 2007
  4. Junio C HamanoNov 17, 2007
  5. Sebastian HarlNov 16, 2007
  6. Jakub NarebskiNov 16, 2007
  7. Dan ZwellNov 17, 2007
  8. Jakub NarebskiNov 17, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.