Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

simple perl question

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...
thanks.
0
mwhuen
Asked:
mwhuen
  • 2
1 Solution
 
maneshrCommented:
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 ""
0
 
bighuenCommented:
what is extended regular expressions?
0
 
maneshrCommented:

"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:

(?#text)

     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.

(?(condition)yes-pattern|no-pattern)
(?(condition)yes-pattern)

     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.
0
 
ozoCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now