CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Storing the output of perl command in a file

 

First page Previous page 1 2 Next page Last page  View All


zak100
User

Nov 9, 2013, 11:54 PM

Post #1 of 27 (2889 views)
Storing the output of perl command in a file Can't Post

Hi,
I am trying to store the output of a perl command in a file but its creating a file with zero size. I am using the command in a script:

Code
system ("perl","$fileName > output");


Instead of perl script when i executed command on dos prompt i got following the :

Code
D:\Zulfi\PERL MAIN FOLDER>perl filetest1.pl > output 
Can't locate object method "aaaaaaaaa" via package "bbbbbbbbbb" (perhaps you for
got to load "bbbbbbbbbb"?) at filetest1.pl line 2.

D:\Zulfi\PERL MAIN FOLDER>dir o*
Volume in drive D has no label.
Volume Serial Number is 8E4F-05BE

Directory of D:\Zulfi\PERL MAIN FOLDER

11/10/2013 12:45 PM 0 output
1 File(s) 0 bytes
0 Dir(s) 524,334,653,440 bytes free

D:\Zulfi\PERL MAIN FOLDER>type filetest1.pl

aaaaaaaaa
bbbbbbbbbb
cccccccccccc
D:\Zulfi\PERL MAIN FOLDER>


Somebody plz guide me.

Zulfi.


Laurent_R
Veteran / Moderator

Nov 10, 2013, 2:13 AM

Post #2 of 27 (2886 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

I really don't understand what you are trying to do, but the bottom line is certainly that your filetest1.pl is not a valid Perl program and has no chance whatsoever of compiling correctly.

If you want to print your strings of characters to the output file, you should at least do something like this:


Code
print "aaaaaaaaa \n"; 
print "bbbbbbbbbb \n";
print "cccccccccccc \n";



zak100
User

Nov 10, 2013, 6:54 AM

Post #3 of 27 (2883 views)
Re: [Laurent_R] Storing the output of perl command in a file [In reply to] Can't Post

Thanks for your reply. When i would compile this file, i would get an error. I want to store this error in a file.

Zulfi.


zak100
User

Nov 10, 2013, 10:18 AM

Post #4 of 27 (2876 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

Hi,
I am showing my complete code. I want to store output of perl command in a file (testfile). My code is:

Code
 
#!C:/Perl64/bin/perl.exe
my $dirstr = 'D:/Zulfi/dir1';
print $dirstr;

$fileName = $dirstr . "/data1.txt";

mkdir $dirstr unless -d $existingdir; # Check if dir exists. If not create it.


open (MYFILE, ">$fileName");
print MYFILE "Bob\n";
close (MYFILE);

open (MYFILE, ">TestFile") || die "\n Could not create write file.\n\n";
print MYFILE `perl $fileName $_`;


The code is compiling but its not storing any error message in TestFile. TestFile is of zero bytes. Kindly guide me.
I want to do some thing like :
http://stackoverflow.com/questions/2870887/how-can-i-write-output-to-a-new-external-file-with-perl
Zulfi.


wickedxter
User

Nov 10, 2013, 11:40 AM

Post #5 of 27 (2874 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

read this: http://www.perlmonks.org/?node_id=51097


Laurent_R
Veteran / Moderator

Nov 10, 2013, 2:19 PM

Post #6 of 27 (2868 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

The first thing you should do is to add the following lines near the top of your file:


Code
use warnings; 
use strict:


This will force you to declare all the variables that you use (with the my operator), but you will save tremendous gains because the compiler will tell you about many of the errors that you are doing.

For example, it will tell you that the $existingdir has not been declared anywhere, so that your test:


Code
... unless -d $existingdir


is just useless.

Then, you write "Bob\n" into the file $filename. Fine.

But the final line of your script


Code
print MYFILE `perl $fileName $_`;

just does not make any sense: you are calling a system command to execute the Perl script $filename, which does not contain a valid Perl script and passing a parameter ($_) which is not defined. This is doomed to fail: Perl will not be able to compile a file containing simply "Bob". I suspect you are trying to do something else, but can't figure out what exactly.

Please explain what you are trying to do with this last command of your script.


zak100
User

Nov 11, 2013, 7:46 AM

Post #7 of 27 (2856 views)
Re: [Laurent_R] Storing the output of perl command in a file [In reply to] Can't Post

Hi,
I feel sorry for not making myself clear related to my problem. However i thank you people for your continued interest in my problem. I compile my code and i got some error. I want to store the error message in a file. For example right now i am getting following error:


