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

 



zak100
User

Nov 9, 2013, 11:54 PM

Post #1 of 12 (1639 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 12 (1636 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 12 (1633 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 12 (1626 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 12 (1624 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 12 (1618 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 12 (1606 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 12 (1601 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 12 (1598 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 12 (1593 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 12 (1591 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 12 (3 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.

 
 


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

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