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

Hello,

2 questions on below example from Perl cookbook,

1)Never seen while being used inside of qutation while being assign to $code(on line 11) In this case, what is $code in line 17?

2)do not understand this line s{\\Q$in\\E}{$out}g at line 17
I looked up \Q and see that it means Quote metacharacters till \E
What does "quoting metacharacters" mean? I understand what metacharacter is(special character such as $ or ^) but just what is quoting metachracter mean??

\Q -- Quote (de-meta) metacharacters till \E.
1 #!/usr/bin/perl -w 2 # fixstyle - switch first set of <DATA> strings to second set 3 # usage: $0 [-v] [files ...] 4 use strict; 5 my $verbose = (@ARGV && $ARGV[0] eq '-v' && shift); 6 if (@ARGV) { 7 $^I = ".orig"; # preserve old files 8 } else { 9 die warn "$0: Reading from stdin\n" if -t STDIN; 10 } 11 my $code = "while (<>) {\n"; 12 # read in config, build up code to eval 13 while (<DATA>) { 14 chomp; 15 my ($in, $out) = split /\s*=>\s*/; 16 next unless $in && $out; 17 $code .= "s{\\Q$in\\E}{$out}g"; 18 $code .= "&& printf STDERR qq($in => $out at \$ARGV line \$. +\\n)" 19 if $verb +ose; 20 $code .= ";\n"; 21 } 22 $code .= "print;\n}\n"; 23 eval "{ $code } 1" || die; 24 __END__ 25 analysed => analyzed 26 built-in => builtin 27 chastized => chastised 28 commandline => command-line 29 de-allocate => deallocate 30 dropin => drop-in 31 hardcode => hard-code 32 meta-data => metadata 33 multicharacter => multi-character 34 multiway => multi-way 35 non-empty => nonempty 36 non-profit => nonprofit 37 non-trappable => nontrappable 38 pre-define => predefine 39 preextend => pre-extend 40 re-compiling => recompiling 41 reenter => re-enter 42 turnkey => turn-key

Replies are listed 'Best First'.
Re: script fixstyle from cookbook
by ysth (Canon) on Nov 08, 2007 at 07:03 UTC
    Probably the easiest way to get a grip on what that code is doing is to replace the eval "{ $code } 1" with a print "Code is: $code.\n";, then look at the output and compare it to the lines that are generating it.
Re: script fixstyle from cookbook
by erroneousBollock (Curate) on Nov 08, 2007 at 06:30 UTC
    Never seen while being used inside of qutation while being assign to $code(on line 11)
    while is not being used "inside" a quotation... $code is just a string being used to build up some code which will be evaluated with eval.

    In this case, what is $code in line 17?
    Depends on what's in the __DATA__ segment.

    What does "quoting metacharacters" mean? I understand what metacharacter is(special character such as $ or ^) but just what is quoting metachracter mean??
    Metacharacters in this example are characters which have special meaning in a regular expression, such as ] [ } { ) ( ^ $. See regular expressions.

    Since the loop is building code to be evaluated, it needs to escape regexp metacharacters so that they'll be correct after the eval.

    -David

Re: script fixstyle from cookbook
by cdarke (Prior) on Nov 08, 2007 at 11:45 UTC
    What does "quoting metacharacters" mean?

    You will find that people also use the term 'escape', which can be equally baffling.
    Some characters have special meanings, as you are aware in regular expressions these are called 'meta-characters'. Within Perl (and languages such as UNIX shells) placing single quotes around a character removes its special meaning, such as '*'. For single characters this is rather tedious, so placing a back-slash \ character in-front has the same effect, such as \*. The Perl Q relieves the tedium further by placing a \ in front of each non-alphanumeric (includes _) character for us.

    In the olden days we sometimes needed character pairs which were prefixed with the <ESC> character, and this is how it was done, hence the term 'escape'.
    You will note that the \ character removes the special meaning from a special (meta) character, but can also add a special meaning to an ordinary character, for example "\n". It has to be inside double-quotes or qq (interpolation) for that to happen.