angle operator, backticks, and redirection

E

Ed Mancebo

I have a short perl script, it goes like this:

#!/usr/bin/perl

$line = <STDIN>;
print $line;
`echo $line > output`;

I was expecting it to read a line from std. input, then output the line
to the screen and to a file named 'output'. When I try this, the
output file is always empty, even though the print statement works.
Can someone tell me why?

Thanks,

Ed
 
A

A. Sinan Unur

I have a short perl script, it goes like this:

#!/usr/bin/perl

$line = <STDIN>;
print $line;
`echo $line > output`;

I was expecting it to read a line from std. input, then output the line
to the screen and to a file named 'output'. When I try this, the
output file is always empty, even though the print statement works.
Can someone tell me why?

There is a newline at the end of $line.

Sinan
 
S

Sherm Pendley

Ed said:
I have a short perl script, it goes like this:

Shelling out to call "echo" is quite possibly the most horribly inefficient
way I can imagine to store $line in a file. But I'm assuming that this is
just a minimal example as suggested in the posting guidelines, so I'm
answering the question as given, rather than suggesting an entirely
different approach.
#!/usr/bin/perl

use warnings;
use strict;
$line = <STDIN>;

my $line = <STDIN>;

# Or, more simply:

my $line = said:
print $line;
`echo $line > output`;

Store and print the results of that command - I think you'll find it
enlightening.

my $output = `echo $line > output`;
print $output;

Then, have a look at "perldoc -f chomp" to find out how to get rid of the
trailing newline in $line.

(BTW, if you really *are* using backticks and echo to write to a file, you
have a *lot* to learn. Have a look at "perldoc perlopentut".)

sherm--
 
W

Whitey Johnson

I have a short perl script, it goes like this:

#!/usr/bin/perl

$line = <STDIN>;
print $line;
`echo $line > output`;

Try using the system command instead of backticks.

#!/usr/bin/perl
use strict;
use warnings;

print "input something\n";
chomp(my $line = <STDIN>);
print "$line\n";
system "echo $line > output";
 
S

Sherm Pendley

Whitey said:
Try using the system command instead of backticks.

#!/usr/bin/perl
use strict;
use warnings;

print "input something\n";
chomp(my $line = <STDIN>);
print "$line\n";
system "echo $line > output";

Your code is correct - but your explanation is not. It's the chomp() in your
version that's making the difference, not your use of system() instead of
backticks.

sherm--
 
W

Whitey Johnson

On Wed, 12 Jan 2005 13:50:16 -0500, Sherm Pendley wrote:

Your code is correct - but your explanation is not. It's the chomp() in your
version that's making the difference, not your use of system() instead of
backticks.

sherm--

My bad. I saw that explained in the other email and didn't think it needed
rementioning. Now looking back at my email it does look like I am trying
to say that the system command is what would fix the OP's problem. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top