Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

$` $' in bash??

by ZWcarp (Beadle)
on Jul 20, 2011 at 17:13 UTC ( #915685=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, I was wondering if there is a way to use $` and $' in bash commands... lets say

perl -lane '$F[1]=~s/(chr\w[^_]?)_.*\b/$1/;print $`. "\t". $F[1] ."\t" .$'' (which does not work because the apostrophe ends the executable... \' doesn't work either) .

Also why is it that \1 doesn't work... is using $1 the correct way to do this? If anyone knows of a reference that documents what special characters translate to bash commands it would be very appreciated. Thank you for your help.

Replies are listed 'Best First'.
Re: $` $' in bash??
by ikegami (Patriarch) on Jul 20, 2011 at 18:01 UTC

    There's no issue with «`» (within single quotes). The problem is with «'».

    Ways of making «foo$'bar$`baz» into a bash literal:

    foo\$\'bar\$\`baz "foo\$'bar\$`baz" 'foo$'"'"'bar$`baz' 'foo$'\''bar$`baz'

    Of course, you could use avoid using «'», starting by writing code that's much clearer.

    perl -lanE'$F[1]=~/(.*?)(chr\w[^_]?)_.*\b(.*)/; say "$1\t$1$2$3\t$3"'

    The pattern looks very fishy. /.*\b.*/? There are clearer and safer ways of writing that. I have no idea what you are trying to match with that.

Re: $` $' in bash??
by Sandy (Curate) on Jul 20, 2011 at 17:22 UTC
    use English
    perl -lane 'use English;$F[1]=~s/(chr\w[^_]?)_.*\b/$1/;print $PREMATC +H. "\t". $F[1] ."\t" .$POSTMATCH'

      It's silly to keep using the bad variables instead of /p if you're going to use the English names.

      perl -lane '$F[1]=~s/(chr\w[^_]?)_.*\b/$1/p;print ${^PREMATCH}. "\t". +$F[1] ."\t" .${^POSTMATCH}'
        Actually, I never use English, so I am not sure what it is that you are saying.

        bad variables??

        What is the reason for using ${^PREMATCH} instead of $PREMATCH?

Re: $` $' in bash??
by JavaFan (Canon) on Jul 20, 2011 at 20:16 UTC
    Using a standard trick to include a single quote:
    perl -lane '$F[1]=~s/(chr\w[^_]?)_.*\b/$1/;print $`. "\t". $F[1] ."\t" + .$'"'"
    The single quote directly following the $ matches the first single quote. In Perl, this will end a string, but not so in the shell - here single quotes act like \Q/\E in Perl. Directly following the $ and the single quote is "'", which for the shell is just a single quote. Which makes that the Perl program ends in $', as intended.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2023-01-29 03:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found