Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > angle operator, backticks, and redirection

Reply
Thread Tools

angle operator, backticks, and redirection

 
 
Ed Mancebo
Guest
Posts: n/a
 
      01-12-2005
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

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-12-2005
"Ed Mancebo" <> wrote in
news: oups.com:

> 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
 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      01-12-2005
Ed Mancebo wrote:

> 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 = <>;

> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Whitey Johnson
Guest
Posts: n/a
 
      01-12-2005
On Tue, 11 Jan 2005 21:46:24 -0800, Ed Mancebo wrote:

> 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";


>
> 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


 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      01-12-2005
Whitey Johnson wrote:

> On Tue, 11 Jan 2005 21:46:24 -0800, Ed Mancebo wrote:
>
>> 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";


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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Whitey Johnson
Guest
Posts: n/a
 
      01-12-2005
On Wed, 12 Jan 2005 13:50:16 -0500, Sherm Pendley wrote:

<snip>
>
> 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.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-13-2005
Whitey Johnson <> wrote:

> in the other email


> looking back at my email



This is NOT email.

This is Usenet.

They are not the same, and the difference makes a difference!


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Point and Shoot with wide angle lens (28mm min), optical or electronicviewfinder, image-stabilization, and support for external flash (hot shoeor wireless) SMS 斯蒂文• 夏 Digital Photography 4 11-21-2007 09:00 PM
Not many "wide-angle" compacts but, heck, many are wide-angle anyway! JeffOYB@hotmail.com Digital Photography 10 01-09-2006 08:30 AM
Wide angle and telephoto Peter Billinghurst Digital Photography 6 12-04-2003 04:21 PM
Olympus C720 - Quick & dirty and cheap wide angle solution? G. de Haan Digital Photography 1 08-01-2003 05:20 PM
Sony F717 telephoto and wide angle lenses Satish K Digital Photography 1 07-11-2003 03:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57