Shlomi/Uri,
what do you mean by 'It's a good idea not to use "select" on filehandles like 
that because it willaffect the default filehandle permanently.'?

I am using perl 5.14
ty.



________________________________
 From: Shlomi Fish <shlo...@shlomifish.org>
To: perl list <beginners@perl.org> 
Sent: Friday, June 28, 2013 4:48 AM
Subject: Re: printing content of a pipe
 

Hi Rajeev,

see below for some comments on your code.

On Thu, 27 Jun 2013 15:07:50 -0700 (PDT)
Rajeev Prasad <rp.ne...@yahoo.com> wrote:

> in the below code I am not able to print anything except whatever is in the
> $pty.  I want to print LINE_START: in the beginning of each line of the
> output, but it does not print that, it only prints what the output of the
> exceuted command produced. why is that so?
> 
>     while(<$pty>) {
> 
>          print "LINE_START: $. $_";
>     }
> 
> 
> 
> I am suspecting it has something to do with select STDOUT; $| = 1;, but i
> dont know what.
> 
> complete code:
> 
> #!/usr/bin/perl
> use strict;
> use warnings;

The strict and warnings are a good idea.

> use Net::OpenSSH;
> use Expect;
> 
> select STDOUT; $| = 1;
> select STDERR; $| = 1;

It's a good idea not to use "select" on filehandles like that because it will
affect the default filehandle permanently. Instead do:

<CODE>
use IO::Handle;

STDOUT->autoflush(1);
STDERR->autoflush(1);
</CODE>

> 
> my $password = $ARGV[0];
> my $target = $ARGV[1];
> my $runas = $ARGV[2]; 
> my $cmd = $ARGV[3];

This uses positional indexes and is subject to error. See:

http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments

(my own link).

> my $timeout = 20;
> my $debug = 0;
> 
> my $ssh = Net::OpenSSH->new($target,
>                         user => 'abcd',
>                         password => $password,
>                         master_opts => [-o => 'StrictHostKeyChecking=no',
>                                              -o => 'ConnectTimeout 60']
>                         );

The formatting here is inconsistent.

> my $sshExitCode = $ssh->error;
> if ($sshExitCode eq "0") {
>     my $ostype= $ssh->capture("uname -s");chomp($ostype);
>     my $sudo_path=$ssh->capture("which sudo");chomp($sudo_path);
>     my $su_path=$ssh->capture("which su"); chomp($su_path);

1. You've placed two statements on the same line three times.

2. You may wish to extract a subroutine or a closure for doing that:

<CODE>
my $read_str = sub {
    my ($cmd) = @_;
    my $ret = $ssh->capture($cmd);
    chomp($ret);
    return $ret;
};
</CODE>

Also see what Uri said.

Regards,

    Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish      http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

Well, one thing I can tell you about parenthood is that such things
can progress from figurative to literal, extremely quickly.
    — http://www.shlomifish.org/humour/Summerschool-at-the-NSA/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/

Reply via email to