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

Looking for this text : MyName='Custom' in a file. Not sure how to code for this with the single quotes, tried my $customTextinPrePostScript = "/MyName='Custom'/"; if ($line =~ /$customTextinPrePostScript/i) { do something} How do I code it to match with the single quotes?

Replies are listed 'Best First'.
Re: Match Text with Single Quotes
by toolic (Bishop) on Oct 16, 2013 at 18:03 UTC
    q
    use warnings; use strict; my $customTextinPrePostScript = q(MyName='Custom'); while (my $line = <DATA>) { if ($line =~ /$customTextinPrePostScript/i) { print $line; } } __DATA__ hello MyName='Custom' foo nothing
Re: Match Text with Single Quotes
by kcott (Archbishop) on Oct 17, 2013 at 05:16 UTC

    G'day JLowstetter,

    Welcome to the monastery.

    I'd use qr for that. See perlop: Regexp Quote-Like Operators for details.

    #!/usr/bin/env perl use strict; use warnings; my @lines = ("MyName='Custom'\n", "MyName!='Custom'\n"); my $re = qr{MyName='Custom'}; for my $line (@lines) { if ($line =~ $re) { print "MATCH: $line"; } else { print "NO MATCH: $line"; } }

    Output:

    MATCH: MyName='Custom' NO MATCH: MyName!='Custom'

    -- Ken