Professional Web Applications Themes

Trying to understand this piece of code - PERL Beginners

sudhindra k s wrote:  The $' and $` are builtin Perl variables. They are doented in perldoc perlvar $' is the text following the last successful match (i.e. the stuff on the line to the right of "CR List"). $` is the text preceeding the last successful match (the stuff on the line to the left of "(2)")...

  1. #1

    Default RE: Trying to understand this piece of code

    sudhindra k s wrote: 

    The $' and $` are builtin Perl variables. They are doented in

    perldoc perlvar

    $' is the text following the last successful match (i.e. the stuff on the
    line to the right of "CR List"). $` is the text preceeding the last
    successful match (the stuff on the line to the left of "(2)")
    Bob Guest

  2. #2

    Default RE: Trying to understand this piece of code

    On Mon, 2 Aug 2004, Bob Showalter wrote:
     
    >
    > The $' and $` are builtin Perl variables. They are doented in
    >
    > perldoc perlvar
    >
    > $' is the text following the last successful match (i.e. the stuff on
    > the line to the right of "CR List"). $` is the text preceeding the
    > last successful match (the stuff on the line to the left of "(2)")[/ref]

    And according to several reliable sources of doentation, notably
    _Mastering Regular Expressions_, they should *never be used*.

    The presence of these variables anywhere in your code changes the
    pattern matching strategy that Perl uses in such a way that performance
    is much, much worse than normal in most cases.

    The whys and hows of this are complicated -- it's worth reading _MRE_.
    The short version is that you should almost always replace code that
    uses $`, $&, or $' with functionally equivalent code that gets the same
    result without using these variables. For example, this code could be:

    if ( /CR List(.*)/ .. /(.*)\(2\)/ ) {
    if ( /CR List/ ) { print " $1\n"; }
    elsif ( /\(2\)/ ) { print " $1 \n" ;}
    else { print " $_\n";}
    }

    ....or something to that effect.



    Of course, it could be that more recent versions of Perl have fixed this
    problem and I'm not aware of it. If so, please correct me :-)



    --
    Chris Devers
    Chris Guest

  3. #3

    Default RE: Trying to understand this piece of code

    From: Chris Devers <com> 
    > >[/ref]
    > The whys and hows of this are complicated -- it's worth reading _MRE_.
    > The short version is that you should almost always replace code that
    > uses $`, $&, or $' with functionally equivalent code that gets the
    > same result without using these variables. For example, this code
    > could be:
    >
    > if ( /CR List(.*)/ .. /(.*)\(2\)/ ) {
    > if ( /CR List/ ) { print " $1\n"; }
    > elsif ( /\(2\)/ ) { print " $1 \n" ;}
    > else { print " $_\n";}
    > }
    >
    > ...or something to that effect.[/ref]

    There should be

    if ( /CR List(.*)/ .. /(.*?)\(2\)/ ) {

    Otherwise you might get different results.
    The original finds the first (2) in the line, your version the last
    one.
     

    I believe the penalty is still there.

    Of course in a one-off script that contains no other regexps it may
    not matter, but I'd still recommend forgetting these variables even
    exist.

    Jenda
    ===== cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Guest

Similar Threads

  1. This piece of code is not working for storing cookies
    By ruchir123 in forum Macromedia Flex General Discussion
    Replies: 4
    Last Post: November 23rd, 07:22 PM
  2. want to time a piece of code
    By shahzad in forum Macromedia ColdFusion
    Replies: 2
    Last Post: March 14th, 10:45 PM
  3. How do I pass a variable into a PHP piece of code
    By Bob in forum PHP Development
    Replies: 1
    Last Post: May 1st, 02:22 PM
  4. Ugent need help or piece of Code
    By Malik hassan in forum Microsoft SQL / MS SQL Server
    Replies: 1
    Last Post: June 30th, 10:48 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139