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.


In reply to Re: Perl5 Internal Representation of string variable by muba
in thread Perl5 Internal Representation of string variable by flexvault

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.