RSS Forums RSS
Please support our Perl advertiser: Programming Forums
Views: 14577 | Replies: 4 | Solved | Thread Tools  Display Modes
Reply
Join Date: Oct 2004
Posts: 57
Reputation: satimis is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
satimis satimis is offline Offline
Junior Poster in Training

How to print to file

  #1  
Nov 13th, 2004
Hi folks,

Perl
===

Instead of using following command printing output to a file;

$ perl AAA.pl > /pathto/name_of_file

is it possible adding "print file function" to the script

1) If YES
Please advise how to make it.

2) Can I add following bash syntax to the script
user=$(whoami);
now=$(date +%Y.%m.%d.%R);
File="/tmp/satimis/comparison_${user}_${now}.txt";

so that the printout file will carry name of creator, date and time, etc.

If possible please shed me some light to re-edit the script.

TIA

B.R.
satimis
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,336
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 12
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to print to file

  #2  
Dec 6th, 2004
on question number one, you could just pipe the output of the command to the lpd daemon, or whatever program you use:

perl AAA.pl | lpd

on question two, you could use the system() function, I do believe. I'm not a Perl programmer (I only play one on TV!), but you might be able to do something like this using backticks ` `:

user=`whoami`;
now=`date +%Y.%m.%d.%R`;
File="/tmp/satimis/comparison_"+$user+"_"+$now+".txt"; 

From Perl Documentation:

Originally Posted by From Perl Documentation
qx/STRING/
`STRING`

A string which is (possibly) interpolated and then executed as a system command with /bin/sh or its equivalent. Shell wildcards, pipes, and redirections will be honored. The collected standard output of the command is returned; standard error is unaffected. In scalar context, it comes back as a single (potentially multi-line) string. In list context, returns a list of lines (however you've defined lines with $/ or $INPUT_RECORD_SEPARATOR).

Because backticks do not affect standard error, use shell file descriptor syntax (assuming the shell supports this) if you care to address this. To capture a command's STDERR and STDOUT together:

$output = `cmd 2>&1`;

To capture a command's STDOUT but discard its STDERR:

$output = `cmd 2>/dev/null`;

To capture a command's STDERR but discard its STDOUT (ordering is important here):

$output = `cmd 2>&1 1>/dev/null`;

To exchange a command's STDOUT and STDERR in order to capture the STDERR but leave its STDOUT to come out the old STDERR:

$output = `cmd 3>&1 1>&2 2>&3 3>&-`;

To read both a command's STDOUT and its STDERR separately, it's easiest and safest to redirect them separately to files, and then read from those files when the program is done:

system("program args 1>/tmp/program.stdout 2>/tmp/program.stderr");

Using single-quote as a delimiter protects the command from Perl's double-quote interpolation, passing it on to the shell instead:

$perl_info = qx(ps $$); # that's Perl's $$
$shell_info = qx'ps $$'; # that's the new shell's $$

Note that how the string gets evaluated is entirely subject to the command interpreter on your system. On most platforms, you will have to protect shell metacharacters if you want them treated literally. This is in practice difficult to do, as it's unclear how to escape which characters. See the perlsec manpage for a clean and safe example of a manual fork() and exec() to emulate backticks safely.

On some platforms (notably DOS-like ones), the shell may not be capable of dealing with multiline commands, so putting newlines in the string may not get you what you want. You may be able to evaluate multiple commands in a single line by separating them with the command separator character, if your shell supports that (e.g. ; on many Unix shells; & on the Windows NT cmd shell).

Beware that some command shells may place restrictions on the length of the command line. You must ensure your strings don't exceed this limit after any necessary interpolations. See the platform-specific release notes for more details about your particular environment.

Using this operator can lead to programs that are difficult to port, because the shell commands called vary between systems, and may in fact not be present at all. As one example, the type command under the POSIX shell is very different from the type command under DOS. That doesn't mean you should go out of your way to avoid backticks when they're the right way to get something done. Perl was made to be a glue language, and one of the things it glues together is commands. Just understand what you're getting yourself into.

See I/O Operators for more discussion.
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Oct 2004
Posts: 57
Reputation: satimis is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
satimis satimis is offline Offline
Junior Poster in Training

Re: How to print to file

  #3  
Dec 6th, 2004
Hi alc6379,

Tks for your advice. Problem solved already.

B.R.
satimis
Reply With Quote  
Join Date: Dec 2007
Posts: 1
Reputation: surinder oberoi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
surinder oberoi surinder oberoi is offline Offline
Newbie Poster

Help Re: How to print to file

  #4  
Dec 7th, 2007
Originally Posted by satimis View Post
Hi folks,

Perl
===

Instead of using following command printing output to a file;

$ perl AAA.pl > /pathto/name_of_file

is it possible adding "print file function" to the script

1) If YES
Please advise how to make it.

2) Can I add following bash syntax to the script
user=$(whoami);
now=$(date +%Y.%m.%d.%R);
File="/tmp/satimis/comparison_${user}_${now}.txt";

so that the printout file will carry name of creator, date and time, etc.

If possible please shed me some light to re-edit the script.

TIA

B.R.
satimis



i have to print the output which is coming after running the program in the c++ compiler
tell me the way how to print that output or how to take screen shorts of that
the output consist of one graphical picture
Reply With Quote  
Join Date: Mar 2006
Posts: 684
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 42
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: How to print to file

  #5  
Dec 7th, 2007
This thread is three years old surinder oberoi and this forum is for asking perl questions.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 7:58 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC