Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


simple perl question

Posted on 2000-05-17
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 16

Accepted Solution

maneshr earned 80 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

730 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