Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I saw this in belg4mit's signature and I can't figure it out:
perl -wpe "s/\b;([mnst])/'$1/g"
Thanks for your time.

20041001 Edit by Steve_p: Changed title from 'What does this do?'

Replies are listed 'Best First'.
Re: What does belg4mit's signature do?
by ccn (Vicar) on Sep 30, 2004 at 19:40 UTC

    • It reads STDIN line by line
    • substitutes ; by ' if it resides within a word and next character is a 'm' or 'n' or 's' or 't'
    • writes result to STDOUT

    e.g. input: don;t output: don't

    perl -MO=Deparse -wpe "s/\b;([mnst])/'$1/g" BEGIN { $^W = 1; } LINE: while (defined($_ = <ARGV>)) { s/\b;([mnst])/'$1/g; } continue { print $_; } -e syntax OK
      Weird...

      $ perl -MO=Deparse -wpe "s/\b;([mnst])/'$1/g" BEGIN { $^W = 1; } LINE: while (defined($_ = <ARGV>)) { s/\b;([mnst])/'/g; } continue { print $_; } -e syntax OK

      Where did my $1 went to? :-|

      This should be something trivial, but... my $1 is disappearing... :-| I'm going from "don;t" to "don'" :-|

      Wasn't ' the same thing as ::?

        Your shell has interpolated $1 before it came to perl

        You may try perl -MO=Deparse -wpe 's/\b;([nmst])/\'$1/g'

Re: What does belg4mit's signature do?
by NetWallah (Canon) on Sep 30, 2004 at 19:39 UTC
    Try it !

    On the command line, feed it various names.

    See what it does to a name like "Mc;master".

        Earth first! (We'll rob the other planets later)

Re: What does belg4mit's signature do?
by JediWizard (Deacon) on Sep 30, 2004 at 19:41 UTC

    As far as i can tell, it will print each line of STDIN to STDOUT with any semi-colon preceded by a word charater and followed by one of the letters m, n, s, or t converted to a single quote. I don't know why one would want to do such a thing, but that appears to be what that does.

    May the Force be with you
      I don't know why one would want to do such a thing

      It's a common typo to write "don;t" (etc) instead of "don't". Maybe he does this a lot and is asking us to ignore any such typos?

        Or maybe he's a semi-colon phobic ;-)
Re: What does belg4mit's signature do?
by talexb (Chancellor) on Sep 30, 2004 at 21:15 UTC

    What part can't you figure out?

    • perl -- runs perl
    • -wpe "s/\b;([mnst])/'$1/g" -- passes in the following command line arguments:
      • w -- enable warnings (p.502);
      • p -- loop this script around each input line (p.499); and
      • e "s/\b;([mnst])/'$1/g" -- some search and replace regular expression.
        • This regexp takes an input line and looks for \b;([mnst]), captures whatever it finds inside the brackets, and replaces it with '$1, where $1 is the thing it captured. The \b is a backspace key (p.161) word boundary, capturing and clustering is covered on p.182, using square brackets to define a list of characters is covered on p.201.
    So what does it do? My interpretation is that it looks for a backspace word boundary, followed by a semi-colon, followed by any one of 'm', 'n', 's' or 't', and replaces that with an apostrophe followed by the alpha character that it found. It does this globally on the line.

    So it probably allows belg4mit to clean up text files where he hit the ; instead of the ', since they're next to each other on his keyboard (as they are on mine.

    Did you figure out the part that you missed now?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

    Update: As my learned brothers NetWallah and VenTatsu point out, \b is not a backspace in this context but a word boundary. Thanks brothers.

    Update 2: Again, as DigitalKitty has pointed out, my conslusion doesn't include the corrections described in my first update. Hope fully these updates don't get longer than the original post. :(

         talexb:   The \b is a backspace key

      Nope.

      From "perlre":

      \b Match a word boundary

          Earth first! (We'll rob the other planets later)

      You have a minor problem with your explinaiton, from perlreref
      This one works differently from normal strings: \b An assertion, not backspace, except in a character class
      In regular expressions \b matches word boundries.
Re: What does belg4mit's signature do?
by Anonymous Monk on Oct 01, 2004 at 05:38 UTC
    Suppose If a line contains as follows check;mode It will be replaced to check'ode INPUT OUTPUT check;mj check'j check;mn check'n check;nm check'm This is what the above Expression will do .. I think It will cleare Ur doubt For PerlMonks JesuAshok
    A reply falls below the community's threshold of quality. You may see it by logging in.