Code
D:\Zulfi\PERL MAIN FOLDER>perl filecompileoutput.pl 
D:/Zulfi/dir1Can't locate object method "Bobaaaaaaaaaaaaaa" via package "Bobaaaa
aaaaaaaaaa" (perhaps you forgot to load "Bobaaaaaaaaaaaaaa"?) at D:/Zulfi/dir1/d
ata1.pl line 1.


I want to store the error message:

Quote

D:/Zulfi/dir1Can't locate object method "Bobaaaaaaaaaaaaaa" via package "Bobaaaa
aaaaaaaaaa" (perhaps you forgot to load "Bobaaaaaaaaaaaaaa"?) at D:/Zulfi/dir1/d
ata1.pl line 1.

in a file.

My code is and other information is listed below:

Code
D:\Zulfi\PERL MAIN FOLDER>type filecompileoutput.pl 
#!C:/Perl64/bin/perl.exe
my $dirstr = 'D:/Zulfi/dir1';
print $dirstr;

$fileName = $dirstr . "/data1.pl";

mkdir $dirstr unless -d $existingdir; # Check if dir exists. If not create it.


open (MYFILE, ">$fileName");
print MYFILE "Bobaaaaaaaaaaaaaa\n";
print MYFILE "Bobaaaaaaaaaaaaaa\n";
print MYFILE "Bobaaaaaaaaaaaaaa\n";
close (MYFILE);

open (MYFILE, ">TestFile") || die "\n Could not create write file.\n\n";
print MYFILE `perl $fileName`;


close (MYFILE);


D:\Zulfi\PERL MAIN FOLDER>dir TestFile
Volume in drive D has no label.
Volume Serial Number is 8E4F-05BE

Directory of D:\Zulfi\PERL MAIN FOLDER

11/11/2013 08:35 PM 0 TestFile
1 File(s) 0 bytes
0 Dir(s) 524,333,301,760 bytes free

D:\Zulfi\PERL MAIN FOLDER>cd ..

D:\Zulfi>cd dir1

D:\Zulfi\dir1>type data1.pl
Bobaaaaaaaaaaaaaa
Bobaaaaaaaaaaaaaa
Bobaaaaaaaaaaaaaa

D:\Zulfi\dir1>


It shows that it has created the TestFile but its size is zero. This is my problem "How to store data in TestFile which is to store error messages generated during comiling a program using perl?"

Kindly guide me.

Zulfi.


FishMonger
Veteran / Moderator

Nov 11, 2013, 8:47 AM

Post #8 of 27 (2851 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

Repeating yourself multiple times without following the advise you've been given won't help to finding the answer.

1) Normal/standard output of a command go out to STDOUT.

2) Error messages go out to STDERR.

3) When you execute a command via backticks, you receive the STDOUT stream, but not the STDERR.

4) To receive both STDOUT and STDERR, you need to do I/O redirection i.e., redirect STDERR to STDOUT.


Code
print MYFILE `perl $fileName 2>&1`;



(This post was edited by FishMonger on Nov 11, 2013, 8:48 AM)


FishMonger
Veteran / Moderator

Nov 11, 2013, 9:03 AM

Post #9 of 27 (2848 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

A better option for separate handing of the STDOUT and STDERR data streams is to use IPC::Open3 to run your command instead of using backticks.

IPC::Open3 - open a process for reading, writing, and error handling using open3()
http://search.cpan.org/~rjbs/perl-5.18.1/ext/IPC-Open3/lib/IPC/Open3.pm


Laurent_R
Veteran / Moderator

Nov 11, 2013, 9:36 AM

Post #10 of 27 (2843 views)
Re: [FishMonger] Storing the output of perl command in a file [In reply to] Can't Post


In Reply To


Code
print MYFILE `perl $fileName 2>&1`;



It seems the OP is working under Windows. I am not sure whether this type of redirection can work under Windows.


zak100
User

Nov 11, 2013, 9:40 AM

Post #11 of 27 (2841 views)
Re: [FishMonger] Storing the output of perl command in a file [In reply to] Can't Post

Hi,
I tried the first option b/c i was not able to follow the details of 2nd one.

My code after imrovement is now:

Code
#!C:/Perl64/bin/perl.exe 
my $dirstr = 'D:/Zulfi/dir1';
print $dirstr;

$fileName = $dirstr . "/data1.pl";

mkdir $dirstr unless -d $existingdir; # Check if dir exists. If not create it.


open (MYFILE, ">$fileName");
print MYFILE "Bobaaaaaaaaaaaaaa\n";
print MYFILE "Bobaaaaaaaaaaaaaa\n";
print MYFILE "Bobaaaaaaaaaaaaaa\n";
close (MYFILE);

