Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!banjo.cis.ohio-state.edu!jgreely From: jgreely@banjo.cis.ohio-state.edu (J Greely) Newsgroups: comp.sources.bugs Subject: Re: Making perl 2.0 core dump Message-ID: <19941@tut.cis.ohio-state.edu> Date: 14 Aug 88 20:09:04 GMT References: <6670@bloom-beacon.MIT.EDU> Sender: news@tut.cis.ohio-state.edu Organization: THE Ohio State University, CIS Dept. Lines: 81 In article <6670@bloom-beacon.MIT.EDU> tytso@athena.mit.edu writes: >[original code deleted] The first problem is that you misunderstood how parameters are passed to a subroutine: "Any arguments passed to the routine come in as array @_, ...". You assumed that when you say "do some_sub($foo,@bar,$baz)", that "local($var1,@var2,$var3) = @_;" would get back what you put in. What really happens is that the subroutine receives "@_ = ($foo,$bar[0] ... $bar[$#bar],$baz)." Perl doesn't understand what you were trying to do (but I think it *should* have been an error rather than a core dump). The correct way to pass arrays is something like this: "do some_sub($foo,$#bar,@bar,$baz,'string')", and parse it like this: $local($var1,$v2count,$var3,$var4); #note: local arrays don't work $var1 = shift(@_); $v2count = shift(@_) + 1; while ($v2count--) { push(@var2,shift(@_)); } ($var3,$var4) = @_; The other problems were mostly simple bugs that you would have caught fairly quickly. Below is a functioning version of your program (with a few little extras tossed in): -------------------- cut here -------------------- #!/usr/local/bin/perl @foo = (4,6,17,13); do Bargraph("foo", $#foo,@foo, 0, $#foo, 1, 0, do max(@foo), 1); #bargraph: # print a primitive bargraph #usage: # do Bargraph($title, $count, $array[0] ... $array[$count], $x_min, # $x_max, $x_step, $y_min, $y_max, $y_step) sub Bargraph { @printchars = ("#","%","*","$"); local ($title, $count, $x_min, $x_max, $x_step, $x, $y, $y_min, $y_max, $y_step); $title = shift(@_); $count = shift(@_) + 1; while ($count--) { push(@array,shift(@_)); } ($x_min,$x_max,$x_step,$y_min,$y_max,$y_step) = @_; for ($y=$y_max; $y >= $y_min; $y -= $y_step) { for ($x=$x_min; $x <= $x_max; $x += $x_step) { if ($array[$x] < $y) { print " "; } else { print " ",$printchars[$x]; } } printf(" %2d\n", $y); } } #max: # return largest numeric value in passed array # sub max { @_ = sort comp @_; return($_[$#_]); } #comp: # numeric comparison routine for sort # sub comp { $a < $b ? -1 : $a > $b ? 1 : 0; } -------------------- cut here -------------------- -=- J Greely "...but would it be kidnapping, or grand theft auto?" jgreely@cis.ohio-state.edu, {att,pyramid,killer}!cis.ohio-state.edu!jgreely