Open file in perl and STDERR contents problems - perl

I'm trying to write some unit tests for a perl file uploading script. I'm still pretty new to perl so I'm having some issues achieving the outcome I expect from my code.
Basically my thought process is that I can pass a test_only attribute along with the request that will tell the script to just grab a file already on the system rather than try to use an uploaded file.
I created a test file and put it in my output/tmp directory. I made sure to set its permissions to 775. Its just a simple .txt file that says "I am a test file".
What I expect to happen currently is that when I run my test script I should see the contents of the file printed out to the error log as well as some reference to the buffer(so I can verify the file is being opened properly). However, this is not happening, nothing is being put in the error log. I'm wondering if the file is being opened properly?
I'm sure I'm just missing something fundamental about how perl opens files. Any help will be appreciated. Thanks :)
This is the appropriate snippet of my code:
my $test_only = 1;
my $tmp_uploads_path = "/home/my_instance/output/tmp/";
if($test_only)
{
#put simulated file handle and file name here
$file = "";
$file_name = "test_file.txt";
}
else
{
$file = $q->upload('file')
|| die "No file data sent\n $!";
$file_name = $q->param('file_name')
|| die "No file_name sent\n $!";
}
########
#SAVE THE UPLOAD
########
my $bufsize = 1024;
my $buffer = '';
open(my $TMPFILE, ">".$tmp_uploads_path.$file_name);
binmode $TMPFILE;
print STDERR "=> ".Dumper($TMPFILE)."\n";
while(read ($TMPFILE, $buffer, $bufsize)){
print STDERR "=> ".Dumper($TMPFILE)."\n";
print STDERR "=> ".Dumper($buffer)."\n";
print $TMPFILE $buffer;
}
close($TMPFILE);

