Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to include a doublequoted string into a backticked expression?

by pat_mc (Pilgrim)
on Jan 23, 2009 at 11:38 UTC ( [id://738440]=perlquestion: print w/replies, xml ) Need Help??

pat_mc has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks -

I am trying to assign the return value of a Bash command into a variable. The problem seems to be the handling of the required double quotes inside of the backticked expression:
sub get_valence { my $lemma = shift @_; my $lexical_entry = `egrep -m 1 "^$lemma:" /home/lexicon`; return $lexical_entry; }
This subroutine consistently returns a blank string while the bash command itself has a non-zero length return value when executed from the shell. Could it be the string $lemma does not even get interpolated into the expression?
Can you please let me know how to correctly include the doublequotes inside the backticked expression?

Thanks in advance for your help!

Cheers - Pat

Replies are listed 'Best First'.
Re: How to include a doublequoted string into a backticked expression?
by Corion (Patriarch) on Jan 23, 2009 at 11:51 UTC

    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:/; }; ...
      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 (κς,πμ,πλ)
      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.

Re: How to include a doublequoted string into a backticked expression?
by Anonymous Monk on Jan 23, 2009 at 12:00 UTC
    backticks interpolate, so $lemma gets interpolated. See perlop Quote and Quote-like Operators The problem is linux shell does its own interpolation, just like windows
    my $backtick = q~" print qq,$_, for @ARGV " 1 2 "3 4 5" 6 %cd% ~; print " # copy in between backticks and paste in shell `$^X -le $backtick` "; print `$^X -le $backtick `; __END__ # copy in between backticks and paste in shell `D:\Perl\bin\perl.exe -le " print qq,$_, for @ARGV " 1 2 "3 4 5" 6 %cd +% ` 1 2 3 4 5 6 D:\
      The problem is linux shell does its own interpolation
      Yes, Anonymous Monk - you were spot-on in your analysis.

      Here is why:
      $ a="Hello" $ echo $a Hello $ echo "$a" Hello $ echo '"$a"' "$a" $ echo "'"$a"'" 'Hello' $ echo ""'"$a"'"" "$a"
      For the actual solution to my problem, this means:
      my $command = qq/egrep "'"^$lemma:"'" \/home\/lexicon/; my $lexical_entry = `$command`;

      Thanks to all those who were willing to help. Your effort to respond is much appreciated.

      Thanks for leading me towards the actual solution!

      Cheers - Pat
      Thanks, Anonymous Monk, for pointing this out. Essentially, if I understand your reply correctly, each shell will handle certain variables in its own, idiosyncratic way. I guess, this comes as no surprise as such.

      Any idea then as to why a string consisting of \w+ characters only would not interpolate properly into my backticked expression then?

      Cheers - Pat
        Any idea then as to why a string consisting of \w+ characters only would not interpolate properly into my backticked expression then?
        You're making the assumption that it does not interpolate properly. Prove each part. As you do, you'll find your error.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://738440]
Approved by Bloodnok
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found