Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Length of Array Passed as Reference

by eyepopslikeamosquito (Archbishop)
on Dec 10, 2020 at 03:23 UTC ( [id://11124927]=note: print w/replies, xml ) Need Help??


in reply to Length of Array Passed as Reference

In addition to the excellent answers already given, I embed below a test program, for educational purposes, to show how to perform more thorough checking of function arguments. Such belt-and-braces checking of function arguments is probably over the top for most programs ... though there are times when you may feel it is warranted. Understanding the extra checks below should also help you better understand references in Perl.

use strict; use warnings; use Data::Dumper; use Carp; # Note: you could do extra manual checking of function arguments. # Not necessarily recommended: shown for educational purposes. # In example below, test_function takes exactly one argument, a ref to + an ARRAY sub test_function { my $narg = @_; $narg == 1 or croak "oops: this function takes exactly one argumen +t, a ref to an array (you provided $narg arguments)"; my $ref = shift; defined($ref) or croak "oops: argument is undef"; my $reftype = ref($ref); $reftype eq '' and croak "oops: argument should be a reference"; $reftype eq 'ARRAY' or croak "oops: argument should be ARRAY ref ( +not a '$reftype' ref)"; warn "reftype='$reftype'\n"; # Note: next line will die with "Not an ARRAY reference error" if +$ref is not an ARRAY ref warn "num elts=", scalar(@$ref), "\n"; # ... function body, process array ref $ref ... print Dumper($ref); } my @test_array = ([1, 2, 3], [4, 5, 6]); my %test_hash = ( "one" => 1, "two" => 2 ); # Can test function being called with invalid arguments... # test_function(); # oops, no arguments # test_function(undef); # oops, passed undef # test_function( \@test_array, 2 ); # oops, two arguments # test_function(1); # oops, one argument but not a re +f # test_function( @test_array ); # oops, array (not array ref) # test_function( \%test_hash ); # oops, hash ref (not array ref) test_function( \@test_array ); # Correct! One argument, array re +f!

Update: Minor improvements to argument checking, based on responses below; switched from die to Carp croak.

Replies are listed 'Best First'.
Re^2: Length of Array Passed as Reference
by afoken (Chancellor) on Dec 10, 2020 at 08:21 UTC

    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". ;-)

      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".

Re^2: Length of Array Passed as Reference
by tobyink (Canon) on Dec 10, 2020 at 09:21 UTC

    Or:

    use feature 'state'; use Types::Standard -types; use Type::Params 'compile'; sub test_function { state $signature = compile( ArrayRef[Str] ); my ( $ref ) = &$signature; # function body goes here }

    The signature will check that the function was passed the correct number of arguments, the correct types, etc, and die if it wasn't.

      Nice. It seems to me your API embraces "Declarative trumps Imperative" (one of Conway's seven API design tips, mentioned at On Interfaces and APIs).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-03-29 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found