Why did my Perl open () fail with the error "Failed to open the name of the file containing the new line"?

I tried to place ${date[0]}in my directory, which is equivalent 01252010, but @hitsnot printed. How did I manage to open the catalog to get the desired result? Thank.

ERROR: Could not open file name containing new line string. /total.pl, line 1, line 1.

#!/opt/perl/bin/perl -w

use strict;

open(FH,"/home/daily/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;
+3
source share
3 answers

You need to remove the line ending character. Use chomp:

chomp(my @date = <FH>);
+5
source
  • Use chomp for the resulting values, as Ivan suggested
  • Where did you find the syntax $ {date [0]}? use $ date [0].
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

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


All Articles