bv has asked for the wisdom of the Perl Monks concerning the following question:
I was messing around trying to get the offset of a string within a file with this code:
#!/usr/bin/perl use strict; use warnings; open my $fh, '<', shift or die "Error: $!"; my $chunk; my $find = 'regf'; while (my $bytes = read $fh, $chunk, 1024) { unless ((my $idx = index $chunk, $find) < 0) { printf "Found %s at offset %x\n", $find, tell $fh + $idx - $by +tes; } }
There is a bug in my code at tell $fh + $idx which is fixed by tell($fh) + $idx. I understand that part. What I don't understand is why it segfaults? What is the result of adding an integer to a filehandle?
This is perl, v5.10.0 built for i486-linux-gnu-thread-multi
Update: fixed logic to give actual offset
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl segfaults: Why?
by BrowserUk (Patriarch) on Sep 15, 2009 at 16:25 UTC | |
Re: Perl segfaults: Why?
by cdarke (Prior) on Sep 15, 2009 at 16:33 UTC | |
by ikegami (Patriarch) on Sep 15, 2009 at 19:52 UTC | |
by almut (Canon) on Sep 15, 2009 at 20:52 UTC | |
by almut (Canon) on Sep 16, 2009 at 03:35 UTC | |
by bv (Friar) on Sep 15, 2009 at 16:59 UTC | |
by ikegami (Patriarch) on Sep 15, 2009 at 17:11 UTC | |
Re: Perl segfaults: Why?
by ikegami (Patriarch) on Sep 15, 2009 at 16:36 UTC | |
by BrowserUk (Patriarch) on Sep 15, 2009 at 17:05 UTC | |
by FunkyMonk (Chancellor) on Sep 15, 2009 at 19:18 UTC | |
by ikegami (Patriarch) on Sep 15, 2009 at 19:55 UTC | |
by FunkyMonk (Chancellor) on Sep 15, 2009 at 21:31 UTC | |
by Burak (Chaplain) on Sep 15, 2009 at 21:27 UTC | |
Re: Perl segfaults: Why?
by ikegami (Patriarch) on Sep 15, 2009 at 16:57 UTC |