unescaping a carriage return in perl
I need to get some info out of MPlayer, namely the stdout line that shows the current position and sync of the file and such like. Now, the thing is that this line (from looking at the c source code) terminates in a carriage return ( \r ) which results in a very handy output of this line just overwriting the last, rather than churning out screen after screen of the same info.
so now the problem comes when i wish to read it in from a perl open command, as the line doesn't come into view for me, all those eding in \n are naturally fine, but i don't get the carriage return lines, which is what i want! Any suggestions? |
There are two ways of which I can think to solve this problem.
1. Alter the line input seperator ($/) so that is is a carriage return, instead of a newline. Code:
{ local $/ = "\r"; 2. split the input at the carriage returns. Code:
while(<IN>) { I am thinking that #1 will work best for you. My advice would be to only set $/ to "\r" at the very point of the code that you will be reading such a line from the input stream, and set it back to "\n" once you're done. This will allow you to also read normal lines of data from the input stream, if the need is there. I hope this helps! TLD |
ahh that's great, i'd just got the same solution from someone in irc, using the second method... work's great now. actually i was just abuot to change the post entirely for a new problem...
I'm trying to handle signals better, as i need to force mencoder to exit cleanly before my script closes, so i have: Code:
$SIG{'INT'} = sub { |
First, the exit is unnecessary with die, because the die will terminate the script.
I don't know much about signal handling, so I would personally try to find a way to do so without using them. (= How is mencoder started? |
well i open it in an open command and pipe the output through a function. there's no other way to stop the program, but i need it to exit correctly as i need to ensure it builds the AVI index (mencoder is a dvd encoding program ICYDK), if it stops instantly, the files are pretty useless.
i had die and exit as they normally seem to be there in peoples examples. maybe exit is just a backup incase the die fails? that's guessing it's even possible for a die to fail... the source is still at http://thirtythreeandathird.net/mrip/mrip but i guess i just leave it the way it is, without the die or anything... |
I'm assuming that the section in question is the:
Code:
my $cmd = "mencoder -ovc lavc -lavcopts $LAVC -oac mp3lame" . And pretty much, you just need to loop/wait until it is finished, right? So, when: Code:
while (<ARSE>) Code:
while (<ARSE>) I have a feeling I'm missing something.... (= |
well no, it's a case of when the perl script is abnormally terminated, it instantly whips the carpet out from underneath mencoder, and doesn't let it tie up it's ends properly, which is essential, so i need to catch the CtrlC signal, and pass it through to mencoder properly. Anyway, that seems to be working, if a little odd. now i kill mencoder OR die depencding on wether mencpid has a value, ok for now i guess...
Brings me to yet another issue. I feel dumb asking these questions, but i guess the questions are reasonable! so... how do i know if an open command acutally worked? my line Code:
open (TITLE_INFO, "title_info $main::DVD_DEVICE 2> /dev/null | ") or die "error running title_info! is it on your path?\n"; |
Quote:
Code:
my($stopped) = "no"; Quote:
Quote:
Code:
open (TITLE_INFO, "title_info $main::DVD_DEVICE 2> /dev/null | ") or |
well if i make the command deliberately wrong, e.g. try to run title_infoI_REALLY_LIKE_DOUGHNUTS instead, nothing different happens, with || OR or...
oh yeah, sorry about my variable names... i still think it's better that crappy foobar *shudder, i just use my favourite words. as for your suggestion for the while condition, well that will exit that section, however the prblem lies in sending a signal to the child process itself, not handling the output. i'm happy enough with what i've got for now though... thanks |
AHHHHHHHH yes... the stderr redirection blocks the signals!! works fine without it....
|
but now i have stderr popping out where i don't want it... bum
ohh fixed that to. nothign to reply to! |
All times are GMT -5. The time now is 01:54 PM. |