开发者

Why does my Perl open() fail with "Unsuccessful open on filename containing newline"?

开发者 https://www.devze.com 2022-12-17 20:28 出处:网络
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.

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>);


  1. Use chomp on got values, as Ivan suggested
  2. 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消