How to print to file

Thread Solved
Reply

Join Date: Oct 2004
Posts: 57
Reputation: satimis is an unknown quantity at this point 
Solved Threads: 0
satimis satimis is offline Offline
Junior Poster in Training

How to print to file

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to print to file

 
0
  #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 ` `:

  1. user=`whoami`;
  2. now=`date +%Y.%m.%d.%R`;
  3. 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 Quick reply to this message  
Join Date: Oct 2004
Posts: 57
Reputation: satimis is an unknown quantity at this point 
Solved Threads: 0
satimis satimis is offline Offline
Junior Poster in Training

Re: How to print to file

 
0
  #3
Dec 6th, 2004
Hi alc6379,

Tks for your advice. Problem solved already.

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

Re: How to print to file

 
0
  #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 Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: How to print to file

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

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC