in reply to Detecting type of output from subs

How do the caller tell if S returned a scalar or an array?

Perl subs can only return a list of scalars. They cannot return arrays.

So you're asking how to determine if the value returned by S originated in an array or not. That's insane. What problem are you actually trying to solve?

Replies are listed 'Best First'.
Re^2: Detecting type of output from subs
by mogul (Novice) on Dec 24, 2010 at 18:53 UTC
    I am building a transport system, I'm going to call user funcions, collecting their output, serialize it with JSON and transmit through a socket.

    I don't know what will be in those functions, but would like to serialize to as correct JSON as possible.

    The posted code shows that perl it self somehow were able to tell the differense, I would like to do the same in my program.
      The posted code shows that perl it self somehow were able to tell the differense...

      It doesn't. Apart from the value of $mode, these two lines do the same thing.

      return 42 if $mode eq 'a'; return (42) if $mode eq 'b';

      The parentheses are superfluous; they do not change the behavior in this case.

      In the final case:

      my @l = (42); return @l if $mode eq 'c';

      The return value depends on the context in which the caller of this function invoked it. When a function called in scalar context (as you've shown here) returns an array, that return value evaluates to the number of elements in the array.

      Oh, and the parentheses in the assignment to the array are also superfluous. This code is equivalent:

      my @l = 42;

      If you'd like to learn more about context, the first chapter of Modern Perl explains it in detail.

      I don't know what will be in those functions, but would like to serialize to as correct JSON as possible.

      Functions return lists of scalars. It is as correct as possible to transmit a list of scalar. You could use a JSON array for that.

      $json->encode( [ f() ] )

      The equivalent of

      my $s = f(); my @a = f();

      is

      my $s = $json_decode(...)->[0]; my @a = @{ $json_decode(...) };