Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi, I need your help in doing this. I have written a program
use KB::2mmint; use Time::HiRes; my $nodeno = 28; my @all_errorlist = (112); my @resultlist; my @result; open( LOGFILEHANDLE, ">testlog.txt" ) or die $!; my $overAllResult=0; $result = &Active_err( $nodeno, \@all_errorlist); $overAllResult=1 unless ($result eq "PASSED"); $result = &set_all_faults( $nodeno, \@all_errorlist); $overAllResult=1 unless ($result eq "PASSED"); sub Active_err { my ( $nodeno, $refErrlst ) = @_; my ( @dataIn, @dataOut ); @dataIn = ( 0x65, 0, 3, 0 ); # commands for activating error my $result = "PASSED"; foreach $var (@$refErrlst) { $dataIn[20] = $var & 0xFF; # give the fault no $dataIn[21] = $var >> 8; print ("\ntest :", $dataIn[20], "-", $dataIn[21], "\n"); undef @dataOut; # clear output buffer $retVal = &KB::2mmint::Request( $nodeno, 128, 1, \@dataIn, \@dataOut + ); if($retVal != 0) { logMessage($retVal, "Error caused by 2mmint", sprintf ("2mmint returned %d", $retVal)); return "ERROR"; } unless($dataOut[4] == ord('P')) # char nr. 4 must P for Passed { my $failMessage = sprintf ("activating ErrNr. %d failed\n" +, $var); $failMessage = sprintf ("%s\n", join (" ", @dataOut)); logMessage(1, "Active_err Failed", $failMessage); $result = "FAILED"; } } print $result; if($result eq "PASSED") { logMessage(0, "Active_err Passed", ""); return "PASSED"; } else { logMessage(1, "Active_err ", $result); return $result; } } ## Active_err sub set_all_faults { my ( $nodeno, $refErrlst, $refResList ) = @_; my ( @dataIn, @dataOut ); @dataIn = ( 0x67, 0, 3, 0 ); # commands for set_all_faults my $result = "PASSED"; foreach $var (@$refErrlst) { $dataIn[20] = $var & 0xFF;# give the fault no $dataIn[21] = $var >> 8; print ("\ntest :", $dataIn[20], "-", $dataIn[21], "\n"); undef @dataOut; # clear output buffer $retVal = &KB::2mmint::Request( $nodeno, 128, 1, \@dataIn, \@dataOut + ); if($retVal != 0) { logMessage($retVal, "Error caused by 2mmint", sprintf ("2mmint returned %d", $retVal)); return "ERROR"; } unless($dataOut[4] == ord('P')) # char nr. 4 must P for Passe +d { my $failMessage = sprintf ("set all faults. %d failed\n", +$var); $failMessage = sprintf ("%s\n", join (" ", @dataOut)); logMessage(1, "set_all_faults Failed", $failMessage); $result = "FAILED"; } } print $result; if($result eq "PASSED") { logMessage(0, "set_all_faults Passed", ""); return "PASSED"; } else { logMessage(1, "set_all_faults ", $result); return $result; } } sub logMessage { my ( $Failed, $LogMessage, $ErrMessage ) = @_; printf LOGFILEHANDLE ("--------------------------\n"); # get time and print it my ( $secs, $usecs ) = Time::HiRes::gettimeofday(); my @ltime = localtime($secs); my ( $dyear, $dmonth, $ddate, $dhour, $dmin, $dsecs ) = ( $ltime[5] + 1900, $ltime[4] + 1, $ltime[3], $ltime[2], $ltime[1], $ltime[0] ); my $TimeString = sprintf( "%04d-%02d-%02d %02d:%02d:%02d.%06d", $dyear, $dmonth, $ddate, $dhour, $dmin, $dsecs, $usecs ); printf LOGFILEHANDLE ( "Timestamp %s\n", $TimeString ); printf LOGFILEHANDLE ( "%s \n", $LogMessage ); if ($Failed) { printf LOGFILEHANDLE ("ErrorMessage:\n"); printf LOGFILEHANDLE ( "%s\n", $ErrMessage ); printf LOGFILEHANDLE ( "----------%s----------\n\n", "FAILED" +); } else { printf LOGFILEHANDLE ( "----------%s----------\n\n", "PASSED" +); } } }
so now i want the code starting from

foreach $var (@$refErrlst)in sub Active_err and ending at  return "ERROR";<p> to be written as a seperate function and called in sub Active_err. as the same code is used in set_all_faults function.


In reply to calling function with function by Arbariya

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found