http://www.perlmonks.org?node_id=863130


in reply to Perl5 Internal Representation of string variable

What you have to realize here is that you're dealing with two entirely different concepts, one of which is seamlessly intergrated into the other.

On one hand, we have Perl. In Perl, the character + has a special meaning, which in most cases would be the additive operator (IE, it adds two numbers). " is another example of a character with a special meaning. In most cases " comes in pairs and a pair of these double quotes is one of many ways to mark the part between the pair of quotes as a string. + inside a string has no special meaning.

So, basically, $foo = 3 + 4 assigns the value of 7 to $foo. $foo = "3 + 4" assigns the string of 3 + 4 to $foo, but doesn't add 3 and 4.

The second concept we're dealing with, is regular expressions. In regexes, + has a special meaning too. It's not an additive operator (so m/3 + 4/ doesn't result in something somehow matching the value 7). In regexes, + means that the symbol that precedes it, is to appear one or more times in the string you're matching against. So m/3 + 4/ would match a literal 3, followed by one or more spaces, then another space, and then a litereal 4.</c>

So if you have a string like, say, $foo = "+bar", then that's just fine. It's a string that consists of a literal "+", followed by the word "bar". Fine. But if you go m/+bar/, then you want is a bit unclear to me, but it sort of looks like (nothing, one or more times), followed by a literal occurance of the word "bar".

Like I said earlier, Perl seamlesly intergrates regexes into its language and allows you to do usefull things such as m/$foo/, incorporating a string you defined earlier into your regex. However, if $foo = "+bar", then the + inside that string does take its special meaning inside the regex, even though it doesn't mean anything but just a "+" character inside the string.

So it's not about how Perl internally represents strings, it's all about how regular expressions are a language within a language, in which certain characters or character sequences have special meanings which differ from the meaning they have in Perl. That's the whole point.

Replies are listed 'Best First'.
Re^2: Perl5 Internal Representation of string variable
by flexvault (Monsignor) on Oct 03, 2010 at 14:16 UTC

    Thank you for an excellent explanation. It was/is the regex language within perl that I failed to grasp.

      It was/is the regex language within perl that I failed to grasp.

      You may find My Favourite Regex Tools useful.

      HTH,

      planetscape