I have a file in.txt with some text.
I am using IIS 7 local server on Windows. and isapiModule is working fine for perl.
When I run this perl code via browser it is printing to browser just print line (not data) but not writing to file out.txt and just opening the empty file out.txt (i.e. unable to write via browser).
My wwwroot folder has full control access for all users.
html code:
<html>
<body>
<form name="form1" action="perl.pl" method="post"
enctype="multipart/form-data">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
perl.pl:
open(file,"<in.txt");
#x=<file>;
open(OUT,">out.txt");
print"print to browser...";
print OUT"#x\n";#for printing to file
close file;
close OUT;
What may be the reason and solution.
UPDATE: program is writing when manually run via command drive.
Have you tried something like this?
#!/usr/bin/perl -w
use strict;
my $in = 'in.txt';
open my $file, '<', $in or die "Can't open $in\n";
my $outfile = 'out.txt';
open my $outprint, '>', $outfile or die "Can't write to $outfile\n";
print "print to browser…\n";
while (<$file>) {
chomp;
print $outprint "$_\n";
}
Related
I want to modify a Perl script to save the result to text file output.txt and open the results output.txt when task is done
I have tried used "use strict;
use warnings;" and other methods but I keep getting an errors' The Last error was "Couldn't open output.txt"
#!/usr/bin/perl
use HTTP::Request;
use LWP::UserAgent;
system('cls');
system "color c";
print"\n";
print" |||||||||||||||||Robots scanner|||||||||||||||||||||";
print "\n";
print " Scan Your site Site\n\n Example: www.test.com \n\n-> ";
$site=<STDIN>;
chomp $site;
if($site !~ /http:\/\//) { $site = "http://$site/"; };
print "\n";
#path = ('robotx.txt','robots.txt','robot.txt','search',);
foreach $robots(#path){
$url = $site.$robots;
$req = HTTP::Request->new(GET=>$url);
$useragent = LWP::UserAgent->new();
$response = $useragent->request($req);
my $filename = 'report.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
if ($response->is_success){
print ">>>>>>Robots Found !!!>>>: $url\n";
print $fh "out put has been generated by perl\n"; # THIS DOESN'T WORK THE report.txt is empty
print "done\n";
}else{
print "NotFound : $robots\n";
print "done\n";
# I want to open the file from windows explorer automatically here when its done
close $fh;
}
}
after running cmd as Admin , as you can see the report.txt file shows empty
I expect to see the out put of the Response
I also want perl to open the report.txt ( without going to windows explorer and opening it manually by the user ) I want it automatically open when its done but I wasn't able to achieve that.
Thanks!
Seems you're always overwriting your report file.
Your open is inside the foreach loop. The loop is done 4 times. You will only receive the output of the last run as the first 3 file creations will be overwritten by the last one.
You should put the open and close outside the loop.
If you only need the first result, you can leave, the loop by putting a last statement at the end of the "then-part" of your if.
Try changing open(my $fh, '>', $filename) to open(my $fh, '>>', $filename) (two arrows instead of one. This will add each new entry instead of delete the last one.
Here are some answers on opening Windows files with Perl:
How to run an executable file using Perl on Windows XP?
I have an HTML page in which I run a Perl script to get the value from HTML and writing it onto a file.
The problem I'm facing is that whenever I run the Perl script from the terminal as a root, the HTML page gets updated and runs fine, however if I try the same thing from HTML (using the Firefox browser) it fails. Perhaps because of a permission issue.
The following is an extract from my HTML page:
<div>
<form id="QA1_Insert" action="http://localhost/cgi-bin/person.pl" METHOD="POST" >
<table>
<tr>
<td class="columnLabel"> Name</td>
<td><input type="text" name="Name"></td>
<td class="columnLabel">Address:</td>
<td><input type="text" name="Description"></td>
</tr>
</table>
<input type="submit" value="submit"></form>
</div>
And here is my person.pl:
#!/usr/bin/perl -w
use strict;
use CGI ':standard';
use warnings;
use IPC::System::Simple qw(system capture);
use DBI;
use Cwd qw(chdir);
use FileHandle;
use Fcntl;
#sysopen (FILE ,"/root/info/person.txt",O_RDWR|O_CREAT,0755);
open (my $file,"<","/root/info/person.txt");
chmod 755,$file;
print FILE "<html><head></head><body><p>sakshi</p></body></html>";
close (FILE);
print
header(),
start_html(
-title => 'Command',
-text => '#520063'
);
print "Hello ";
print end_html();
I have tried creating it with sysopen, which doesn't work due to permission errors. I've also tried chmod. However, neither are working. Any suggestions on how to solve it would be appreciated.
Your web server does not run as the root user. That is correct. It should not do that. Since your file is in root's home directory, the web server user cannot access it. The chmod command will fail as well as the open.
Put your output file in a directory that the web server can access (and should access, unlike root's stuff) and it will work.
Note that you have a mix of lexical and global filehandles there. Decide which one you want. (You want $fh!) Also you are opening the file for reading with < and then you try printing to it. That will also not work.
You should check the return value of the open call. For example, you can say:
open my $fh, '<', $filename or die $!;
Check your web server's log file to see the errors. You are using strict and warnings, which is good, but you do not profit from the error messages if you do not look at them. It's probably saying 500 Internal Server Error. Your web server log has more information.
As an alternative, use CGI::Carp qw(fatalsToBrowser); will redirect them to the browser. Do not use this in production though.
Finally, note that CGI has been removed from the Perl core recently and that in recent releases of the CGI module on CPAN the HTML creation functions have been deprecated. They should not be used in new code.
I'm attempting to print out to two different files. For some reason, print statements work fine for one file, but not for the other. When I run this program, filter2.out consists of a single line that reads "Beginning". filter2.err remains empty.
open(OUTPUT, "> Filter2/filter2.out");
open(ERROR, "> Filter2/filter2.err");
print OUTPUT "Beginning\n";
print ERROR "Beginning\n";
UPDATE: So I was running this at the beginning of a larger program and realized that it only updates the ERROR file in batches or when the file is closed. Any idea why this occurs?
Consider adding
use strict;
use warnings;
to the top of your script. These statements will help catch errors that are otherwise silently ignored by Perl. In addition, consider adding error checking to your open calls: in all likelihood, it's not actually opening. I'd write it like this:
use strict;
use warnings;
open(OUTPUT, "> Filter2/filter2.out")
or die "Can't open filter2.out: $!";
open(ERROR, "> Filter2/filter2.err")
or die "Can't open filter2.err: $!";
print OUTPUT "Beginning\n";
print ERROR "Beginning\n";
for example, by just adding adding strict and warnings I got:
print() on closed filehandle OUTPUT at .\printer.pl line 6.
print() on closed filehandle ERROR at .\printer.pl line 7.
Hmm...!
By adding error checking, I got:
PS C:\dev> perl .\printer.pl
Can't open filter2.out: No such file or directory at .\printer.pl line 4.
Aah! Looking, I didn't have the folder. After I added the folder, everything ran fine. You'll probably find something similar.
Finally, you should probably also use the modern, lexical file handles. This helps catch other errors (like re-used handle names.) Thus, the final script would look like:
use strict;
use warnings;
open(my $output, ">", "Filter2/filter2.out")
or die "Can't open filter2.out: $!";
open(my $error, ">", "Filter2/filter2.err")
or die "Can't open filter2.err: $!";
print $output "Beginning\n";
print $error "Beginning\n";
Viola! Now you can see exactly where the problem fails, as it fails, and make sure that other libraries or code you write later can't accidentally interfere with your file handles.
You need to check that your files were properly opened. Also it's better to use local variables as file handles instead of bare words:
open( my $err, "> Filter2/filter2.err") or die "Couldn't open error: $!"
print $err "Beginning\n"
I am trying to read a UTF-8 encoded xml file. This file is of size around 8M and contains only one line.
I used below line to open this single line xml file:
open(INP,"<:utf8","$infile") or die "Couldn't open file passed as input, $!";
local $/ = undef;
my $inputfile = <INP>;
print $inputfile; ## Not working..
But after this line program get stuck and keep waiting.
I have tried other methods like binmode and decode but getting the same issue.
The same Program works when i change above mentioned file opening code to:
open(INP,"$infile") or die "Couldn't open file passed as input, $!";
local $/ = undef;
my $inputfile = <INP>;
print $inputfile; ## It works..
open(INP,"$infile") or die "Couldn't open file passed as input, $!";
binmode(INP, ":utf8");
local $/ = undef;
my $inputfile = <INP>;
print $inputfile; ## Not working..
Can you please help me what I am doing wrong here? I need to perform some operation on the input data and have to get utf8 encoded output.
I tried your last snippet here (Ubuntu 12.04, perl 5.14.2) and it works as expected. Only problem I have, is a difference between the input and output. Input file is UTF-8 and output is ISO-8859-1.
When I add
use utf8;
use open qw(:std :utf8);
this problem is gone, though. So this must be an environment issue.
I need to read/copy the contents of a file(test.pl) just as the way it is formatted and email it.
I am using the following code but I am unable to print anything out.
I get this error even though the file exists in the same directory.
Failed: No such file or directory
Code:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
open my $fh, '<', 'test.pl '
or die "Failed: $!\n";
my $text = do {
local $/;
<$fh>
};
close $fh
or die "Failed again: $!\n";
print $text, "\n";
It looks like there is an extra space in the filename you are trying to open. In your open statement, try changing 'test.pl ' to 'test.pl'.
if you are going to read files names from STDIN (user's input), you may want to trim them either by using regex (s/^\s+//....) or Text::Trim among other validations.