I completely agree that there is need for a test of arrays and hashes
(and scalars?) of "was the optional parameter passed in?".
There may be other pertinent uses that I'm not aware of.

I think that this sums up my unease about calling the test "defined".
This is Perl 5:

$ ~/test/undefined.pl
For scalar assignment
from... undef   empty   data
to
undef   undef   empty   data
empty   undef   empty   data
data    undef   empty   data

For array assignment
from... undef   empty   data
to
undef   undef   undef   data
empty   empty   empty   data
data    empty   empty   data



(deprecation warnings turned off, script appended)

ie "undefined" in scalars is a property of the value, and always passed to
the assignee in assignment.

whereas for arrays, not so. The shape of that table is definitely an
implementation detail exposed.

Nicholas Clark

#!/usr/bin/perl -w
use strict;

sub scalar {
    my $what = shift;
    my $scalar;
    return \$scalar if $what eq 'undef';
    $scalar = $what eq 'empty' ? '' : 'data';
    return \$scalar;
}

sub array {
    my $what = shift;
    my $array = [];
    return $array if $what eq 'undef';
    push @$array, "data";
    pop @$array if $what eq 'empty';
    return $array;
}

my @states = qw(undef empty data);
foreach my $what (qw(scalar array)) {
    # strict refs can be circumvented :-)
    my $gen = main->can($what);
    print "For $what assignment\n";
    print join "\t", 'from...', @states;
    print "\nto\n";
    foreach my $to (@states) {
        print "$to";
        foreach my $from (@states) {
            my $src = &$gen($from);
            my $dest = &$gen($to);
            my $result;
            if ($what eq 'scalar') {
                $$dest = $$src;
                $result = defined $$dest
                    ? length $$dest ? 'data' : 'empty' : 'undef';
            } else {
                no warnings 'deprecated';
                @$dest = @$src;
                $result = defined @$dest
                    ? @$dest ? 'data' : 'empty' : 'undef';
            }
            print "\t$result";
        }
        print "\n";
    }
    print "\n";
}
  
__END__

Reply via email to