Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

wierd output

by oldirtysingh (Novice)
on Dec 17, 2012 at 12:41 UTC ( #1009158=perlquestion: print w/replies, xml ) Need Help??
oldirtysingh has asked for the wisdom of the Perl Monks concerning the following question:

Hi ppl,

Got a really weird problem, which comes and goes but is upsetting my perl output.

Code snippet
sub cheat { my $cheatfile = "/home/cheatsheets/$prefix.txt"; open( INFO, $cheatfile ); @lines = <INFO>; close(INFO); foreach (@lines){ print $_; } }
##above stores and prints file contents to screen
my $counter = 0; while($counter <= 1){ $session->exp->interact(\*STDIN,"\cA"); my $exit = qx(cat $showlog); if ($exit =~ /exit|quit|logout|Inactivity/){ print colored("logfile:$showlog\n",'yellow'); die; ++$counter; } &cheat; $session->exp->send("\r\n"); }

#the above hands session to user..... and loops until it finds an exit|logout or quit in the logfile then quits
#in the meantime whilst in interact mode the user can press cntrl + a and it prints the files contents by calling the subroutine.

This all works fine... however the output of the file changes

correct output

networks> adslc -s -s 1________________________line status [card mode] adslc -s -e 1________________________line errors [card mode] networks>

incorrect output

networks> adslc -s -s 1________________________line status [card mode] adslc -s - +e 1________________________line errors [card mode] networks>

i can press cntrl + a any number of times and randomly the output will distort from the normal format like above.

Anyone know a possible cause? Tried all kinds of workarounds and come to the conclusion that anytime a "\n" is used in the loop, this distortion occurs every so often. The next line in the file prints at the end of the previous line but on a newline, if that makes sense :s

i.e. line1 line2 line3

Would be very greatful for the help

Replies are listed 'Best First'.
Re: wierd output
by oldirtysingh (Novice) on Dec 17, 2012 at 13:43 UTC

    Thank you for providing me with better and more efficient methods on file side, i see what you've done and i didn't even think of passing arguments to the subrouting instead. And the links, will give them a read.

    But on the looping of the ssh interact is where im stuck

    The script automatically logs into the device and hands the ssh over to the user with the interact method. They then can press its escape seq of cntrl + A which quits the interact and prints a statement. The user is then put back into the interact because of the loop. They only exit this loop when the user enters logout|quit|exit.

    If i were to simply use a print statement like...
    while($session){ $session->exp->interact(\*STDIN,"\cA"); ##save logfile to var my $exit = qx(cat $showlog); ##check logfile for exit strings if ($exit =~ /exit|quit|logout|Inactivity/){ print colored("logfile:$showlog\n",'yellow'); #kill session die; } print "\njohn\nsmith"; }
    in the while loop i would still get output like..
    networks> john smith networks> john smith networks>

    Thought it could be buffering and tried flushing them, but no use

Re: wierd output
by Anonymous Monk on Dec 17, 2012 at 12:53 UTC

      FWIW, a smarter way would be

      cheat( "/home/cheatsheets/$prefix.txt" , \*STDOUT ); sub cheat { my( $cheatfile, $outhandle ) = @_; open my($info), '<', $cheatfile or die "Can't open( $cheatfile ): +$!"; while(<$info>){ print $outhandle $_; } close $info; }

      But if you're going to write that you might as well use File::Copy::copy :)

      use File::Copy qw' copy '; copy( "/home/cheatsheets/$prefix.txt" , \*STDOUT );

      This assuming you're only copy-ing verbatim

Re: wierd output
by oldirtysingh (Novice) on Dec 17, 2012 at 14:15 UTC

    Thanks for the help ppl but i've managed to fix the distortion in output

    foreach (@lines){ chomp($_); print "$_\r\n"; }

    using a \r carriage return fixes the problem.

      It appears to me that you have fixed the symptom, not the problem. That is sometime good enough (or the best that we can do), but be aware of the difference.
Re: wierd output
by aitap (Curate) on Dec 17, 2012 at 16:24 UTC
    This looks like broken terminal settings, options [-]icrnl, [-]igncr [-]inlcr [-]ocrnl, [-]onlcr, [-]onlret or [-]onocr (I'm not sure which one).
    Sorry if my advice was wrong.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009158]
Approved by marto
Front-paged by Tommy
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-02-17 19:40 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (249 votes). Check out past polls.