Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

calling function with function

by Arbariya (Initiate)
on May 12, 2005 at 08:33 UTC ( #456291=perlquestion: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re: calling function with function
by rev_1318 (Chaplain) on May 12, 2005 at 10:12 UTC
    ... I have written a program...

    No you haven't. Because if you did, you would know how to write an new subroutine which does what you want.

    Paul

Re: calling function with function
by thcsoft (Monk) on May 12, 2005 at 09:38 UTC
    could you please specify your problem? nobody here will really want to study all your code for finding out, what your question is.

    thx :)<br<
    language is a virus from outer space.
Re: calling function with function
by robartes (Priest) on May 12, 2005 at 10:11 UTC
    <naivety>

    Are you sure you are asking for the correct thing? Your code already uses 'functions within functions': logMessage is called from within both Active_err and set_all_faults. Just put the bit of code you want in a separate sub and call it just like the logMessage calls.

    </naivety>
    Update: corrected spelling of that difficult word. Sarcasm is much better without spelling errors.

    CU
    Robartes-

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-09-25 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?