0

I am using cgi to call a perl function, and inside the perl function, I want it to write something in a txt. But it's not working for now. I have tried to run the cgi script on my apache server. The cgi could print the webpage as I requied, but I can't see the file that I want it to write. Seems like the perl script is not execute by the server.

The perl script is quite simple.The file name is perl predict_seq_1.pl

#!/usr/bin/perl -w
use strict;
use warnings;


my $seq = $ARGV[0];
my $txtfile = "test.txt";
open(my $FH, '>', $txtfile);
print $FH "test $seq success\n";
close $FH;

And the cgi part, I just call it by

system('perl predict_seq_1.pl', $geneSeq);
3
  • 1
    Start by adding error checking, as Perl built ins do not throw exceptions by default: open(my $FH, '>', $txtfile) or die "Failed to open $txtfile for writing: $!"; and for your system call, see the system function docs for examples. You can also check print and close for errors but these are less important. Commented Dec 12, 2018 at 23:01
  • This is total speculation, but my guess is the location of the file is not where you think it's going. Can you add an absolute path to a known location to see if that works? If it does, the contextual directory from wherever the script was called is probably where the file is being dropped. Commented Dec 13, 2018 at 0:03
  • if you can see the output from your CGI script in the browser use cwd ( perldoc.perl.org/Cwd.html ) to see where the script thinks the current working directory is, then look for the text.txt file there, or check if you have permissions to write there. Also do this: open (my $FH, '>', $txtfile) or die $!; this will tell you why it cannot write the file, if that is the problem. Also looking at the web server error log should give you more information, if you have access to the web server error log. Commented Dec 13, 2018 at 23:50

2 Answers 2

1

Your CGI program is going to run by a user who has very low permissions on your system - certainly lower than your own user account on that same system.

It seems likely that one of two things is happening:

  1. Your CGI process can't see the program you're trying to run.
  2. Your CGI program doesn't have permission to execute the program you're trying to run.

You should check the return value from system() and look at the value of $?.

Sign up to request clarification or add additional context in comments.

Comments

0

Either give system a single string, or give it individual arguments. Your script is attempting and failing to run the program perl predict_seq_1.pl, rather than having perl run the script predict_seq_1.pl.

system('perl', 'predict_seq_1.pl', $geneSeq);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.