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

OK, I am still kind of new to all of this, and I have looked in a book that I have to try to help me, but my code for a program is getting to be enormous. I would like to shrink it by creating subroutines. I would also like to know how to use them and how to give the subroutine variables to use and have them return others. Here is an example of code that I would like to turn into a subroutine.
{ $xachange = ($blocks[$blockcount][$icount][1] - $datapoint[($blocks[$b +lockcount][$icount][5])][1]); $xbchange = (($datapoint[($blocks[$blockcount][$icount][5] +)][1]) - ($blocks[$blockcount][($icount + 1)][1])); $yachange = ($blocks[$blockcount][$icount][2] - $datapoint +[($blocks[$blockcount][$icount][5])][2]); $ybchange = (($datapoint[($blocks[$blockcount][$icount][5] +)][2]) - ($blocks[$blockcount][($icount + 1)][2])); $da = sqrt(($xachange*$xachange) + ($yachange*$yachange)); $db = sqrt(($xbchange*$xbchange) + ($ybchange*$ybchange)); $nxa = (($blocks[$blockcount][$icount][1] + ($xachange/$da +)*$radius)); $nya = (($blocks[$blockcount][$icount][2] + ($yachange/$da +)*$radius)); $nxb = (($blocks[$blockcount][$icount][1] + ($xbchange/$db +)*$radius)); $nyb = (($blocks[$blockcount][$icount][2] + ($ybchange/$db +)*$radius)); $gxa1 = $nxa / 25.4; $gya1 = $nya / 25.4; $gxb1 = $nxb / 25.4; $gyb1 = $nyb / 25.4; $gpx = $datapoint[($blocks[$blockcount][$icount][5])][1] / + 25.4; $gpy = $datapoint[($blocks[$blockcount][$icount][5])][2] / + 25.4; select JOB; printf "G01X%.3fY%.3f\n", $gxa1, $gya1; printf "G03X%.3fY%.3fI%.3fJ%.3f\n", $gxb1, $gyb1, $gpx, $g +py; select STDOUT; print " 1 [4] G033333333333333333 $blockcount, $icount, 1 +\n"; $blocks[$blockcount][$icount][3] = 1; $icount = $icount + 1; }
Any help would be greatly appreciated, even a small subroutine, where I have to give it global variables, it uses them by naming them different local variables and does calculations, then gives return variables. Thanks

Replies are listed 'Best First'.
Re: subroutine help
by dragonchild (Archbishop) on Feb 12, 2003 at 21:33 UTC
    Start by making small test programs. Read the Llama book. Don't start with something too big. If you have specific questions, bring the thing you're having a problem with to us and we'll be delighted to help answer your specific question. This is more of something where we'd be posting a tutorial that's better written up in the Llama book. merlyn, contrary to popular belief, does know what he's talking about. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: subroutine help
by tall_man (Parson) on Feb 13, 2003 at 01:49 UTC
    Yes, you could use some subroutines. I always look for a way to wrap things into objects when indexing gets this messy. However, you don't have to use objects in this case. Here is a way to simplify life with some variables protected in a scope.