Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

eval a command

by Xantara (Initiate)
on Feb 21, 2013 at 14:01 UTC ( #1019950=perlquestion: print w/ replies, xml ) Need Help??
Xantara has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I was wondering if someone could lend me a solution. :(

I have a list of commands (user defined perl module subroutine) in a text file, that my script reads in one at a time, and then I try to execute as below.

eval $cmd

Each command will wait for response for 90sec to go for next command.

Here one of my command should not wait for the response to go further commands.

I tried as below but failed by getting a popup menu:

"Perl Command Line Interpreter has encountered a problem and needs to +close"
Also I got error in the Perl cmd prompt:Attempt to free unreferenced scalar

foreach (@commands) { $cmd = $_; if($cmd =~ /PLA_ENABLE/i){ $pid = fork(); if (defined $pid && $pid == 0) { # child eval $cmd; } exit 0; } else{ eval $cmd; } }
$cmd will be holding commands like MCI->Do("SETP NAS_R99_QOS 0");
sub Do { my ($self, $cmd, $time_out) = @_; my ($TimeOut, @lines, $StopTime); my ($prematch, $result, $retVal); my ($success, @reply); my $nice = 0; my $linecount = 0; tie $retVal, 'ReturnValue'; $retVal = PASS; # Empty the input buffer $self->{_tnet}->buffer_empty; # Clear the last error message $self->{_tnet}->errmsg(@reply); #cut newline if there is one chomp $cmd; print $gLog Text => "$cmd\n", Name => $self->{_name}; if ($cmd =~ /Wait/) { # MDL 'Wait' command can be used in scripts, but is not recogn +ised by MCI... # Engine provides a 'Wait' function, but sometimes 'Wait' is i +n a TM script and does not get # sent to the Engine 'Wait' function because it doesn't end in + a semicolon. $cmd =~ s/^\s+//; my $waittime = ${[split /\s+/, $cmd]}[1]; return $gEngine->Wait($waittime); } #If time out is defined this sets the time out. if (defined($time_out)) { if ($time_out =~/^[0-9]+$/) { $TimeOut = $time_out; } else { print $gLog Text => "Invalid time out provided using 90sec +s.\n", Color => "blue", Name => $self->{_name}; $TimeOut = 90; } } else { $TimeOut = 90; } $_ = $cmd; # SCFG timeout is 15 minutes. /SCFG/i && do { $TimeOut = 900; $nice = 1; }; # initcellsearch timeout is now 60 seconds. /initcellsearch/i && do { $TimeOut = 60; }; # Send command. $self->{_tnet}->timeout(1); $success = $self->{_tnet}->print( $cmd ); if (!$success) { print $gLog Text => "Sending the command $cmd to the mobile f +ailed\n", Color => "red", Name => $self->{_name}; return $self->_checkfailures($cmd); } $StopTime = time + $TimeOut; # Wait for reply - matches the presence of a 'C:' $result = ""; WAIT: { do { undef @lines; _sleep_for(0.05) if ($nice); @lines = $self->{_tnet}->getlines(TimeOut => 0); foreach $prematch (@lines) { push @reply, $prematch; $result .= $prematch; } if (time > $StopTime) { print $gLog Text => "Confirmation C: not received fro +m mobile in $TimeOut seconds. Checking for errors.\n", Color => 'red' +, Name => $self->{_name}; $retVal = $self->_checkfailures($cmd); last WAIT; } } until (($result =~ /C:\s*(\w+)\s*0x\d\d/i) || ($result =~ /O +K/i)); } # Count the number of lines received in the WAIT loop. $linecount = @reply; if ($linecount > 0) { # Print the line. Scan it for errors and set the state accordi +ngly. $retVal = $self->_printReturnedLines($result); } else { # Fatal error as no lines were received. $retVal = FATAL; } return $retVal if (($retVal == FATAL) || ($retVal == CRASH)); $retVal = $self->Pass_Conf_Ind(@lines, $cmd); return wantarray ? ($retVal, $result) : $retVal; }

Comment on eval a command
Select or Download Code
Re: eval a command
by tmharish (Friar) on Feb 21, 2013 at 14:13 UTC

    The following code exists and the fork prints "Hello World" after the required 5 sec.

    use strict ; use warnings ; my $cmd = 'sleep 5; print "Hello World\n";' ; my $pid = fork(); if (defined $pid && $pid == 0) { # child eval $cmd; } exit 0;

    OS, Perl version and details on the command you are trying to execute might help

      All the commands that I run are subroutines of perl modules only. Execution of the $cmd, calls userdefined perl module API. In my case $mci->Do(); OS - Windows XP Perl Version 5.6

        If you can update from 5.6 its probably a good idea.

        On the other hand did you try to directly call that function and see if that works. Also why not just call these functions instead of using eval?

        5.6 is over ten years old, seriously consider upgrading if at all posible.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019950]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-09-19 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (131 votes), past polls