simple perl question

Posted on 2000-05-17
Last Modified: 2012-08-13
hello all,
i have problem in understanding the following code:

 $text =~ s{ %% (.*?) %%}
    { exists( $fillings->{$1} ) ? $fillings->{$1} : "" }gsex;

can anyone explain it in very detail?

what is g?s?e?x? (i mean the gsex)

i guess it gets (.*?) and then
check if $fillings{$1} exists, if exist, substitute $1 by $fillings{$1},
if not, substitute $1 by "",
isn't it?

please explain in details...
Question by:mwhuen
  • 2
LVL 16

Accepted Solution

maneshr earned 20 total points
ID: 2817950
The following list defines Perl's pattern-matching operators.

g = Match globally, i.e., find all occurrences
s = Treat string as single line.
e = Evaluate the right side as an expression.
x = Use extended regular expressions.

the above pattern searches for all occurances of anything between %% %% and that string is passed to exists.

Exists function returns true if the specified hash key exists in its hash, even if the corresponding value is undefined.

if true is returned then $fillings->{$1} is evaluated else ""

Expert Comment

ID: 2824738
what is extended regular expressions?
LVL 16

Expert Comment

ID: 2825702

"what is extended regular expressions?"

Extended Regular Expressions

Perl defines an extended syntax for regular expressions. The syntax is a pair of parentheses with a question mark as the first
thing within the parentheses. The character after the question mark gives the function of the extension. The extensions are:


     A comment. The text is ignored.


     This groups things like "(...)" but doesn't make backreferences.


     A zero-width positive lookahead assertion. For example, /\w+(?=\t)/ matches a word followed by a tab, without
     including the tab in $&.


     A zero-width negative lookahead assertion. For example, /foo(?!bar)/ matches any occurrence of "foo" that isn't
     followed by "bar".


     A zero-width positive lookbehind assertion. For example, /(?<=bad)boy/ matches the word boy that follows bad,
     without including bad in $&. This only works for fixed-width lookbehind.


     A zero-width negative lookbehind assertion. For example, /(?<!=bad)boy/ matches any occurrence of "boy" that
     doesn't follow "bad". This only works for fixed-width lookbehind.


     Matches the substring that the standalone pattern would match if anchored at the given position.


     Matches a pattern determined by a condition. The condition should be either an integer, which is "true" if the pair of
     parentheses corresponding to the integer has matched, or a lookahead, lookbehind, or evaluate, zero-width assertion.
     The no-pattern will be used to match if the condition was not meant, but it is also optional.

Hope that helps.
LVL 84

Expert Comment

ID: 2826473
perldoc perlre
                 x   Extend   your   pattern's   legibility  by  permitting
                     whitespace and comments.

                 These are usually written as  ``the  /x  modifier'',  even
                 though  the  delimiter in question might not actually be a
                 slash.  In fact,  any  of  these  modifiers  may  also  be
                 embedded  within  the  regular expression itself using the
                 new (?...) construct.  See below.

                 The /x modifier itself needs a  little  more  explanation.
                 It   tells   the   regular  expression  parser  to  ignore
                 whitespace  that  is  neither  backslashed  nor  within  a
                 character  class.   You  can  use  this  to  break up your
                 regular expression into (slightly)  more  readable  parts.
                 The  #  character  is  also  treated  as  a  metacharacter
                 introducing a comment, just  as  in  ordinary  Perl  code.
                 This  also  means  that  if  you want real whitespace or #
                 characters in the pattern (outside of a  character  class,
                 where  they are unaffected by /x), that you'll either have
                 to escape them or encode them using octal or hex  escapes.
                 Taken  together,  these  features  go  a  long way towards
                 making Perl's regular  expressions  more  readable.   Note
                 that  you  have  to  be careful not to include the pattern
                 delimiter in the commentperl has no way of knowing you did
                 not  intend to close the pattern early.  See the C-comment
                 deletion code in the perlop manpage.

(?x) syntax does not require a /x switch

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question