Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I/O redirection in perl

Status
Not open for further replies.

harishram

Programmer
Aug 10, 2005
6
0
0
US
Hi,

This piece of code works fine..the output is redirected correctly to mytest.out

#!/usr/bin/perl
$TEST="2005";
system("echo $TEST > mytest.out");

But the below piece of code does not work..the output file mytest.out is empty and display comes on the screen.

Assumption: The file "input" contains string 2005

#!/usr/bin/perl
open (TXNFILE, "input");
$TEST=<TXNFILE>;
system("echo $TEST > mytest.out");

Can someone please tell me why.

Thanks


 

open( INFILE, "toread.txt" )
or die( "Can not open toread.txt: $!" );

open( OUTFILE, ">towrite.txt" )
or die( "Can not open towrite.txt: $!" );

@contents=<INFILE>;
print OUTFILE @contents;


close( INFILE );
close( OUTFILE );

dmazzini
GSM System and Telecomm Consultant

 
Hi dmazzini,

Thanks for ur reply. Let me give a clearer picture of what i was trying to do..i am trying to execute a program CreateTemplate with $TRNXID as a parameter and i want the output to go to the file mytest.out.

system("/usr/bin/java CreateTemplate -tranId $TRNXID > mytest.out");

The output is coming onto the screen but is not going to mytest.out.
can u tell me why this is not working and how do i overcome it.

Thanks
 
how about:-

1) assigning the result of the system call to a scalar
2) write the contents of the scalar to your output file

Code:
$result = system("/usr/bin/java CreateTemplate -tranId $TRNXID");

open (OUT, "> mytest.out");
print OUT $result;
close OUT;


Kind Regards
Duncan
 
Sorry duncdeude,
I think you got that one wrong. You're gonna be writing out the return code and not the output of the command.
Why is no-one suggesting opening a pipeline?

Code:
#!/usr/bin/perl -w
use strict;

open IN, "/usr/bin/java CreateTemplate -tranId $TRNXID |"
     or die "Failed to open CreateTemplate";
open OUT, ">mytest.out" or die "Failed to create logfile";
print OUT while(<IN>);
close OUT;
close IN;

(very injured) Trojan.
 
Hi Trojan

Blimey - i am not awake this morning!

O.K. - is that because nothing will happen when run from the command line? i.e. no output to the screen is generated?


Kind Regards
Duncan
 
No, the system command returns the exit code of the process (integer 0-255), to get the stdout, use backticks or as in my case, a pipeline.


(very injured) Trojan.
 
Well - they say you learn something every day... and it's only 7:40 a.m. and i've learnt somehing already!

I always assumed that system("") was the same as backticks... probably because i never use system("")

I am a muppet!

Thanks Trojan


Kind Regards
Duncan
 
Hi Trojan,

Thanks for the solution. It worked fine..but had a question..In the file open command what is the significance of "|" at the end.

Thanks
 
The "|" char changes the open from a file open to a pipeline open.




(very injured) Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top