in reply to die in regexp

i don't know, but you could avoid the issue w/something like (esp since you're not modifying the string, so no point in a substitution):
my $s = "qwe"; croak "w is bad" if $s =~ /w/; # croak if the string contains a w # OR, faster: croak "w is bad" if index($s,'w') >= 0;
(don't use $a or $b as a varname -- it's a global var for sort)

Replies are listed 'Best First'.
Re^2: die in regexp
by powerman (Friar) on Jul 03, 2006 at 19:25 UTC
    I know how I can avoid this issue. :-) I don't know is I should avoid it. :) Also, I can't avoid it so ease - I need to do s/// in some complex template, and I wish to detect errors in this template. Example with "qwe" can be converted to simple m//, but not my real task. My real task can be converted to something like:
    while (1) { $tmpl =~ /\G(...)/gcs && do { if ($1 eq "w") { croak "w is bad"; } else { $result .= process_template($1); } } || $tmpl =~ /\G(...)/gcs && do { ...do.something.else... } || last; }
    You see, it's too complex to replace current single-line s/// just to be able to croak() on errors. :(
      My real task can be converted to something like

      ...this?

      use Tie::IxHash; tie my %dt, 'Tie::IxHash', qr/one/ => \&handle_one, qr/two/ => \&handle_two; my $c="trigger one trigger two"; sub handle_one { print "one: ", shift, "\n"; } sub handle_two { print "two: ", shift, "\n"; } foreach (keys %dt) { $c =~ /\G.+?($_)/gcs and $dt{$_}->($1); }

      --
      David Serrano

        Nice example, :) but no, my task can't be converted to something like this. Here is complete story. I've template like: "some text @~var1~@ some other text @~var 2~@ end of text" and I've this hash: { "var1" => $value1, "var 2" => $value2, }. So:
        $tmpl =~ s/@~(.*?)~@/$hash{$1}/g;
        But I wish to add some protection here. Minimum:
        $tmpl =~ s/@~(.*?)~@/ defined $hash{$1} ? $hash{$1} : croak("@~$1~@ not defined") /ge;
        Maximum - also detect orphaned @~ and ~@ like in this example: "some ~@ text @~ here @~".