open (MYFILE, ">TestFile") || die "\n Could not create write file.\n\n";
print MYFILE `perl $fileName 2>&1`;


close (MYFILE);


Thanks for solving my problem.

Zulfi.


zak100
User

Nov 27, 2014, 10:32 AM

Post #12 of 27 (1253 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

Hi,
I have converted this into a cgi-script. The code is:

Code
#!C:/Perl64/bin/perl.exe 


use DBI;
use CGI;
use CGI::Session;
use Path::Class;

my $existingdir;
my $cgi;
my $dirstr;
my $filename;
my $TextMessage;

$cgi = new CGI;

##for creating a unique file for each client we have to do session management
##right now just compile the code for the client

$ClientCode = $cgi->param ('ClientCode') || '';
#storing the code in file


my $dirstr = 'D:/Zulfi/dir1';


$fileName = $dirstr . "/filetest1.pl";

mkdir $dirstr unless -d $existingdir; # Check if dir exists. If not create it.


open (MYFILE, ">$fileName")or die "Can't open '$fileName'\n";
print MYFILE $ClientCode;
close (MYFILE);

open (MYFILE, ">TestFile") || die "\n Could not create write file.\n\n";
print MYFILE `perl $fileName 2>&1`;
close (MYFILE);

open(MYFILE, "<TestFile") or die "Unable to open file, ";

my @entire_file=<MYFILE>; #Slurp!
close (MYFILE);

print "Content-type: text/html\n\n";
print <<EOF;
<HTML>
<HEAD>
<TITLE> Coding Page </TITLE>
</Head>
<BODY>
<textarea name="ProjectCode" rows="5" cols="60">

</textarea></p>
<form method="post" action="compile.pl">

<p>Client Code:
<textarea name="ClientCode" rows="5" cols="60">
EOF
print $ClientCode;


print <<EOF;
</textarea>

</p>


<p><input type="submit" name="compile" value="compile"></p>

</form>
<textarea name="Output_Error" rows="5" cols="60">
EOF
print @entire_file;
print <<EOF;
</textarea></p>
</BODY></HTML>

EOF
exit;


Its storing the output of command in TestFile. But if i give error type code in code window like:

print qqqqqqqqqqqq

then it will not store the error message in the TestFile.
The program consists of 3 windows (text areas). I would write code in the client window (middle one). If compile button is pressed, then it would store the output or error in the output_error window (bottom one). Right now its only storing output of commands like if i write following in client window:
print "AAAAA";
print "BBBBB";
Then the output would store in output_error window but if there is a error as in th example below:

kkkkkkkkkkkkkkkk

then it will not store the error message of perl in the output window. Please guide me.

Zulfi.


BillKSmith
Veteran

Nov 28, 2014, 1:00 PM

Post #13 of 27 (1182 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

If I understand your question correctly, it has nothing to do with perl. You want to know how to redirect the error output of a command to a file. The answer would depend on your operating system (and shell). Your file paths suggest that you are using Windows. I have never seen a way to do it in that environment. Perhaps a Windows forum would be of more help.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Nov 28, 2014, 1:20 PM

Post #14 of 27 (1178 views)
Re: [BillKSmith] Storing the output of perl command in a file [In reply to] Can't Post

The I/O redirection is handled the same as *nix.


Laurent_R
Veteran / Moderator

Nov 29, 2014, 3:29 AM

Post #15 of 27 (1148 views)
Re: [FishMonger] Storing the output of perl command in a file [In reply to] Can't Post


In Reply To
The I/O redirection is handled the same as *nix.


Thanks, FishMonger, I did not know that.


BillKSmith
Veteran

Nov 29, 2014, 5:49 AM

Post #16 of 27 (1142 views)
Re: [FishMonger] Storing the output of perl command in a file [In reply to] Can't Post

I believe that the OP's original mistake was to redirect the standard output (which is the same in *nix and windows) rather than the error output. I do not know remember how to redirect errors on any system. Please give us an example.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Nov 29, 2014, 6:13 AM

Post #17 of 27 (1139 views)
Re: [BillKSmith] Storing the output of perl command in a file [In reply to] Can't Post

0 is the file descriptor for stdin
1 is the file descriptor for stdout
2 is the file descriptor for stderr

Redirect stderr to stdout

Code
script.pl 2>&1


Redirect stderr to a file

Code
script.pl 2>&filename


