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.


In reply to Re: Length of Array Passed as Reference by eyepopslikeamosquito
in thread Length of Array Passed as Reference by Leudwinus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.