what is the option '/gee' used for in the RE expression?

J

Jking

Hi,

You perl gurus, I am reading the code of blosxom, but get stumped at the
following line:

======================================================================

# Define default interpolation subroutine
$interpolate =
sub {
package blosxom;
my $template = shift;
$template =~
s/(\$\w+(?:::)?\w*)/"defined $1 ? $1 : ''"/gee;
return $template;
};

=======================================================================

the interpolate function is used to substitute the variable value in the
context for the occurrence of variable names in $template string.

But I wonder what is meaning of the RE options /gee?

Any one can help me?

Thank you!

Justin
08/13
 
J

J. Gleixner

Jking said:
Hi,

You perl gurus, I am reading the code of blosxom, but get stumped at the
following line:

======================================================================

# Define default interpolation subroutine
$interpolate =
sub {
package blosxom;
my $template = shift;
$template =~
s/(\$\w+(?:::)?\w*)/"defined $1 ? $1 : ''"/gee;
return $template;
};

=======================================================================

the interpolate function is used to substitute the variable value in the
context for the occurrence of variable names in $template string.

But I wonder what is meaning of the RE options /gee?

Any one can help me?

You answered your own question. The interpolate subroutine does that
by using /ee.

If you want more details:

perldoc -q "How can I expand variables in text strings"
 
E

Eric Pozharski

Jking said:
# Define default interpolation subroutine
$interpolate =
sub {
package blosxom;

Don't. All lowercase modulenames are reserved for magic. You are
unqualified to make magic. If I'm wrong, then you haven't asked
question first. Because you'd be able to find answer yourself.
my $template = shift;
$template =~
s/(\$\w+(?:::)?\w*)/"defined $1 ? $1 : ''"/gee;

L<perlop|/Regexp Quote-Like Operators> has all you need to find answer
yourself (look for C<s/PATTERN/REPLACEMENT/egimosx>). In case you are
illiteral:

First left side captures variable names (lexicals, some fraction of
dynamics, and some fraction of specials). In I<$template>, obviously.
Then captured variable is replaced with right side.

Here trick begins. First C<e> forces right side to be Perl command.
That actually does nothing, since I<$1> is defined (it contains captured
part of I<$template>), captured part is replaced exactly with itself.
The second C<e> B<eval>s that string before. And what B<defined> sees
(when it's executed) is captured variable name (which can be undefined,
in that case it's replaced with empty string).

Discovering what C<g> is left as an exercise for courious reader.

22:53:04 43 [2:0]$ perl -wle '
$foo = q(xyz);
$bar = q($foo);
$bar =~ s/(\$\w+)/$1/e;
print $bar'
Name "main::foo" used only once: possible typo at -e line 2.
$foo
22:56:26 46 [2:1]$ perl -wle '
$foo = q(xyz);
$bar = q($foo);
$bar =~ s/(\$\w+)/$1/ee;
print $bar'
Name "main::foo" used only once: possible typo at -e line 2.
xyz

*CUT*

p.s. Did I called dragons again?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,210
Messages
2,571,091
Members
47,691
Latest member
Jenny-jane

Latest Threads

Top