Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Perl5 Internal Representation of string variable

by muba (Priest)
on Oct 03, 2010 at 02:51 UTC ( #863130=note: print w/replies, xml ) Need Help??

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.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863130]
[marto]: slowclap.gif

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2017-07-24 09:52 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (350 votes). Check out past polls.