Making perl 2.0 core dump

J Greely jgreely at banjo.cis.ohio-state.edu
Mon Aug 15 06:09:04 AEST 1988


In article <6670 at bloom-beacon.MIT.EDU> tytso at 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, at bar,$baz)", that "local($var1, at 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, at 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, at 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 at cis.ohio-state.edu, 
	  {att,pyramid,killer}!cis.ohio-state.edu!jgreely



More information about the Comp.sources.bugs mailing list