Nathan Hilterbrand <noset...@cotse.net> writes:

> On 01/25/2016 05:13 PM, lee wrote:
>> Paul Johnson <p...@pjcj.net> writes:
>>
>>> On Mon, Jan 25, 2016 at 12:24:04AM +0100, lee wrote:
>>>> Paul Johnson <p...@pjcj.net> writes:
>>> [...]
>>>> Consider with these examples that an expression like (1, 3) might
>>>> unexpectedly evaluate to 3, and you start to think that you don't like
>>>> things like
>>>>
>>>>
>>>> sub s {
>>>>      my $a = 1;
>>>>      my $b = 3;
>>>>
>>>>     return ($a, $b);
>>>> }
>>>>
>>>>
>>>> anymore because you could, by mistake (or intentionally), write
>>>>
>>>>
>>>> my $x = s;
>>>>
>>>>
>>>> .  So let me re-phrase my original question to: How do you /safely/
>>>> return arrays?
>>>>
>>>> That means I need an error message for '$x = s;' because I'd write that
>>>> only by mistake.
>>> You could add to your subroutine:
>>>
>>>    die "Only call in list context" unless wantarray;
>> Oh, that's really cool :)
>>
> IMHO, this is pretty cool too:
>
> return wantarray() ? ($a, $b) : [$a, $b];
>
> In a list context, you get back a list..  otherwise you get back a
> reference to a list.  Might not be what you want, though.
>
> Works with arrays, too..
>
> my @anarray = (....)
> return wantarray() ? (@anarray) : [@anarray];

That's chaos.  How can I specify the return type of a function (when
declaring it) just as I do in C?


On a side note:

The same is with 'typedef'.  The post about data structures here made me
look that up because I thought right away 'is there nothing like
typedef?', and I found that there is no typedef in perl but chaos and
ppl saying that the idea is to instead use the data the way you want to
use it, while showing ways in which I certainly don't want to use data,
as examples.

That leaves the option to integrate some C --- and I don't see (yet) how
that could be useful (worthwhile) for what I'm using perl for other than
increasing performance for which it seems difficult to use.  And
performance is great.

Is there a way to do some profiling on perl programs to find out what
they spend their time with?

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to