Perl:在Image :: ExifTool的哈希中保存哈希引用

问题描述 投票:0回答:1

尝试两天将ExifTool的所有可读(非二进制)输出保存到散列中,并将文件路径作为键:

use strict;
use warnings;
use Image::ExifTool;
use Data::Types qw(:all);
use v5.28;

my @directories = ("/some/filepath/to-pictures");
my @suffixes = qw(jpg jpeg gif png raw svg tif tiff psd orf nef eps cr2 arw);

my %file_catalog = ();
while (my $folder = shift @directories) {

    opendir(DirHandle, "$folder") or die "Cannot open $folder\n";
    my @files = readdir(DirHandle);
    closedir(DirHandle);

    foreach my $file (@files) {
        my $file_string = "$folder/$file";
        if (-f $file_string) {
            my $sep_pos = rindex($file, ".");
            my $end_chars = -($sep_pos - (length $file) + 1);
            my $suffix = substr $file, $sep_pos + 1, $end_chars;
            if (grep ( lc $suffix, @suffixes)) {
                my $exif_tool = new Image::ExifTool;
                my $info = $exif_tool->ImageInfo($file_string);
                say "Datei: $file_string";
                %file_catalog = ($file_string => $info);
                foreach (keys %{$info}) {
                    say "Key: $_ => Value: $$info{ $_ }";
                }
            }
        }
    }
}

也行不通:%file_catalog = ($file_string => %{$info});

但是,我要么在%file_catalog中获得不止一张图片的信息 - 你能解释一下吗?

或者我在"Can't use string as a HASH ref while "strict refs" in use"的提交历史中可以看到解除引用("Odd number of elements in hash assignment")或存储(repo)时遇到很多问题。

当然,并非所有图像都具有相同的exif-informations,每个图像都提供不同的key->值。

对于测试输出:

my $hash_size = keys %file_catalog;
print "\n---------------------------\n";
print "In 'file_catalog'-hash recorded image-path (key)-/ exif-data (value)-hashes: ", $hash_size;
print "\n---------------------------\n";

my ($i, $j) = 0;
foreach my $key (sort keys %file_catalog) {
    $i++;
    printf "%s. key: $key\n", uc chr($i + ord('A') - 1 );
    foreach my $inner_key (keys %{$file_catalog{ $key }}) {
        $j++;
        say "$j. inner key: $inner_key: $file_catalog{$key}{$inner_key}";
    }
}

也许有人可以帮助我理解。也可能有比我的新手代码更好的构造。

perl hash
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.