Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

parsing rsync output

by eddor1614 (Beadle)
on Nov 21, 2007 at 14:00 UTC ( #652133=perlquestion: print w/replies, xml ) Need Help??
eddor1614 has asked for the wisdom of the Perl Monks concerning the following question:

hello. i need to make an HTML with the progress output of a rsync command. i'm doing this:
open (CORE, "/usr/bin/rsync $rsyncOPT --progress $outbox $inbox |"); while (<CORE>) { # code }
but the problem is that the progress update sends \r and the loop doesn't see the line until it finish the transfer and sends a \n. i try:
local $/="\r";
but on small files never sends a \r because the transfer finish quickly. i need to have two $/. any ideas?

Replies are listed 'Best First'.
Re: parsing rsync output
by gamache (Friar) on Nov 21, 2007 at 14:21 UTC
    When you use \r, it must be in a double-quoted string; in a single-quoted (non-interpolated) string, it will literally be a backslash followed by lowercase r. (I don't know that this is your entire problem here, but it's worth a fix.)
      yes. i made an error in the post. sorry. i'm using double quotes.
Re: parsing rsync output
by jasonk (Parson) on Nov 22, 2007 at 02:51 UTC

    You've got a buffering problem. One option would be to set $/ to a reference to an integer, which will cause perl to give you a chunk of that many bytes on each read, rather than reading a line at a time. If you do this though, you then have to deal with assembling the records into lines yourself. Something like this should work...

    open ( CORE, "/usr/bin/rsync $rsyncOPT --progress $outbox $inbox |" ); local $/ = \128; # read 128 bytes at a time my $buffer = ''; while( <CORE> ) { $buffer .= $_; while ( $buffer =~ s/^(.*?)[\r\n]// ) { local $_ = $1; # code } }

    Another (possibly better) option would be to use Expect, which is designed for this type of application...

    We're not surrounded, we're in a target-rich environment!
      thanks i will look into Expext.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://652133]
Approved by marto
[hippo]: But is under vendor_perl in your error message so you are mixing up modules installed from the packager and by hand. That's rarely a good plan.
[vedagiri89]: the prm fine if i run file directly..not working if i run through apache http file
[hippo]: Environments differ.
[vedagiri89]: so, how to fix that differ
[choroba]: option one is install everything from the vendor
[choroba]: option two is install everything by hand
[choroba]: option three is mixed the two, but know well what you're doing
[vedagiri89]: right..let me check

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2018-06-19 11:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.