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

perl -nle '/tilda/ && s/\[// && s/\]// && print qq{insert into mytable + values(SEQ_MY_ID.nextval, '$_', '01',sysdate,'','')}' report.conf
the above obviously failed because of single quotes. thought i would use double quote instead..
% perl -nle "/tilda/ && s/\[// && s/\]// && print qq{insert into mytab +le values(SEQ_MY_ID.nextval, '$_', '01',sysdate,'','')}" report.conf _: Undefined variable.
there must be a simple fix but i can't get my head around it.. is there a way to fix it? thanks.

Replies are listed 'Best First'.
Re: quote in perl command line
by moritz (Cardinal) on Aug 09, 2007 at 14:41 UTC
    You can use double quotes inside the one-liner:

    perl -nle '/tilda/ && s/\[// && s/\]// && print qq{insert into mytable values(SEQ_MY_ID.nextval, "$_", "01",sysdate,"","")}' report.conf

    That works as long as $_ doesn't contain double quotes, and your database engine has not problems with double quotes..

    If you use double quotes on the outside, the shell tries to interpolate $_, so you have to escape it with a backslash: \$_

Re: quote in perl command line
by swampyankee (Parson) on Aug 09, 2007 at 15:55 UTC

    Quoting rules vary by shell, differing especially between Windows and Unix and Unix-like systems, e.g., Linux or cygwin. In *ixen, embedded single quotes can be escaped with a backslash. On *ixen, as in Perl, double-quoted strings will expand variables, which would include strings starting with dollar signs ($). This may not be what you want.


    emc

    Information about American English usage here and here.

    Any New York City or Connecticut area jobs? I'm currently unemployed.

Re: quote in perl command line
by graff (Chancellor) on Aug 10, 2007 at 02:21 UTC
    If I were really determined to do that sort thing on the command line, I'd probably be inclined to kluge it like this (using a bash shell, of course):
    perl -nle '$q="\x27"; /tilda/ && s/\[// && s/\]// && print "insert int +o mytable values(SEQ_MY_ID.nextval, $q$_$q, ${q}01$q,sysdate,$q$q,$q$ +q)"' report.conf
    But then I'd have to go and wash my hands...
Re: quote in perl command line
by ForgotPasswordAgain (Vicar) on Aug 09, 2007 at 15:58 UTC

    The problem is more the spaces than the quotes. For example, in bash you sometimes see something like this:

    if [[ x"$a" == x ]]; then # $a was empty, so do something fi

    You can just put characters right beside the quotes like that. So focus more on the spaces than the quotes. The quotes you always see around a command are just there to protect the spaces! (plus single quotes obviously prevent variables from interpolating)

    perl -le print\ \''hello world!\n'\'

    (That's like perl -le 'print 'hello world!\n'' except it works. :) The inner pair single-quotes are there just to keep the space and \n from being interpolated by the shell.)

Re: quote in perl command line
by technojosh (Priest) on Aug 09, 2007 at 16:53 UTC
    you need to declare double quotes in your $_ variable...

    try:

    $_ =~ s|\"|\\"|g;