You opened the $TMPFILE for writing, due to the > mode. Therefore, you cannot read from it.
You should always put use strict; use warnings; at the top of your scripts, this would have alerted you to this problem!
You should open files like
my $name = ...;
open my $fh, "<", $name or die "Can't open $name: $!";
or
use autodie;
open my $fh, "<", $name;
That is, do proper error handling, and use the three-arg variant of open: handle, mode and name (don't concat mode and name, except on ancient perls).
I am also suprised that you are using read. You can get a similar effect by
local $/ = \$bufsize;
while (defined(my $buffer = <$TMPFILE>)) { ... }

Related

How Can I output the response to text file and open the text file when done

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?

Perl not able to tail rotated log file

I am using property based filters of rsyslog to send specific logs to seperate file where those logs will be parsed using perl.
This is my rsyslog entry
$template SPLIT,"/home/shivam/hello-%$YEAR%%$MONTH%%$DAY%%$HOUR%%$MINUTE%"
:msg, contains, "hello" -?SPLIT
So rsyslog will create separate files for logs coming after every minute. Files will be created like this hello-201505281139.
My perl script to parse these files is
use strict;
use warnings;
use POSIX qw(strftime);
my $date = strftime "%Y%m%d%H%M", localtime;
my $file = '/root/defer-'.$date;
open(my $fh,'<',$file) or die "unable to open file $!\n";
while(1) {
while(my $line = <$fh>){
print "$date\n";
print "$line";
}
sleep(2);
unless ($date == strftime "%Y%m%d%H%M", localtime) {
close($fh);
$date = strftime "%Y%m%d%H%M", localtime;
$file = '/root/defer-'. $date;
system("touch $file");
open(my $fh,'<',$file) or die "unable to open file $!\n";
}
}
In unless block i am checking that if minute has changed then i close previous file and open new file.
The reason i am creating new file from script and not waiting for rsyslog to create file is that the frequency of logs coming is not that much. So i create file and just start reading on it in hope that when any new log will come i will be able to read that.
But what is happening is that i am able to create new file but not able to read anything from that file.
This is what i am getting as warning
readline() on closed filehandle $fh at test.pl line 14.
Line 14 in my code is this line
while(my $line = <$fh>){
I am not able to see anything wrong in my code. Please suggest what is the mistake.
You have two different $fh lexical (my) variables,
So instead declaring new one
open(my $fh,'<',$file) or die "unable to open file $!\n";
keep using previously declared one,
open($fh,'<',$file) or die "unable to open file $!\n";

Zipping a file with perl results in an invalid archive

I am currently trying to zip some files with perl. The resulting file is printed, so a user who calls the page which executes the script can download or open the zip file.
Looking at the size of the zip file it seems everything worked ok, but if I try to open the file on the server no contents are shown. If I open the file after downloading it, the archive is invalid.
Here's the code:
my $zip = Archive::Zip->new();
my $i;
foreach $i(#files)
{
my $fh = $zip->addFile("$directoryPath$i") if (-e "$directoryPath$i");
}
my $zipFilePath = "Test.zip";
die 'Cannot create $zip_file_name: $!\n' if $zip->writeToFileNamed("$zipFilePath") != AZ_OK;
open (DLFILE, "<$zipFilePath");
#fileholder = <DLFILE>;
close (DLFILE);
print "Content-Type:application/x-download\n";
print "Content-Disposition:attachment;filename=$zipFilePath\n\n";
print #fileholder;
Can you please tell me where the error is?
I am running the code using xampp on my local windows machine.
Edit: The same happens when I use
use strict;
use warnings;
use autodie;
Edit: The first problem is solved by ysth, thanks for that. Now the archive is not invalid after downloading, but still no files are shown if I open it, while the zip-file's size seems to be correct.
You are corrupting it here:
open (DLFILE, "<$zipFilePath");
#fileholder = <DLFILE>;
close (DLFILE);
by opening it such that it translates "\r\n" to just "\n".
Try this:
open( DLFILE, '<:raw', $zipFilePath );

Perl : Cannot open a file with filename passed as argument

I am passing two filenames from a DOS batch file to a Perl script.
my $InputFileName = $ARGV[0];
my $OutputFileName = $ARGV[1];
Only the input file physically exists while the Outputfile must be created by the script.
open HANDLE, $OutputFileName or die $!;
open (HANDLE, ">$OutputFileName);
open HANDLE, ">$OutputFileName" or die $!;
All three fail.
However the following works fine.
open HANDLE, ">FileName.Txt" or die $!;
What is the correct syntax?
Edit : Error message is : No such file or directory at Batchfile.pl at line nn
The proper way is to use the three-parameter form of open (with the mode as a separate parameter) with lexical file handles. Also die doesn't have a capital D.
Like this
open my $out, '>', $OutputFileName or die $!;
but your last example should work assuming you have spelled die properly in your actual code.
If you are providing a path to the filename that doesn't exist then you also need to create the intermediate directories.
The die string will tell you the exact problem. What message do you get when this fails?
code:
$file_name = $ARGV[1];
open (OUTPUT "> $file_name") or error("unable to create or open $file_name");
print OUTPUT "hello world";
close(OUTPUT);
command to execute:
perl perl_file.pl data.txt
it will work try

How to read multiple files from a directory, extract specific strings and ouput to an html file?

Greetings,
I have the following code and am stuck on how I would proceed to modify it so it will ask for the directory, read all files in the directory, then extract specific strings and ouput to an html file? Thanks in advance.
#!/usr/local/bin/perl
use warnings;
use strict;
use Cwd;
print "Enter filename: "; # Should be Enter directory
my $perlfile =STDIN;
open INPUT_FILE, $perlfile || die "Could not open file: $!";
open OUTPUT, '>out.html' || die "Could not open file: $!";
# Evaluates the file and imports it into an array.
my #comment_array = ;
close(INPUT_FILE);
chomp #comment_array;
#comment_array = grep /^\s*#/g, #comment_array;
my $comment;
foreach $comment (#comment_array) {
$comment =~ /####/; #Pattern match to grab only #s
# Prints comments to screen
Print results in html format
# Writes comments to output.html
Writes results to html file
}
close (OUTPUT);
Take it one step at a time. You have a lot planned, but so far you haven't even changed your prompt string to ask for a directory.
To read the entered directory name, your:
my $perlfile =STDIN;
gives an error (under use strict;). Start by looking that error up (use diagnostics; automates this) and trying to figure out what you should be doing instead.
Once you can prompt for a directory name and print it out, then add code to open the directory and read the directory. Directories can be opened and read with opendir and readdir. Make sure you can read the directory and print out the filenames before going on to the next step.
a good starting point to learn about specific functions (from the cmd line)
perldoc -f opendir
However, your particular problem is answered as follows, you can also use command line programs and pipe them into a string to simplify file handling ('cat') and pattern matching ('grep').
#!/usr/bin/perl -w
use strict;
my $dir = "/tmp";
my $dh;
my #patterns;
my $file;
opendir($dh,$dir);
while ($file = readdir($dh)){
if (-f "$dir/$file"){
my $string = `cat $dir/$file | grep pattern123`;
push #patterns, $string;
}
}
closedir($dh);
my $html = join("<br>",#patterns);
open F, ">out.html";
print F $html;
close F;