2

I am using CGI script for my website. And I have problem with opening a dynamically generated file. Here is the code:

#!/usr/bin/perl
my @output = `/export/es_share/Zhou/./notification_finder.sh range $date $time $range $ulh TestProd1 $actionname`;
my $filen = $output[0];
open(my $result, "<", $filen) or die "Can't open $filen - $!";
Do something with the file...

Always fails, with the output:

Can't open /var/tmp/notification-finder-1375086676-658183725.tmp - 
No such file or directory at /var/www/cgi-bin/appsupport/logapp_test/perltest.cgi line X.

While as this succeeds:

#!/usr/bin/perl
open(my $result, "<", /var/tmp/notification-finder-1375086676-658183725.tmp) or die "Can't open $filen - $!";
Do something with the file...

I have also checked if it was the problem of asynchronous execution of the backticks problem, but from my research on stackoverflow it does not seem to be the issue. I also tried this:

#!/usr/bin/perl
my @output = `/export/es_share/Zhou/./notification_finder.sh range $date $time $range $ulh TestProd1 $actionname`;
sleep(10);
my $filen = $output[0];
open(my $result, "<", $filen) or die "Can't open $filen - $!";
Do something with the file...

I have found this similar issue here, but I don't seem to have the same problem as the asker... Thanks for reading this far.

Community
  • 1
  • 1
Juto
  • 1,246
  • 1
  • 13
  • 24
  • 1
    Perhaps you can use something like [`File::Temp`](http://search.cpan.org/perldoc?File%3A%3ATemp). – TLP Jul 29 '13 at 08:45
  • @TLP could ou please elaborate a bit more? I am new to Perl. :) – Juto Jul 29 '13 at 08:46
  • No, I'm afraid I've never used that module. I'm not sure you can use it in this context, of passing a temp file name to a system command. – TLP Jul 29 '13 at 08:48
  • @TLP I will read on the link, thanks! – Juto Jul 29 '13 at 08:51

1 Answers1

4

The error indicates there is a newline on the end of $filen, otherwise it would be:

Can't open /var/tmp/notification-finder-1375086676-658183725.tmp - No such file or directory at /var/www/cgi-bin/appsupport/logapp_test/perltest.cgi line XXXXXXXXX.

Remove it with:

chomp $filen;
Juto
  • 1,246
  • 1
  • 13
  • 24
RobEarl
  • 7,862
  • 6
  • 35
  • 50