http://en.wikipedia.org/wiki/File_descriptor
http://en.wikipedia.org/wiki/Redirection_%28computing%29


zak100
User

Nov 29, 2014, 10:11 AM

Post #18 of 27 (1128 views)
Re: [FishMonger] Storing the output of perl command in a file [In reply to] Can't Post

Hi,
I have tried my code as a non-cgi program. Its working fine.

Code
use CGI; 
use CGI::Session;
use Path::Class;

my $existingdir;
my $cgi;
my $dirstr;
my $filename;
my $TextMessage;

$cgi = new CGI;

##for creating a unique file for each client we have to do session

management
##right now just compile the code for the client

$ClientCode = "print 'ABCDE\n' print 'abcde' print '12345' ";
#storing the code in file


my $dirstr = 'D:/Zulfi/dir1';


$fileName = $dirstr . "/filetest1.pl";

mkdir $dirstr unless -d $existingdir; # Check if dir exists. If not create it.


open (MYFILE, ">$fileName")or die "Can't open '$fileName'\n";
print MYFILE $ClientCode;
close (MYFILE);
open (MYFILE, ">TestFile") || die "\n Could not create write file.\n\n";
print MYFILE `perl $fileName 2>&1`;
close (MYFILE);


Also I found a documeion from:
http://www.perlhowto.com/executing_external_commands

which says the same thing which you [FishMonger]told me last time:



Quote
Example:

#-- scalar context
$result = `command arg1 arg2`;

#-- the same command in list context
@result = `command arg2 arg2`;

Notice that the only output captured is STDOUT, to collect messages sent to STDERR you should redirect STDERR to STDOUT


I would try to change the logic & do the explicit redirection as you showed.

Zulfi.


(This post was edited by zak100 on Nov 29, 2014, 10:17 AM)


FishMonger
Veteran / Moderator

Nov 29, 2014, 10:21 AM

Post #19 of 27 (1124 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post


Quote
mkdir $dirstr unless -d $existingdir;

You never assigned a value to $existingdir, so the unless -d will always return false which means that even if the directory exists, your statement will always try to create the directory.


zak100
User

Nov 29, 2014, 11:07 AM

Post #20 of 27 (1121 views)
Re: [FishMonger] Storing the output of perl command in a file [In reply to] Can't Post

Hi,
I assigned the value:

Code
my $existingdir="D:/Zulfi/dir1";


But still the output is same. If i type

Code
print "AAAAAA";

in the code window, i would get "AAAAAA" in the Output_Error text area. But if i give an error code like:

Code
print "AAAAAA'


It writes Nothing in the Output_error window.

Please guide me.

Zulfi.


FishMonger
Veteran / Moderator

Nov 29, 2014, 11:38 AM

Post #21 of 27 (1117 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

It's hard to guide you when your posts don't make any sense.

Post your complete script as a file attachment as well as any accompanying file(s) that are needed so that we can run a test to see if we can duplicate you problem.


zak100
User

Nov 29, 2014, 9:39 PM

Post #22 of 27 (1011 views)
Re: [FishMonger] Storing the output of perl command in a file [In reply to] Can't Post

Hi,
My code is same as in Post#12 only I have changed the $existingdir on your advice. The fresh problem starts from this post. Earlier I wrote this problem but it was related to non-cgi script & guided me. Now I have transformed this non-cgi code (solved in post 11) into a cgi code post#12.
I am gain posting this code. There are two files: (i)codeform.pl & also (ii)compile.pl. I have attached the files.

codeform.pl:

Code
#!C:/Perl64/bin/perl.exe 
print "Content-type: text/html\n\n";
print <<EOF;
<HTML>
<HEAD>
<TITLE> Coding Page </TITLE>
</Head>
<BODY>
Project Code: <textarea name="ProjectCode" rows="5" cols="60">

</textarea></p>

<form method="post" action="compile.pl">

<p>Client Code: <textarea name="ClientCode" rows="5" cols="60">

</textarea></p>


<p><input type="submit" name="compile" value="compile"></p>

</form>
Output_Error<textarea name="Output_Error" rows="5" cols="60">

</textarea></p>
</BODY></HTML>
EOF
exit;


and second file is:compile.pl


Code
#!C:/Perl64/bin/perl.exe 


use DBI;
use CGI;
use CGI::Session;
use Path::Class;

my $existingdir="D:/Zulfi/dir1";
my $cgi;
my $dirstr;
my $filename;
my $TextMessage;

$cgi = new CGI;

##for creating a unique file for each client we have to do session management
##right now just compile the code for the client

$ClientCode = $cgi->param ('ClientCode') || '';
#storing the code in file


