title: Can I use unpack to split a string into characters in Perl?
body: <p>A common 'Perlism' is generating a list as something to loop over in this form:</p>
  
  <p><code>for($str=~/./g) { print "the next character from \"$str\"=$_\n"; }</code></p>
  
  <p>In this case the global match regex returns a list that is one character in turn from the string <code>$str</code>, and assigns that value to <code>$_</code></p>
  
  <p>Instead of a regex, <code>split</code> can be used in the same way or <code>'a'..'z'</code>, <code>map</code>, etc. </p>
  
  <p>I am investigating <code>unpack</code> to generate a field by field interpretation of a string. I have always found <code>unpack</code> to be less straightforward to the way my brain works, and I have never really dug that deeply into it. </p>
  
  <p>As a simple case, I want to generate a list that is one character in each element from a string using unpack (yes -- I know I can do it with <code>split(//,$str)</code> and <code>/./g</code> but I really want to see if unpack can be used this way...)</p>
  
  <p>Obviously, I can use a field list for unpack that is <code>unpack("A1" x length($str), $str)</code> but is there some other way that kinda looks like globbing? ie, can I call <code>unpack(some_format,$str)</code> either in list context or in a loop such that unpack will return the next group of character in the format group until $str is exausted? </p>
  
  <p>I have read <a href="http://search.cpan.org/~jesse/perl-5.12.0/pod/perlfunc.pod#pack" rel="nofollow noreferrer">The Perl 5.12 Pack pod</a> and the <a href="http://search.cpan.org/~jesse/perl-5.12.0/pod/perlpacktut.pod" rel="nofollow noreferrer">Perl 5.12 pack tutorial</a> and <a href="http://www.perlmonks.org/?node_id=224666" rel="nofollow noreferrer">the Perkmonks tutorial</a> </p>
  
  <p>Here is the sample code:</p>
  
  <pre><code>#!/usr/bin/perl
  use warnings;
  use strict;
  
  my $str=join('',('a'..'z', 'A'..'Z')); #the alphabet...  
  
  $str=~s/(.{1,3})/$1 /g;                #...in groups of three
  print "str=$str\n\n";
  
  for ($str=~/./g) { 
   print "regex: = $_\n";
  }
  
  for(split(//,$str)) {
   print "split: \$_=$_\n";
  }
  
  for(unpack("A1" x length($str), $str)) {
   print "unpack: \$_=$_\n";
  }
  </code></pre>