Perl hashing - comparing keys and values

I read over perl doc, but I can't get my head around hashes. I am trying to find if a hash key exists, and if so, compare its value. What confuses me is that my searches say that you discover if a key exists if (exists $files{$key}), but what $files{$key}also gives meaning? The code I'm working on:

foreach my $item(@new_contents) {
    next if !-f "$directory/$item";
    my $date_modified = (stat("$directory/$item"))[9];

    if (exists $files{$item}) {
        if ($files{$item} != $date_modified {
            $files{$item} = $date_modified;
            print "$item has been modified\n";
        }
    } else {
        $files{$item} = $date_modified;
        print "$item has been added\n";
    }
}
+3
source share
2 answers

$files{$key}will really return the value of this key. But what if this value turns out to be false in a boolean context, for example, 0or ''(empty string)?

Consider this hash:

my %foo = ( red => 42, blue => 0, green => '', yellow => undef );

if ( $foo{blue} ), . , blue, , $foo{blue} . green yellow - undef .

exists () , - , , . ( keys, grep , .)

. exists .

+9

exists $hash{key} , , defined $hash{key} , , $hash{key} , (. http://perldoc.perl.org/perlsyn.html#Truth-and-Falsehood).

+3

Source: https://habr.com/ru/post/1741642/


All Articles