in reply to Re^3: $. - smarter than you might think
in thread $. - smarter than you might think

Ok, I played with your code and came up with a modification of my own:

local $SIG{__DIE__} = sub { my ($d) = ( $_[0] =~ /line (\d+)\.$/ ); unless ($d) { print STDERR $_[0]; return; } my $context = 3; #number of context lines seek DATA,0,0; while(<DATA>) { next if $. < ($d-$context); #cycle until context starts warn ($. == $d ? '>' : ' ') #mark line if it contains the error .sprintf('%4d ',$.) #use 4-place line numbers (alignment) .$_; #show code on line last if $. > ($d+$context); #move on once we've finished context } };

The actual run-time efficiency on long code will be helped by the last if.. line: there's no reason to keep doing I/O once your context has been exhausted.

The other modifications are largely maintenance-minded:

  1. variable for number of context lines makes it easier to change your mind about context later;
  2. using the (?:) syntax allows me to have all formatting done in one place, making it easier to change the format of printed lines at will;

I also fixed one potential bug in your regex: you would have matched any 'line \d+' in the string -- I could break your code by die('I had a problem reading the data file at line 12'). You'd see the context of your code around line 12, but the relevant code might be around line 60 or something. The new regex takes the match at the end of the message.

Updates:

<radiant.matrix>
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

Replies are listed 'Best First'.
Re^5: $. - smarter than you might think
by shmem (Chancellor) on Jun 26, 2006 at 16:23 UTC
    Hey, good on ye... it's always nice to see bad hacks smoothened into a reasonable thing.

    radiantmatrix ++ if $self->($points_left).

    But! you have an extra ) on line two of your code ;) And if would get picky, I'd rather write warn +($. == $d ? '>' : ' ') than print STDERR. Flavours :)

    thx & greets,
    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s,/,($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e,e && print}