Sponsored Content
Full Discussion: Perl script error
Top Forums Shell Programming and Scripting Perl script error Post 302246718 by pietie on Tuesday 14th of October 2008 07:48:34 AM
Old 10-14-2008
Perl script error

Hi all

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

Code:
#!/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

Last edited by otheus; 10-14-2008 at 12:54 PM.. Reason: I think down-thumb is not going to help ge this answered.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Perl Error with ftptail script

Hello everyone, I'm a starting website admin who 2 days ago decided to make the move from shared hosting to a vps on a linux (ubuntu 8.04) platform. This opened a whole new world for me, since I never worked with the console, SSH, linux,... before. I managed to get LAMP setup after a day of... (2 Replies)
Discussion started by: Cooki3s!
2 Replies

2. Shell Programming and Scripting

error in output of perl script

Hi, This is my piece of code. my $logFile = $ARGV; die "usage: $0 <logFile>" unless $logFile; die "Logfile $logFile doesn't exist" unless -f "$logFile"; open(my $log, "<", $logFile) or die "Can't open $logFile for reading."; print "Processing file $logFile...\n"; #my $authenticates... (2 Replies)
Discussion started by: namishtiwari
2 Replies

3. Shell Programming and Scripting

Need help understanding perl script error

I solicited this site earlier this week and got a good answer for a perl Script so I made this script from what understood from the answers But now I have a bug and I'm stump. It doesn't parse correctly the Output it stays on the first line My $f2 and reprints in a endless loop I'm sure there... (3 Replies)
Discussion started by: Ex-Capsa
3 Replies

4. Shell Programming and Scripting

Perl script to rename file,error

#!/usr/bin/perl $FL="ch.txt"; $CFL="mytext.txt"; print "This script will rename textfiles."; mv $FL $CFL; print "Done."; perl file.pl Error: Can't locate object method "mv" via package "ch.txt" (perhaps you forgot to load "ch.txt"?) at file.pl line 7. (5 Replies)
Discussion started by: cola
5 Replies

5. Shell Programming and Scripting

perl script processing error

