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

Hi, I'm a newbie to perl. I'm trying to go through a piece of code and encountered this regular expression. $box =~ s/%/%%/g ; can someone tell me what this is saying? Thanks

2005-10-05 Retitled by Arunbear, as per Monastery guidelines
Original title: 'a regular expression question'

  • Comment on regular expression: what does $box =~ s/%/%%/g; mean?

Replies are listed 'Best First'.
Re: regular expression: what does $box =~ s/%/%%/g; mean?
by davido (Cardinal) on Oct 04, 2005 at 20:31 UTC

    It's a substitution operator. It is telling perl to find every occurrence of "%" in $box, and replace it with "%%".

    See perlrequick and perlre for information on how regular expressions and substitutions work. You will find additional info in perlop under the m// and s/// operators.


    Dave

Re: regular expression: what does $box =~ s/%/%%/g; mean?
by ikegami (Patriarch) on Oct 04, 2005 at 20:44 UTC
    ...and the reason the author is replacing "%" with "%%" is probably because $box will be passed as the format string for printf or sprintf. "%" has a special meaning in these strings, and he wants any/all "%" treated as percent signs. The proper way of escaping a percent sign for the format string of the aformentioned functions is by doubling it.