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
Novice

Nov 9, 2013, 11:54 PM

Post #1 of 11 (1072 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 11 (1069 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
Novice

Nov 10, 2013, 6:54 AM

Post #3 of 11 (1066 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
Novice

Nov 10, 2013, 10:18 AM

Post #4 of 11 (1059 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 11 (1057 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 11 (1051 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
Novice

Nov 11, 2013, 7:46 AM

Post #7 of 11 (1039 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 11 (1034 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 11 (1031 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 11 (1026 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
Novice

Nov 11, 2013, 9:40 AM

Post #11 of 11 (1024 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.

 
 


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

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