打印到屏幕的输出不同于输出到文件
Print output to screen differs from output to file
使用 print
运算符将 key/value 数组写入外部 javascript 文件生成的输出与打印到屏幕的输出不同,尽管我尽了最大努力,但我不能'不知道为什么。具体来说,当按预期打印到屏幕时,我的 Perl 脚本输出:
var genusSpecies={"Adam's Needle (Yucca filamentosa)":["Adam's Needle (Yucca filamentosa)1.jpg","._Adam's Needle (Yucca filamentosa)2.jpg"...
然而,当打印到外部 JavaScript 文件时,点下划线 ._
不正确地添加到数组的键和值之前,输出:
var genusSpecies={"._Adam's Needle (Yucca filamentosa)":["._Adam's Needle (Yucca filamentosa)1.jpg","._Adam's Needle (Yucca filamentosa)2.jpg"...
这是我的 Perl 脚本:
#!/usr/bin/perl
use strict;
use warnings;
use JSON::PP;
use English; ## use names rather than symbols for special variables
my $dir = './Plants1024';
opendir my $dfh, $dir or die "Can't open $dir: $OS_ERROR";
my %genus_species; ## store matching entries in a hash
for my $file (readdir $dfh)
{
next unless $file =~ /.(jpe?g|png)$/i; ## entry must have jpg, jpeg, or png extension, case insensitive
my $genus = $file =~ s/\d*\.(?i)(jpe?g|png)(?-i)$//r;
push(@{$genus_species{$genus}}, $file); ## push to array, the @{} is to cast the single entry to a reference to a list
}
@{$genus_species{$_}} = sort @{$genus_species{$_}}
for keys(%genus_species);
my $str = (JSON::PP->new->utf8->canonical->encode(\%genus_species)); ## define array in Javascript outputting elements containing image file names
print "var genusSpecies=", $str; ## Inserted this line to test "print" output... prints properly WITHOUT adding "._"
my $filename = './Plants1024/PhotoArray.js';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh "var genusSpecies=", $str; ## saves JavaScript key/value array in external JavaScript file, BUT improperly prepends "._" to both keys and values
close $fh;
有趣的是,只有在我的 Raspberry Pi4 上执行此 Perl 脚本时,屏幕输出与文件输出不同,将 ._
添加到写入文件中的数组 keys/values。
在我的 Mac 上,正如预期的那样,屏幕和文件输出是相同的。更重要的是,在 Mac.
上执行我的 Perl 脚本时,._
没有被错误地添加到前面
也许数组 keys/values 中的空格导致了这种行为,但为什么只在 Raspberry Pi4 而不是 Mac 上?请指教
默认情况下,ls
隐藏以 .
开头的文件
$ ls -1
'Adam'\''s Needle (Yucca filamentosa)'
$ ls -a1
.
..
'._Adam'\''s Needle (Yucca filamentosa)'
'Adam'\''s Needle (Yucca filamentosa)'
这会让您认为您没有此类文件,但实际上它们存在。
readdir
不会忽略此类文件。您可以通过添加
来解决这个问题
next if $file =~ /^\./;
虽然 readdir
不会忽略前导 .
的文件,但它可能会忽略 Mac 上前导 ._
的文件。 Mac 创建此类文件以存储有关同名文件的额外信息。我猜您正在阅读的目录是在 Mac.
上创建的
使用 print
运算符将 key/value 数组写入外部 javascript 文件生成的输出与打印到屏幕的输出不同,尽管我尽了最大努力,但我不能'不知道为什么。具体来说,当按预期打印到屏幕时,我的 Perl 脚本输出:
var genusSpecies={"Adam's Needle (Yucca filamentosa)":["Adam's Needle (Yucca filamentosa)1.jpg","._Adam's Needle (Yucca filamentosa)2.jpg"...
然而,当打印到外部 JavaScript 文件时,点下划线 ._
不正确地添加到数组的键和值之前,输出:
var genusSpecies={"._Adam's Needle (Yucca filamentosa)":["._Adam's Needle (Yucca filamentosa)1.jpg","._Adam's Needle (Yucca filamentosa)2.jpg"...
这是我的 Perl 脚本:
#!/usr/bin/perl
use strict;
use warnings;
use JSON::PP;
use English; ## use names rather than symbols for special variables
my $dir = './Plants1024';
opendir my $dfh, $dir or die "Can't open $dir: $OS_ERROR";
my %genus_species; ## store matching entries in a hash
for my $file (readdir $dfh)
{
next unless $file =~ /.(jpe?g|png)$/i; ## entry must have jpg, jpeg, or png extension, case insensitive
my $genus = $file =~ s/\d*\.(?i)(jpe?g|png)(?-i)$//r;
push(@{$genus_species{$genus}}, $file); ## push to array, the @{} is to cast the single entry to a reference to a list
}
@{$genus_species{$_}} = sort @{$genus_species{$_}}
for keys(%genus_species);
my $str = (JSON::PP->new->utf8->canonical->encode(\%genus_species)); ## define array in Javascript outputting elements containing image file names
print "var genusSpecies=", $str; ## Inserted this line to test "print" output... prints properly WITHOUT adding "._"
my $filename = './Plants1024/PhotoArray.js';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh "var genusSpecies=", $str; ## saves JavaScript key/value array in external JavaScript file, BUT improperly prepends "._" to both keys and values
close $fh;
有趣的是,只有在我的 Raspberry Pi4 上执行此 Perl 脚本时,屏幕输出与文件输出不同,将 ._
添加到写入文件中的数组 keys/values。
在我的 Mac 上,正如预期的那样,屏幕和文件输出是相同的。更重要的是,在 Mac.
上执行我的 Perl 脚本时,._
没有被错误地添加到前面
也许数组 keys/values 中的空格导致了这种行为,但为什么只在 Raspberry Pi4 而不是 Mac 上?请指教
默认情况下,ls
隐藏以 .
$ ls -1
'Adam'\''s Needle (Yucca filamentosa)'
$ ls -a1
.
..
'._Adam'\''s Needle (Yucca filamentosa)'
'Adam'\''s Needle (Yucca filamentosa)'
这会让您认为您没有此类文件,但实际上它们存在。
readdir
不会忽略此类文件。您可以通过添加
next if $file =~ /^\./;
虽然 readdir
不会忽略前导 .
的文件,但它可能会忽略 Mac 上前导 ._
的文件。 Mac 创建此类文件以存储有关同名文件的额外信息。我猜您正在阅读的目录是在 Mac.