[TriLUG] Perl substitution problem

Brian via TriLUG trilug at trilug.org
Fri Jun 16 13:15:17 EDT 2017


You need a

use strict;

for "my" to work, which Wes omitted probably for the sake of brevity.

-B


On 06/16/2017 01:07 PM, karl flores via TriLUG wrote:
> Error: Parse error on line 1:
> my $string2 = 'this
> ^
> Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
>
>
> On Fri, Jun 16, 2017 at 12:57 PM, Wes Garrison via TriLUG <trilug at trilug.org
>> wrote:
>
>> My Google-fu is not good enough to solve this simple problem.
>>
>> I am building some simple JSON structures by hand in Perl, and I need to
>> escape double-quotes.
>>
>> my $string1 = 'this "should" work';
>> $string1 =~ s/"/\\"/g;
>> print $string1 \n;
>> this \"should\" work
>>
>> ^ Yay!  it substituted correctly for both double-quotes!
>>
>> my $string2 = 'this "should"*,* work';
>> $string2 =~ s/"/\\"/g;
>> print $string2 \n;
>> this \"should" work
>>
>> ^ the comma after the second double-quote broke the substitution. (and it
>> breaks my JSON)
>>
>> Why?  What the heck am I missing?
>>
>> -Wes
>> _________________________________
>> Wesley S. Garrison
>> Network Engineer
>> Xitech Communications, Inc.
>> phone:  (919) 260-0803
>> fax:       (919) 932-5051
>> __________________________________
>> "Lead us not into temptation, but deliver us from email."
>> --
>> This message was sent to: karl flores <karl.americorp at gmail.com>
>> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
>> address.
>> TriLUG mailing list : https://www.trilug.org/mailman/listinfo/trilug
>> Unsubscribe or edit options on the web  : https://www.trilug.org/
>> mailman/options/trilug/karl.americorp%40gmail.com
>> Welcome to TriLUG: http://trilug.org/welcome
>
>
>
>



More information about the TriLUG mailing list