title: Perl questions regarding unpack() and the v flag in printf()
body: <p>I am trying to accomplish the following:</p>
  
  <p>For an arbitrary Perl string (whether or not it is internally encoded in UTF-8, and whether or not it has the UTF-8 flag set), scan the string from left to right, and for every character, print the Unicode code point for that character in hex format. To make myself absolutely clear: I do not want to print UTF-8 byte sequences or something; I just would like to print the <em>Unicode code point</em> for every character in the string.</p>
  
  <p>At first, I have come up with the following solution:</p>
  
  <pre><code>#!/usr/bin/perl -w
  
  use warnings;
  use utf8;
  use feature 'unicode_strings';
  
  binmode(STDOUT, ':encoding(UTF-8)');
  binmode(STDIN, ':encoding(UTF-8)');
  binmode(STDERR, ':encoding(UTF-8)');
  
  $Text = "\x{3B1}\x{3C9}";
  print $Text."\n";
  printf "%vX\n", $Text;
  
  # Prints the following to the console (the console is UTF8):
  # αω
  # 3B1.3C9
  </code></pre>
  
  <p>Then I have seen some examples, but without reasonable explanations, which made me doubt that my solution is correct, and now I have got questions regarding my own solution as well as the examples.</p>
  
  <p>1) Perl's documentation about the v flag in (...)printf says:</p>
  
  <p>"This flag tells Perl to interpret the supplied string as a vector of integers, one for each character in the string. [...]"</p>
  
  <p>It does not say what it exactly means by "a vector of integers", though. When looking at the output of my example, it <em>seems</em> that those integers are the Unicode code points, but I would like to have this confirmed by somebody who knows for sure.</p>
  
  <p>Hence the question:</p>
  
  <p>1) Can we be sure that every integer which is pulled from the string that way is the respective character's Unicode code point (and not some other byte sequence)?</p>
  
  <p>Secondly, regarding an example which I have found (slightly modified; I can't remember where I got it from, maybe from the Perl docs):</p>
  
  <pre><code>#!/usr/bin/perl -w
  
  use warnings;
  use utf8;
  use feature 'unicode_strings';
  
  binmode(STDOUT, ':encoding(UTF-8)');
  binmode(STDIN, ':encoding(UTF-8)');
  binmode(STDERR, ':encoding(UTF-8)');
  
  $Text = "\x{3B1}\x{3C9}";
  print $Text."\n";
  printf "%vX\n", $Text for unpack('C0A*', $Text);
  
  # Prints the following to the console (the console is UTF8):
  # αω
  # 3B1.3C9
  </code></pre>
  
  <p>Being a C and assembly guy, I just don't get why somebody would write the <code>printf</code> statement like shown in the example. According to my understanding, the respective line is syntactically equivalent to:</p>
  
  <pre><code>for $_ (unpack('C0A*', $Text)) {
    printf "%vX\n", $Text;  
  }
  </code></pre>
  
  <p>As far as I have understood, <code>unpack()</code> takes <code>$Text</code>, unpacks it (whatever that means in detail) and returns a list which in this case has one element, namely the unpacked string. Then $_ runs through that list with one element (without being used anywhere), hence the block (i.e. the <code>printf()</code>) is executed once. In summary, the only action which is done by the above snippet is executing <code>printf "%vX\n", $Text;</code> one time.</p>
  
  <p>Hence the question:</p>
  
  <p>2) What could be the reason for wrapping this into a for loop like shown in the example?</p>
  
  <p>Final questions:</p>
  
  <p>3) If the answer to question 1) is "yes", why do most examples I have seen use <code>unpack()</code> after all?</p>
  
  <p>4) In the three line snippet above, the parentheses which surround the <code>unpack()</code> are necessary (leaving them away leads to syntax errors). In contrast, in the example, the <code>unpack()</code> does not need to be enclosed in parentheses (but it does not harm if they are added nevertheless). Could anybody explain the reason?</p>
  
  <p><strong>Edit / Update in reply to ikegami's answer below:</strong></p>
  
  <p>Of course, I know that strings are sequences of integers. But</p>
  
  <p>a) There are many different <em>encodings</em> for those integers, and the bytes which are in a certain string's memory area depend on the encoding, i.e. if I have two strings which contain exactly the same <em>character sequence</em>, but I store them in memory using different encodings, the <em>byte sequences</em> at the strings' memory locations are different.</p>
  
  <p>b) I strongly suppose that (besides Unicode) there are many other systems / standards which map characters to integers / code points. For example, the Unicode code point 0x3B1 is the Greek letter α, but in some other system, it may be the German letter Ö.</p>
  
  <p>Under these circumstances, the question makes perfect sense IMHO, but I possibly should be more precise and reword it:</p>
  
  <p>If I have a string <code>$Text</code> which only contains characters which are Unicode code points, and if I then execute <code>printf "%vX\n", $Text;</code>, will it print the <em>Unicode</em> code point in hex for every character <em>under all circumstances</em>, notably (but not limited to):</p>
  
  <ul>
  <li>regardless of Perl's actual internal <em>encoding</em> of the string</li>
  <li>regardless of the string's UTF-8 flag</li>
  <li>whether or not <code>use 'unicode_strings'</code> is active</li>
  </ul>
  
  <p>If the answer is yes, what sense do all the examples make which are using <code>unpack()</code>, notably the example above? By the way, I now have remembered where I got that one from: the original form is in Perl's <code>pack()</code> documentation, in the section about the C0 and U0 mode. Since they are using <code>unpack()</code>, there must be a good reason for doing so.</p>
  
  <p><strong>Edit / Update No. 2</strong></p>
  
  <p>I have done further research. The following proves that the UTF8 flag plays an important role:</p>
  
  <pre><code>use Encode;
  use Devel::Peek;
  
  $Text = "\x{3B1}\x{3C9}";
  Dump $Text;
  printf("\nSPRINTF: %vX\n", $Text);
  print("UTF8 flag: ".((Encode::is_utf8($Text)) ? "TRUE" : "FALSE")."\n\n");
  
  Encode::_utf8_off($Text);
  Dump $Text;
  printf "\nSPRINTF: %vX\n", $Text;
  print("UTF8 flag: ".((Encode::is_utf8($Text)) ? "TRUE" : "FALSE")."\n\n");
  
  # This prints the following lines:
  #
  # SV = PV(0x1750c20) at 0x1770530
  #   REFCNT = 1
  #   FLAGS = (POK,pPOK,UTF8)
  #   PV = 0x17696b0 "\316\261\317\211"\0 [UTF8 "\x{3b1}\x{3c9}"]
  #   CUR = 4
  #   LEN = 16
  #
  # SPRINTF: 3B1.3C9
  # UTF8 flag: TRUE
  #
  # SV = PV(0x1750c20) at 0x1770530
  #   REFCNT = 1
  #   FLAGS = (POK,pPOK)
  #   PV = 0x17696b0 "\316\261\317\211"\0
  #   CUR = 4
  #   LEN = 16
  #
  # SPRINTF: CE.B1.CF.89
  # UTF8 flag: FALSE
  </code></pre>
  
  <p>We can see that <code>_utf_off</code> indeed removes the UTF8 flag, but leaves the string's bytes untouched. <code>sprintf()</code> with v flag outputs different results, solely dependent on the string's UTF8 flag even if the string's bytes remain the same.</p>