Perl Binary Strings vs Byte Arrays

In Perl, a binary string and a byte array are conceptually different but closely related.


Binary String

  • A binary string in Perl is simply a scalar ("$scalar") containing raw bytes.
  • Perl scalars are dual-purpose: they can hold both text (UTF-8, ASCII, etc.) and binary data.
  • If a scalar contains binary data, Perl treats it as an opaque sequence of bytes.

Example: Creating a Binary String

my $binary_string = "\x41\x42\x43\x00\x44";  # "ABC" + Null + "D"
print $binary_string, "\n";  # May not print correctly due to null byte
print unpack("H*", $binary_string), "\n";  # Print in hex: 4142430044

Key Points:

  • Can contain null bytes ("\x00") and other raw data.
  • Binary-safe: Perl does not assume encoding unless explicitly set.
  • Stored in a scalar variable ("$scalar").

Byte Array

  • A byte array in Perl is typically represented as a list of integers (0-255).
  • Unlike a binary string, which is a single scalar, a byte array is an array of numbers.

Example: Creating a Byte Array

my @byte_array = (0x41, 0x42, 0x43, 0x00, 0x44);  # List of bytes
print join(", ", @byte_array), "\n";  # Output: 65, 66, 67, 0, 68

Key Points:

  • Each byte is an integer between "0" and "255".
  • Stored as an array ("@array"), not a scalar.