stavros has asked for the wisdom of the Perl Monks concerning the following question:

Hello

my programme is stored in ..Server\htdocs\cgi-bin with name glob.pl and is the following

#!/usr/bin/perl -w use warnings; use LWP 5.66; my $browser = LWP::UserAgent->new; $browser -> timeout(900); use CGI qw(:standard); print header, start_html( -title=> 'Page 1' , -bgcolor=>'#eeeeee'), p(h1({-align=>CENTER},'Trial')) ; @files1 = glob ("../Results/*.txt");#collect all the files into an arr +ay foreach my $file (@files1) { print p("s $file"); if ($file=~/(\S+)[\/](\S+)[\/](\S+).*/g) { print p("to $3"); unlink("$3"); if (unlink($3) == 0) { print "$3 deleted."; } else { print "File was not deleted."; } my $FileToWrite = "../Results/"; open (PearsonResult,">>$FileToWrite$3") || die "co +uldn't open the file $!"; print PearsonResult ("test\n "); close PearsonResult || die "can't close:$!"; } } print end_html;

the output when i run it in my browser is nothing. But when i create manually a txt file (with name Test1.txt) on ..Server\htdocs\Result i take as output

s ../Results/Test1.txt

to Test1.txt

Test1.txt deleted.

So i have two questions:

1.why the txt file is not created automated when i run the script from the begging without the need to created manually first and

2.why the txt file is not deleted finally when i run the programme since as i can see throught the glob and the if clause that i use it is stored in the variable $3 the Test1.txt.

When i run it for second time the script i see that it goes to Test1.txt and it writes the word test.But the third time it doesn't delete it and so i find for second time the word test.

Really many thanks in advance for your time.

Replies are listed 'Best First'.
Re: Delete txt file in a previous file
by Tanktalus (Canon) on Aug 10, 2011 at 19:25 UTC

    What is your current working directory when run by the webserver? I bet it's not what you think it is. So then when you do "../", it's looking in the wrong place, so of course it won't find anything. The glob comes back empty (because no files match the expression), so the loop is skipped, and nothing is output.

      Well it is ..Server\htdocs\cgi-bin. And inside cgi-bin i have the script glob.pl.I suppose it is the right one.As you can see i i use glob for collecting the txt files from the location Server\htdocs\Results where inside Results are supposed to be the txt.
        The current working directory is not necessarily the same directory as where your program is stored. This is a common problem when running on web servers. Don't use relative path names (like ../) use absolute path names instead.
Re: Delete txt file in a previous file
by Anonymous Monk on Aug 10, 2011 at 19:03 UTC

    Two suggestions: use strict; and use or die ... rather than || die

    Also, how is this supposed to work? Files generally can't be deleted twice.

    unlink("$3"); if (unlink($3) == 0)

      Thanks for your reply.

      now i take that

      s ../Results/YAL001C.txt

      to YAL001C.txt

      File was not deleted.

      and i still can't delete the file that exist on ...htdocs\results.

      Also when i delete it manually my code doesn't create it automated.I have to create it in order to write my code the word "test" in it.