Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Perl5 Internal Representation of string variable

by Marshall (Prior)
on Oct 02, 2010 at 17:03 UTC ( #863087=note: print w/ replies, xml ) Need Help??


in reply to Perl5 Internal Representation of string variable

Simple answer is no.

A Perl ASCII string is pretty much like a string in 'C'. This is a sequence of bytes in memory terminated by a x00 byte. Each character is encoded as one byte. In Perl you will never come across this last "null" byte.For fancy multi-byte character sets, I defer to wiser Monks than me.

In a 'C' or a Perl string, you will come across things like \n and \r. That backslash means, "hey this is NOT an "n" or an "r", this is something special and means 0x0A (new line) or 0x0D (carriage return) respectively.

In a Perl string, if you want something that otherwise would have a meaning, like the double quote character " to be taken literally (not part of Perl's translations), you put a \ in front of it.

print " this is a double quote, a \" \n";
In other words, this backslash thing means that the character which follows should be interpreted with a special meaning, if there is any such meaning. In the above, the backslash before the double quote (") means, hey this is not the end of the print statement quote, but rather please print literally a double quote. The \n means: this is not an "n", but rather a 0x0A character.

I guess this as clear as mud, but I tried.

Updated with some strike-thru's.


Comment on Re: Perl5 Internal Representation of string variable
Download Code
Re^2: Perl5 Internal Representation of string variable
by flexvault (Parson) on Oct 02, 2010 at 17:37 UTC

    re: ". . .bytes in memory terminated by a x00 byte. "

    In 'C' => 'YES', in Perl => 'want to know???'

    Either perl has a count of characters for variable or some other mechanism to determine the size of the variable that contains a group of bytes. Can't use x00 as terminator, since perl wonderfully allows x00 to be a valid byte.

      Yes, indeed,
      my $string = "asdf \x00 some more";
      print $string;
      does print past the asdf. So, a Perl string does know "how big it is".
Re^2: Perl5 Internal Representation of string variable
by ig (Vicar) on Oct 02, 2010 at 21:07 UTC

    It will be helpful to distinguish between the strings that scalars contain and quoted strings, including string literals. Escape characters and special codes, such as "\r", "\n", "\t", etc. have special meaning in some quoted strings, depending on the type of quotes used (see Quote and Quote like Operators), but not in the values of scalars. The values of scalars are sequences of characters or bytes, depending on whether they are character or byte strings. A string may contain the characters '\' and 'n', but neither has any special meaning in that context - they are just characters (or bytes) in the string.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863087]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-09-23 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls