SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can Some Please Help Me With Understand This in PERL : "S=/$=="

    I have seen a piece of code that has

    $dir =~ s=/$== ;

    Which will remove a trailing / if present in $dir but this doesn't look like any normal RegEx.
    Although it works and I understand what it is doing, and can manipulate it to do other things it's not a notation I've seen before.

    Any pointers as to what this is called and why use it over regular RegEx appreciated ( I can see it saves a "\" for the purposes of literal translation but what the heck )

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its the same as:

    $dir =~ s/\/$//;

    The person that wrote that regexp changed the default delimiter '/' to '=' so they didn't have to escape the forward slash in the search pattern with a backslash. Perl allows you to change the delimiters in a regexp to many different characters. To me a better way is using curly braces:

    $dir =~ s{/$}{};

    but you will see all sorts of delimiters if you look at enough perl code:

    $dir =~ s|/$||;
    $dir =~ s#/$##;
    $dir =~ s(/$)();
    $dir =~ s@/$@@;

    All the above do the same thing.

    when you used paired brackets/braces like {} and () you have to use the left and right bracket/brace in matching pairs.

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kevin
    Thank you for the explanation.
    Regards
    Mark

  4. #4
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome


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
  •