angle operator, backticks, and redirection

Discussion in 'Perl Misc' started by Ed Mancebo, Jan 12, 2005.

  1. Ed Mancebo

    Ed Mancebo Guest

    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
     
    Ed Mancebo, Jan 12, 2005
    #1
    1. Advertising

  2. "Ed Mancebo" <> wrote in
    news::

    > 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
     
    A. Sinan Unur, Jan 12, 2005
    #2
    1. Advertising

  3. 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
     
    Sherm Pendley, Jan 12, 2005
    #3
  4. 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
     
    Whitey Johnson, Jan 12, 2005
    #4
  5. 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
     
    Sherm Pendley, Jan 12, 2005
    #5
  6. 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.
     
    Whitey Johnson, Jan 12, 2005
    #6
  7. 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
     
    Tad McClellan, Jan 13, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Grant

    Angle class - how is my code ?

    Darren Grant, Dec 15, 2003, in forum: C++
    Replies:
    5
    Views:
    520
    Darren Grant
    Dec 16, 2003
  2. Bob Walton
    Replies:
    1
    Views:
    117
    Brian McCauley
    Jul 19, 2004
  3. John Bullock
    Replies:
    5
    Views:
    123
    Anno Siegel
    Aug 30, 2004
  4. Andrew Dunbar

    Extending the line input (angle) operator

    Andrew Dunbar, Oct 28, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    85
    Peter Wyzl
    Oct 28, 2004
  5. giloosh
    Replies:
    9
    Views:
    186
Loading...

Share This Page