Why does my Perl open() fail with "Unsuccessful open on filename containing newline"?
I tried to placed ${date[0]}
in my directory which is equivalent to 01252010
but @hits
not printed. How can I managed to open the directory to get the desired output? Thanks.
ERROR: Unsuccessful open on filename containing newline at ./total.pl line 11, line 1.
#!/opt/perl/bin/perl -w
use strict;
open(FH,"/home/dail开发者_运维知识库y/scripts/sms_hourly_stats/date.txt");
my @date = <FH>;
print $date[0];
my $path = "/home/daily/output/sms_hourly_stats/${date[0]}/TOTAL.txt";
open(FILE,"$path") or die "Unable to open $path: $!";
my @hits = <FILE>;
print @hits;
close FH;
close FILE;
You need to remove line ending symbol. Use chomp
:
chomp(my @date = <FH>);
- Use chomp on got values, as Ivan suggested
- Where did you find syntax ${date[0]} ? use $date[0].
#!/opt/perl/bin/perl -w
use strict;
open(FH,"/home/daily/scripts/sms_hourly_stats/date.txt");
my @date = <FH>;
my $dir;
print ${date[0]};
chomp($dir = ${date[0]});
my $path = "/home/daily/output/sms_hourly_stats/$dir/TOTAL.txt";
open(FILE,"$path") or die "Unable to open $path: $!";
my @hits = <FILE>;
print @hits;
close FH;
close FILE;
精彩评论