in reply to Re[3]: Modifying STDOUT and keeping control
in thread Modifying STDOUT and keeping control

Check it:
#!/usr/bin/perl -w use strict; # Example script, simmilar to the one I'm writing.. menu(); sub menu { my @menu = ( [ 1 => 'Go' ], ); my %call = ( 1 => \&go, ); system("clear"); print " ", "$_->[0]. $_->[1]\n" for @menu; print "Enter Selection:"; chomp( my $answ = <STDIN> ); my $sub = $call{$answ} or err(); $sub->(); } sub go { # This is where the telnet call gets made. system("telnet", "192.168.1.1"); # Currently spawns shell command # my $var = `telnet 192.168.1.1' # This will save output to $var # So I can edit the output. system("clear"); menu(); } sub err { print "Valid Selection Not Entered!\n"; menu(); }
I want the command to run with my modified output so the user can't see that it is telnetting to 192.168.1.1.

The output can be modified easily enough using s/192.16.1.1//, But I can't seem to figure out how to make the user see the output I desire, while it's executing the telnet command.

Replies are listed 'Best First'.
Re: Re: Re[3]: Modifying STDOUT and keeping control
by fruiture (Curate) on Oct 14, 2002 at 21:54 UTC

    Well, the relevant code is:

    system('telnet' , '192.168.1.1');

    As `perldoc -f system` tells you, system() forks off a child, waits for it to exit and then returns to your program. `perldoc perlop` tells us that qx// does the same, but catches the stdout of the command and returns it (exit status should be in $? anyways). So I can't see why you cannot use qx//...

    my $var = `telnet 192.168.1.1`; $var =~ s/192.168.1.1//; print $var;

    update: oops, code tag typo, thanks Aristotle.

    --
    http://fruiture.de