What no one has said so far is the importance of using Carp when throwing
errors related to how the function was called.  The Carp module provides
versions of warn (carp) and die (croak) that give the line and file where
the call to the function occurred rather than the line of the carp or croak:

#!/usr/bin/perl

use strict;
use Carp;
use warnings;

sub takes_two {
    if (@_ != 2) {
        croak "bad number of arguments: [", join(", ", @_), "]";
    }
}

sub not_as_good {
    if (@_ != 2) {
        die "bad number of arguments: [", join(", ", @_), "]";
    }
}

eval { takes_two(1, 2, 3); 1 } or warn $@;
eval { not_as_good(1, 2, 3); 1 } or warn $@;


On Sun, Nov 19, 2017 at 1:55 PM Gil Magno <gilma...@gilmagno.com> wrote:

> On 19/11/17 15:40, Gil Magno wrote:
> > $num_of_params = scalar @_;
>
> Complementing... In order to get the number of params, you could do
>
>     $num_of_params = @_;
>
> without using "scalar @_", because this assignment is already in scalar
> context.
>
> But if you're in list context, you have to use "scalar", as in these
>
>     say scalar @_;
>     print scalar @_;
>
> --
> 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