Website hosting service by Active-Venture.com
  

 Back to Index

General Regular Expression Traps using s///, etc.

All types of RE traps.

* Regular Expression

s'$lhs'$rhs' now does no interpolation on either side. It used to interpolate $lhs but not $rhs. (And still does not match a literal '$' in string)

 
    $a=1;$b=2;
    $string = '1 2 $a $b';
    $string =~ s'$a'$b';
    print $string,"\n";

    # perl4 prints: $b 2 $a $b
    # perl5 prints: 1 2 $a $b  

* Regular Expression

m//g now attaches its state to the searched string rather than the regular expression. (Once the scope of a block is left for the sub, the state of the searched string is lost)

 
    $_ = "ababab";
    while(m/ab/g){
        &doit("blah");
    }
    sub doit{local($_) = shift; print "Got $_ "}

    # perl4 prints: Got blah Got blah Got blah Got blah
    # perl5 prints: infinite loop blah...  

* Regular Expression

Currently, if you use the m//o qualifier on a regular expression within an anonymous sub, all closures generated from that anonymous sub will use the regular expression as it was compiled when it was used the very first time in any such closure. For instance, if you say

 
    sub build_match {
        my($left,$right) = @_;
        return sub { $_[0] =~ /$left stuff $right/o; };
    }
    $good = build_match('foo','bar');
    $bad = build_match('baz','blarch');
    print $good->('foo stuff bar') ? "ok\n" : "not ok\n";
    print $bad->('baz stuff blarch') ? "ok\n" : "not ok\n";
    print $bad->('foo stuff bar') ? "not ok\n" : "ok\n";  

For most builds of Perl5, this will print: ok not ok not ok

build_match() will always return a sub which matches the contents of $left and $right as they were the first time that build_match() was called, not as they are in the current call.

* Regular Expression

If no parentheses are used in a match, Perl4 sets $+ to the whole match, just like $&. Perl5 does not.

 
    "abcdef" =~ /b.*e/;
    print "\$+ = $+\n";

    # perl4 prints: bcde
    # perl5 prints:  

* Regular Expression

substitution now returns the null string if it fails

 
    $string = "test";
    $value = ($string =~ s/foo//);
    print $value, "\n";

    # perl4 prints: 0
    # perl5 prints:  

Also see Numerical Traps for another example of this new feature.

* Regular Expression

s`lhs`rhs` (using backticks) is now a normal substitution, with no backtick expansion

 
    $string = "";
    $string =~ s`^`hostname`;
    print $string, "\n";

    # perl4 prints: <the local hostname>
    # perl5 prints: hostname  

* Regular Expression

Stricter parsing of variables used in regular expressions

 
    s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o;

    # perl4: compiles w/o error
    # perl5: with Scalar found where operator expected ..., near "$opt$plus"  

an added component of this example, apparently from the same script, is the actual value of the s'd string after the substitution. [$opt] is a character class in perl4 and an array subscript in perl5

 
    $grpc = 'a';
    $opt  = 'r';
    $_ = 'bar';
    s/^([^$grpc]*$grpc[$opt]?)/foo/;
    print ;

    # perl4 prints: foo
    # perl5 prints: foobar  

* Regular Expression

Under perl5, m?x? matches only once, like ?x?. Under perl4, it matched repeatedly, like /x/ or m!x!.

 
    $test = "once";
    sub match { $test =~ m?once?; }
    &match();
    if( &match() ) {
        # m?x? matches more then once
        print "perl4\n";
    } else {
        # m?x? matches only once
        print "perl5\n";
    }

    # perl4 prints: perl4
    # perl5 prints: perl5

 

 

 

Domain name registration service & domain search - 
Register cheap domain name from $7.95 and enjoy free domain services 
 

Cheap domain name search service -
Domain name services at just
$8.95/year only
 

Register domain name -
Buy domain name registration and cheap domain transfer at low, affordable price.

© 2002-2004 Active-Venture.com Web Site Hosting Service

 

[ If the code and the comments disagree, then both are probably wrong.   ]

 

 
 
 

Disclaimer: This documentation is provided only for the benefits of our web hosting customers.
For authoritative source of the documentation, please refer to http://www.perldoc.com