ramki067 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've a command which installs android apks one after the other. These apks names are stored in a file. I'm accessing the text file and issuing the below commmand to install apks.
$t = new Net::Telnet (Timeout => undef); $username="sqa"; $password="Tmie"; $t->open("xxxx.xx.com"); $t->login($username, $password); $req_apks = "re2.txt"; open(F2, "<", "$req_apks") or die "Cannot open APK Filelist file:$ +!"; my $req; while ($req = <F2>) { chomp; @b= $t->cmd("./adb install $req"); print @b; sleep(3); print "\n"; }
But the command "./adb install " is failing after 1 or 2 apks are installed. I've around 10 apks in the text file to be installed. Is there any way so that even if ./adb install commmand fails the script should continue without dying? Thanks, Sharath
  • Comment on Perl script comes out because of a command failure. How to overcome it?
  • Download Code

Replies are listed 'Best First'.
Re: Perl script comes out because of a command failure. How to overcome it?
by kcott (Archbishop) on Nov 05, 2013 at 08:06 UTC

    G'day Sharath,

    I'd recommend starting by cleaning up your code. I don't know if you've shown everything here: there's a few things that could potentially cause problems that can easily be avoided. Here's some suggestions:

    • Use the strict and warnings pragmata. Initially, this will generate quite a few errors: most, if not all, of these can be fixed by simply declaring your variables with my.
    • Avoid Indirect Object Syntax: see perlobj: Invoking Class Methods for a strongly worded explanation of why this syntax is discouraged.
    • Use lexical filehandles: see open for a discussion of this.

    Your current code:

    my $req; while ($req = ...

    would probably be better as:

    while (my $req = ...

    And I suspect:

    chomp;

    is supposed to be:

    chomp $req;

    Once you've done that, it's possible your current problems may be resolved (I'm unfamiliar with this adb program you mention, so I don't really know). However, assuming that's not the case, look at eval for a way to trap errors: you'll want the block form of eval which you'd code something like this:

    eval { # Code which might generate an error } if ($@) { # Handles any errors here # $@ holds the error message }

    -- Ken

      Thanks kcott and Ken. The problem resolved once i added $t->errmode("return"); suggested by Kcott before while statement. I have even added chomp $req what Ken suggested. Thanks again for the solving my 1 week headache. Cheers, Sharath

        You seem a little confused over who's who and who posted what.

        kcott and Ken are one and the same (i.e. me); just as ramki067 and Sharath are one and the same (i.e. you).

        It was Athanasius who suggested $t->errmode("return");. He won't be notified of your reply to me; you may want to thank him separately.

        Anyway, glad you've resolved your problem, regardless of who's advice was involved. :-)

        -- Ken

Re: Perl script comes out because of a command failure. How to overcome it?
by Athanasius (Cardinal) on Nov 05, 2013 at 07:59 UTC

    From the Net::Telnet documentation:

    Errors such as timing-out are handled according to the error mode action. The default action is to print an error message to standard error and have the program die.

    Try adding the line:

    $t->errmode("return");

    before the while loop. This should prevent the script from dying. See the entry for “errmode” in the “METHODS” section of Net::Telnet. (This also shows how to retrieve the error message when a command fails.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,