Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Length of Array Passed as Reference

by afoken (Canon)
on Dec 10, 2020 at 08:21 UTC ( #11124930=note: print w/replies, xml ) Need Help??


in reply to Re: Length of Array Passed as Reference
in thread Length of Array Passed as Reference

Engaging beancounter mode ... - ready.

sub test_function { my $ref = shift; # this function takes one argument, a ref to a +n ARRAY # Note: you could do extra manual checking of function arguments. # Not necessarily recommended: shown for educational purposes. defined($ref) or die "oops: you did not pass any arguments"; @_ and die "oops: this function takes exactly one argument";

There is an edge case where this code produces a wrong error message:

use strict; use warnings; use feature 'say'; sub test_function { my $ref = shift; # this function takes one argument, a ref to a +n ARRAY # Note: you could do extra manual checking of function arguments. # Not necessarily recommended: shown for educational purposes. defined($ref) or die "oops: you did not pass any arguments"; @_ and die "oops: this function takes exactly one argument"; } say "Calling test_function with no arguments:"; eval { test_function(); }; say $@; say "Calling test_function with a single undefined argument:"; eval { test_function(undef); }; say $@;
X:\>perl foo.pl Calling test_function with no arguments: oops: you did not pass any arguments at foo.pl line 10. Calling test_function with a single undefined argument: oops: you did not pass any arguments at foo.pl line 10. X:\>

Generally, the error messages are slightly confusing: They report the line where the error was detected, not where the error was made. Carp can help:

use strict; use warnings; use feature 'say'; use Carp qw( croak ); sub test_function { @_==1 or croak "oops: this function takes exactly one argument"; my $ref = shift; # this function takes one argument, a ref to a +n ARRAY # Note: you could do extra manual checking of function arguments. # Not necessarily recommended: shown for educational purposes. defined($ref) or croak "oops: argument is not defined"; } say "Calling test_function with no arguments:"; eval { test_function(); }; say $@; say "Calling test_function with an undefined argument:"; eval { test_function(undef); }; say $@;
X:\>perl foo.pl Calling test_function with no arguments: oops: this function takes exactly one argument at foo.pl line 7 main::test_function() called at foo.pl line 17 eval {...} called at foo.pl line 16 Calling test_function with an undefined argument: oops: argument is not defined at foo.pl line 12 main::test_function(undef) called at foo.pl line 23 eval {...} called at foo.pl line 22 X:\>

Doing the same without the stack trace takes a little bit more work.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^3: Length of Array Passed as Reference
by eyepopslikeamosquito (Bishop) on Dec 10, 2020 at 09:03 UTC

    Yes, totally agree. I had a senior moment and forgot about Carp and its delightfully named cluck, croak and confess. Also forgot I responded to Best practices for handling errors (BTW, I see SunnyD snuck in a late reply to that old thread with a positive!!! node-rep - wouldn't happen today! :).

    I'll chuck in my customary reference to Conway's Perl Best Practices, Chapter 13 (Error Handling), where Damian wisely counsels you to "Have exceptions report from the caller's location, not from the place where they were thrown".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124930]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2022-01-28 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (72 votes). Check out past polls.

    Notices?