my $dirstr = 'D:/Zulfi/dir1';


$fileName = $dirstr . "/filetest1.pl";

mkdir $dirstr unless -d $existingdir; # Check if dir exists. If not create it.


open (MYFILE, ">$fileName")or die "Can't open '$fileName'\n";
print MYFILE $ClientCode;
close (MYFILE);

open (MYFILE, ">TestFile") || die "\n Could not create write file.\n\n";
print MYFILE `perl $fileName 2>&1`;
close (MYFILE);

open(MYFILE, "<TestFile") or die "Unable to open file, ";

my @entire_file=<MYFILE>; #Slurp!
close (MYFILE);

print "Content-type: text/html\n\n";
print <<EOF;
<HTML>
<HEAD>
<TITLE> Coding Page </TITLE>
</Head>
<BODY>
<textarea name="ProjectCode" rows="5" cols="60">

</textarea></p>
<form method="post" action="compile.pl">

<p>Client Code:
<textarea name="ClientCode" rows="5" cols="60">
EOF
print $ClientCode;


print <<EOF;
</textarea>

</p>


<p><input type="submit" name="compile" value="compile"></p>

</form>
<textarea name="Output_Error" rows="5" cols="60">
EOF
print @entire_file;
print <<EOF;
</textarea></p>
</BODY></HTML>

EOF
exit;



I have no other files. Please guide me.
Thanks for your considerations.
Zulfi.
Attachments: compile.pl (1.48 KB)
  codeform.pl (0.53 KB)


FishMonger
Veteran / Moderator

Nov 30, 2014, 7:55 AM

Post #23 of 27 (981 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

Even though your code is very poorly written, it worked correctly when I tested it.

Instead of posting a printscreen of my browser, I'll post the resulting html code it generated.


Code
<html> 
<head>
<title> Coding Page </title>
</head>
<body>
<textarea cols="60" rows="5" name="ProjectCode"> </textarea>
<p></p>
<form action="compile.pl" method="post">
<p>
Client Code:
<textarea cols="60" rows="5" name="ClientCode">print "hello'</textarea>
</p>
<p>
<input type="submit" value="compile" name="compile">
</p>
</form>
<textarea cols="60" rows="5" name="Output_Error">Can't find string terminator '"' anywhere before EOF at D:/test/filetest1.pl line 2. </textarea>
<p></p>
</body>
</html>



(This post was edited by FishMonger on Nov 30, 2014, 9:44 AM)


zak100
User

Nov 30, 2014, 9:39 AM

Post #24 of 27 (965 views)
Re: [FishMonger] Storing the output of perl command in a file [In reply to] Can't Post

Hi,
Why its showing the EOF error?
Did you type any valid/wrong command on the code window?
I have compile the code. I am not getting EOF error:

Code
C:\Apache\cgi-bin>perl codeform.pl 
Content-type: text/html

<HTML>
<HEAD>
<TITLE> Coding Page </TITLE>
</Head>
<BODY>
Project Code: <textarea name="ProjectCode" rows="5" cols="60">

</textarea></p>

<form method="post" action="compile.pl">

<p>Client Code: <textarea name="ClientCode" rows="5" cols="60">

</textarea></p>


<p><input type="submit" name="compile" value="compile"></p>

</form>
Output_Error<textarea name="Output_Error" rows="5" cols="60">

</textarea></p>
</BODY></HTML>

C:\Apache\cgi-bin>perl compile.pl
Content-type: text/html

<HTML>
<HEAD>
<TITLE> Coding Page </TITLE>
</Head>
<BODY>
<textarea name="ProjectCode" rows="5" cols="60">

</textarea></p>
<form method="post" action="compile.pl">

<p>Client Code:
<textarea name="ClientCode" rows="5" cols="60">
</textarea>

</p>


<p><input type="submit" name="compile" value="compile"></p>

</form>
<textarea name="Output_Error" rows="5" cols="60">
</textarea></p>
</BODY></HTML>


C:\Apache\cgi-bin>



Zulfi.


FishMonger
Veteran / Moderator

Nov 30, 2014, 9:50 AM

Post #25 of 27 (961 views)
Re: [zak100] Storing the output of perl command in a file [In reply to] Can't Post

The html code I posted is the result of submitting the "codeform.pl" script to the "compile.pl" script i.e., it's the output "compile.pl" generated. The input I submitted in the "codeform.pl" script is shown in red.

The error message it displayed in the "Output_Error" textbox is exactly what should be expected from running that syntactically incorrect perl statement.

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives