Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Length of Array Passed as Reference

by afoken (Chancellor)
on Dec 10, 2020 at 08:21 UTC ( [id://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 (Archbishop) 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?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-19 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found