in reply to Extracting strings around comparision operator

I was going to suggest 3-args split, but BrowserUK already did that. So here's another way using a regexp:

... if ($line =~ /^(\w+)=(["'])(.*)\2$/) { my $key = $1; my $value = $3; ... }

Basically it does the same as the 3-args split, but additionally it strips the quotes. (\2 refers to (["']) and makes sure the quotes are balanced)

Replies are listed 'Best First'.
Re^2: Extracting strings around comparision operator
by Anonymous Monk on Aug 20, 2015 at 07:39 UTC
    speaking of which :)
    if( my( $key, undef, $value ) = $line =~ /^(\w+)=(["'])(.*)\2$/) { ...

      I would not write it that way. To me it feels like the matching is made something of an afterthought because it is 'hidden' in the second half of the line. So I wouldn't use that style. It's similar to

      open ......stuff....more stuff......... or die $! vs. open ......stuff....more stuff......... or die $!

      (Basically I like to make it hard to overlook something important when glancing at some piece of code. Even if that results in a slightly increased line count.)