in reply to How to include a doublequoted string into a backticked expression?

perlop says that backticks behave like double-quoted strings, so they interpolate. You can try first building the string and then running it, that way it's easier to debug:

my $cmd = qq{egrep -m 1 "^$lemma:" /home/lexicon}; warn "Running [$cmd]"; my $lexical_entry = `$cmd`;

... but you could also just use Perl instead of egrep:

... my $filename = '/home/lexicon'; open my $fh, '<', $filename or die "Couldn't open '$filename': $!"; while (<$fh>) { return $_ if /^$lemma:/; }; ...

Replies are listed 'Best First'.
Re^2: How to include a doublequoted string into a backticked expression?
by massa (Hermit) on Jan 23, 2009 at 13:40 UTC
    I will second the "use perl, ditch egrep" suggestion. The code will save some forks/execs and will become portable between platforms.
    []s, HTH, Massa (κς,πμ,πλ)
Re^2: How to include a doublequoted string into a backticked expression?
by pat_mc (Pilgrim) on Jan 23, 2009 at 12:18 UTC
    Thanks, Corion

    Forgive if I am being thick ... but based on your post I still cannot see where or why my code is going wrong ... basically it looks like a paraphrase of your code (or am I wrong?)

    I appreciate your illumination!

    Cheers - Pat

      Print your command and copy/paste it to your shell to see what it actually does. Corion already gave you the code for it :-)

      --
      seek $her, $from, $everywhere if exists $true{love};

      I can't either, because I neither know where your subroutine is called, what the parameters are, especially of $lemma and what the contents of /home/lexicon are, and what the expected output is. Possibly the reply by the good Anonymous Monk is right on, as whatever shell you call will also interpret the command line and interpolate things, a second time. But it's hard to tell, as my array of crystal balls is currently busy divining the lottery numbers for next weekend.

        Fair enough, Corion ...

        I have verified the correct value for $lemma by printing it in the subroutine, so I think it should be OK.

        The file lexicon contains many - i.e.: MANY - entries of the format ^\w+\:=($attribute:$value,)+$attribute:$value; where $attribute and $value are lexical attributes of a given lemma (essentially a 'word' ... whatever that exactly may be).

        As I mentioned in my post, the matching works fine when the entire egrep ... command is executed from the shell so I think the problem does not originate from difficulties to match the regex.

        Of course, what is being matched may of course differ between the two invocations based on potentially different results of string interpolation ...