open(IN,"input_file") or die "Can't open Input file.\n"; while (<IN>) { chomp; $line = $_; if($line != '') { print "\nprocessing $line\n"; $size = 0; $hrid = $line; @project_id_array = null; $size = @project_id_array;... (3 Replies)
Discussion started by: vishwakar
3 Replies

6. Shell Programming and Scripting

Perl : Error in calling script

I am getting a strange error with perl's inbuilt script flush.pl. I am callling this script in my other script but it kept throwing error: flush.pl did not return a true value at ./abc.pl line 1 abc.pl has: require 'flush.pl'; Not sure why this error is coming. Can someone pls throw... (4 Replies)
Discussion started by: abhisharma23
4 Replies

7. Shell Programming and Scripting

Error in executing Perl script

Hello All I am facing an issue The unix script is running fine in unix environment which uses ssh connection but when I try to run the same in informatica environment (same server where I was running the unix script manually successfully) its showing the below error command-line line 0:... (11 Replies)
Discussion started by: Pratik4891
11 Replies

8. Programming

getting error in my perl script

hi Here is my code written to identify the particular position which is after a string (chr*). my input file looks some thing like this aaanbb:anhn:iuopl:12345 chr1 12345 asnmkol * # kjiiii.....anmkij:lpolk:lopll:abnnj chr5 123222 polko * dddfgg .... aaanbb:anhn:iuopl:aanjuj chr2 44345 asnmkol... (1 Reply)
Discussion started by: anurupa777
1 Replies

9. Homework & Coursework Questions

A2p, error in the perl script?

1. The problem statement, all variables and given/known data: filename = awkdata Part 1: Write an awk program that removes the first field and prints only those lines where the third field is greater than 15 million. Part 2: Do the same command above, but with perl instead of awk.... (0 Replies)
Discussion started by: AZbase10
0 Replies

10. Shell Programming and Scripting

Slight error with my perl script that I could use some help on

So I have a perl script that prompts the user to enter either q or Q to exit the program or c to continue said program. If the user inputs anything other than those three keys they will be prompted again and again for an appropriate input. My script works for the most part except for one small... (6 Replies)
Discussion started by: Eric1
6 Replies
CDB_File(3pm)						User Contributed Perl Documentation					     CDB_File(3pm)

NAME
CDB_File - Perl extension for access to cdb databases SYNOPSIS
use CDB_File; $c = tie %h, 'CDB_File', 'file.cdb' or die "tie failed: $! "; $fh = $c->handle; sysseek $fh, $c->datapos, 0 or die ...; sysread $fh, $x, $c->datalen; undef $c; untie %h; $t = new CDB_File ('t.cdb', "t.$$") or die ...; $t->insert('key', 'value'); $t->finish; CDB_File::create %t, $file, "$file.$$"; or use CDB_File 'create'; create %t, $file, "$file.$$"; DESCRIPTION
CDB_File is a module which provides a Perl interface to Dan Bernstein's cdb package: cdb is a fast, reliable, lightweight package for creating and reading constant databases. Reading from a cdb After the "tie" shown above, accesses to %h will refer to the cdb file "file.cdb", as described in "tie" in perlfunc. Low level access to the database is provided by the three methods "handle", "datapos", and "datalen". To use them, you must remember the "CDB_File" object returned by the "tie" call: $c in the example above. The "datapos" and "datalen" methods return the file offset position and length respectively of the most recently visited key (for example, via "exists"). Beware that if you create an extra reference to the "CDB_File" object (like $c in the example above) you must destroy it (with "undef") before calling "untie" on the hash. This ensures that the object's "DESTROY" method is called. Note that "perl -w" will check this for you; see perltie for further details. Creating a cdb A cdb file is created in three steps. First call "new CDB_File ($final, $tmp)", where $final is the name of the database to be created, and $tmp is the name of a temporary file which can be atomically renamed to $final. Secondly, call the "insert" method once for each (key, value) pair. Finally, call the "finish" method to complete the creation and renaming of the cdb file. Alternatively, call the "insert()" method with multiple key/value pairs. This can be significantly faster because there is less crossing over the bridge from perl to C code. One simple way to do this is to pass in an entire hash, as in: "$cdbmaker->insert(%hash);". A simpler interface to cdb file creation is provided by "CDB_File::create %t, $final, $tmp". This creates a cdb file named $final containing the contents of %t. As before, $tmp must name a temporary file which can be atomically renamed to $final. "CDB_File::create" may be imported. EXAMPLES
These are all complete programs. 1. Convert a Berkeley DB (B-tree) database to cdb format. use CDB_File; use DB_File; tie %h, DB_File, $ARGV[0], O_RDONLY, undef, $DB_BTREE or die "$0: can't tie to $ARGV[0]: $! "; CDB_File::create %h, $ARGV[1], "$ARGV[1].$$" or die "$0: can't create cdb: $! "; 2. Convert a flat file to cdb format. In this example, the flat file consists of one key per line, separated by a colon from the value. Blank lines and lines beginning with # are skipped. use CDB_File; $cdb = new CDB_File("data.cdb", "data.$$") or die "$0: new CDB_File failed: $! "; while (<>) { next if /^$/ or /^#/; chop; ($k, $v) = split /:/, $_, 2; if (defined $v) { $cdb->insert($k, $v); } else { warn "bogus line: $_ "; } } $cdb->finish or die "$0: CDB_File finish failed: $! "; 3. Perl version of cdbdump. use CDB_File; tie %data, 'CDB_File', $ARGV[0] or die "$0: can't tie to $ARGV[0]: $! "; while (($k, $v) = each %data) { print '+', length $k, ',', length $v, ":$k->$v "; } print " "; 4. For really enormous data values, you can use "handle", "datapos", and "datalen", in combination with "sysseek" and "sysread", to avoid reading the values into memory. Here is the script bun-x.pl, which can extract uncompressed files and directories from a bun file. use CDB_File; sub unnetstrings { my($netstrings) = @_; my @result; while ($netstrings =~ s/^([0-9]+)://) { push @result, substr($netstrings, 0, $1, ''); $netstrings =~ s/^,//; } return @result; } my $chunk = 8192; sub extract { my($file, $t, $b) = @_; my $head = $$b{"H$file"}; my ($code, $type) = $head =~ m/^([0-9]+)(.)/; if ($type eq "/") { mkdir $file, 0777; } elsif ($type eq "_") { my ($total, $now, $got, $x); open OUT, ">$file" or die "open for output: $! "; exists $$b{"D$code"} or die "corrupt bun file "; my $fh = $t->handle; sysseek $fh, $t->datapos, 0; $total = $t->datalen; while ($total) { $now = ($total > $chunk) ? $chunk : $total; $got = sysread $fh, $x, $now; if (not $got) { die "read error "; } $total -= $got; print OUT $x; } close OUT; } else { print STDERR "warning: skipping unknown file type "; } } die "usage " if @ARGV != 1; my (%b, $t); $t = tie %b, 'CDB_File', $ARGV[0] or die "tie: $! "; map { extract $_, $t, \%b } unnetstrings $b{""}; 5. Although a cdb file is constant, you can simulate updating it in Perl. This is an expensive operation, as you have to create a new database, and copy into it everything that's unchanged from the old database. (As compensation, the update does not affect database readers. The old database is available for them, till the moment the new one is "finish"ed.) use CDB_File; $file = 'data.cdb'; $new = new CDB_File($file, "$file.$$") or die "$0: new CDB_File failed: $! "; # Add the new values; remember which keys we've seen. while (<>) { chop; ($k, $v) = split; $new->insert($k, $v); $seen{$k} = 1; } # Add any old values that haven't been replaced. tie %old, 'CDB_File', $file or die "$0: can't tie to $file: $! "; while (($k, $v) = each %old) { $new->insert($k, $v) unless $seen{$k}; } $new->finish or die "$0: CDB_File finish failed: $! "; REPEATED KEYS
Most users can ignore this section. A cdb file can contain repeated keys. If the "insert" method is called more than once with the same key during the creation of a cdb file, that key will be repeated. Here's an example. $cdb = new CDB_File ("$file.cdb", "$file.$$") or die ...; $cdb->insert('cat', 'gato'); $cdb->insert('cat', 'chat'); $cdb->finish; Normally, any attempt to access a key retrieves the first value stored under that key. This code snippet always prints gato. $catref = tie %catalogue, CDB_File, "$file.cdb" or die ...; print "$catalogue{cat}"; However, all the usual ways of iterating over a hash---"keys", "values", and "each"---do the Right Thing, even in the presence of repeated keys. This code snippet prints cat cat gato chat. print join(' ', keys %catalogue, values %catalogue); And these two both print cat:gato cat:chat, although the second is more efficient. foreach $key (keys %catalogue) { print "$key:$catalogue{$key} "; } while (($key, $val) = each %catalogue) { print "$key:$val "; } The "multi_get" method retrieves all the values associated with a key. It returns a reference to an array containing all the values. This code prints gato chat. print "@{$catref->multi_get('cat')}"; "multi_get" always returns an array reference. If the key was not found in the database, it will be a reference to an empty array. To test whether the key was found, you must test the array, and not the reference. $x = $catref->multiget($key); warn "$key not found " unless $x; # WRONG; message never printed warn "$key not found " unless @$x; # Correct RETURN VALUES
The routines "tie", "new", and "finish" return undef if the attempted operation failed; $! contains the reason for failure. DIAGNOSTICS
The following fatal errors may occur. (See "eval" in perlfunc if you want to trap them.) Modification of a CDB_File attempted You attempted to modify a hash tied to a CDB_File. CDB database too large You attempted to create a cdb file larger than 4 gigabytes. [ Write to | Read of | Seek in ] CDB_File failed: <error string> If error string is Protocol error, you tried to "use CDB_File" to access something that isn't a cdb file. Otherwise a serious OS level problem occurred, for example, you have run out of disk space. PERFORMANCE
Sometimes you need to get the most performance possible out of a library. Rumour has it that perl's tie() interface is slow. In order to get around that you can use CDB_File in an object oriented fashion, rather than via tie(). my $cdb = CDB_File->TIEHASH('/path/to/cdbfile.cdb'); if ($cdb->EXISTS('key')) { print "Key is: ", $cdb->FETCH('key'), " "; } For more information on the methods available on tied hashes see perltie. BUGS
The "create()" interface could be done with "TIEHASH". SEE ALSO
cdb(3). AUTHOR
Tim Goodwin, <tjg@star.le.ac.uk>. CDB_File began on 1997-01-08. Now maintained by Matt Sergeant, <matt@sergeant.org> perl v5.14.2 2012-06-01 CDB_File(3pm)
All times are GMT -4. The time now is 09:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy