# Perl script error

**URL:** <https://community.unix.com/t/perl-script-error/216390>\
**Category:** Shell Programming and Scripting\
**Created:** [October 14, 2008, 7:48am UTC](https://community.unix.com/t/perl-script-error/216390 "2008-10-14T07:48:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pietie](https://community.unix.com/letter_avatar/pietie/32/6_449c533f586939b6e5f246632a46421f.png) [@pietie](https://community.unix.com/u/pietie)\
**Post date:** [October 14, 2008, 7:48am UTC](https://community.unix.com/t/perl-script-error/216390/1 "2008-10-14T07:48:34Z")

</div>

Hi all

I keep getting a segmentation fault error while running the script below.

```nohighlight
#!/usr/bin/perl -w
use CGI ':standard';
use GD::Graph::pie;
use strict;
use warnings;

sub trim($)
{
        my $string = shift;
        $string =~ s/^\s+//;
        $string =~ s/\s+$//;
        return $string;
}

my ($ts, $filename, @array, $line, $arri , $k , $total);

open(DOMAINS, "< /mailstats/temp/domains.tmp") || die "Couldn't open. Please check permissions. \n";

$k = 0;
$total = 0;

while ($line = <DOMAINS>) {
$line = trim($line);
#chomp $line; # removes the carriage return
#@array = split(/ / , $line); # breaks the line up into fields

($array[1][$k], $array[0][$k], undef) = split(" ", $line);

$total = $total + $array[1][$k];

#print $array[0][$k] , "\
print $total , "\n";

my $mygraph = GD::Graph::pie->new(300, 300);
$mygraph->set(
    title => 'Grading Policy for COP5555 course',
    '3d' => 0,
) or warn $mygraph->error;

$mygraph->set_value_font(GD::gdMediumBoldFont);
my $myimage = $mygraph->plot(\@array) or die $mygraph->error;

#print "Content-type: image/png\n\n";
open(PICTURE, ">filesplittest.png") or die("Cannot open file for writing");
binmode PICTURE;
print PICTURE $myimage->png;
close PICTURE;
t" , $array[1][$k] , "\n";

## NOW PROCESS EACH LINE HERE

$k = $k + 1;
}
close DOMAINS;

```

now when I comment out the whole graph drawing block, I don't get a segmentation fault.

The data I am using is a lot of data. Can that be the issue?

If the data is the problem, is there any way I can optimize it?

I have tried inputting less data intro the text file, and it worked.

Regards

---

<div class="post-metadata">

**Author:** ![KevinADC](https://community.unix.com/letter_avatar/kevinadc/32/6_449c533f586939b6e5f246632a46421f.png) [@KevinADC](https://community.unix.com/u/KevinADC)\
**Post date:** [October 14, 2008, 4:11pm UTC](https://community.unix.com/t/perl-script-error/216390/2 "2008-10-14T16:11:54Z")

</div>

What is this line doing?

t" , $array[1][$k] , "\n";

---

<div class="post-metadata">

**Author:** ![pietie](https://community.unix.com/letter_avatar/pietie/32/6_449c533f586939b6e5f246632a46421f.png) [@pietie](https://community.unix.com/u/pietie)\
**Post date:** [October 15, 2008, 2:09am UTC](https://community.unix.com/t/perl-script-error/216390/3 "2008-10-15T02:09:33Z")

</div>

Sorry about that, I didnt even notice that the forum (or my copy-paste) chopped of the leading part of that line

That line is part of a line higher up

The code should look like below

```nohighlight
#print $array[0][$k] , "\t" , $array[1][$k] , "\n";

```

That was part of the output to see if the matrix gets created correctly.

Regards

---

<div class="post-metadata">

**Author:** ![KevinADC](https://community.unix.com/letter_avatar/kevinadc/32/6_449c533f586939b6e5f246632a46421f.png) [@KevinADC](https://community.unix.com/u/KevinADC)\
**Post date:** [October 15, 2008, 2:35am UTC](https://community.unix.com/t/perl-script-error/216390/4 "2008-10-15T02:35:08Z")

</div>

From what I know a segmentation fault is an error caused by the operating system or a program that tries to read/write to memory that it's not supposed to. It is possible that the perl program is using too much memory and causing the error. My guess would be that the array you are building is growing too big.

---

<div class="post-metadata">

**Author:** ![pietie](https://community.unix.com/letter_avatar/pietie/32/6_449c533f586939b6e5f246632a46421f.png) [@pietie](https://community.unix.com/u/pietie)\
**Post date:** [October 15, 2008, 9:05am UTC](https://community.unix.com/t/perl-script-error/216390/5 "2008-10-15T09:05:19Z")

</div>

That was my first guess as well. So I started playing around.

After I commented out the graph drawing, the segmentation fault went away.

That was probably because after I commented it out, it didn't need to read the array again

I am not sure about that though

Regards
