String Formatting in Perl

Our first program demonstrates string formatting in Perl. Here’s the full source code with explanations:

#!/usr/bin/perl
use strict;
use warnings;

# Define a simple point structure
package Point;
sub new {
    my ($class, $x, $y) = @_;
    return bless { x => $x, y => $y }, $class;
}

package main;

# Create a point
my $p = Point->new(1, 2);

# Perl offers several ways to format strings and values

# Print a basic representation of the point
printf("struct1: %s\n", $p);

# Print the point with field names
printf("struct2: x:%d y:%d\n", $p->{x}, $p->{y});

# Print the type of the point
printf("type: %s\n", ref($p));

# Format boolean values
printf("bool: %s\n", $p ? 'true' : 'false');

# Format integers
printf("int: %d\n", 123);

# Print binary representation
printf("bin: %b\n", 14);

# Print character corresponding to ASCII value
printf("char: %c\n", 33);

# Print hexadecimal representation
printf("hex: %x\n", 456);

# Format floating-point numbers
printf("float1: %f\n", 78.9);
printf("float2: %e\n", 123400000.0);
printf("float3: %E\n", 123400000.0);

# Format strings
printf("str1: %s\n", '"string"');

# Quote strings
printf("str2: %s\n", qq("string"));

# Print hexadecimal representation of a string
printf("str3: %s\n", unpack("H*", "hex this"));

# Print memory address of a reference
printf("pointer: %p\n", \$p);

# Control width of output
printf("width1: |%6d|%6d|\n", 12, 345);
printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45);
printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45);
printf("width4: |%6s|%6s|\n", "foo", "b");
printf("width5: |%-6s|%-6s|\n", "foo", "b");

# Format without printing
my $s = sprintf("sprintf: a %s", "string");
print "$s\n";

# Print to a file handle (STDERR in this case)
printf STDERR "io: an %s\n", "error";

To run the program, save it as string_formatting.pl and use perl:

$ perl string_formatting.pl
struct1: Point=HASH(0x55e8f7e93bf0)
struct2: x:1 y:2
type: Point
bool: true
int: 123
bin: 1110
char: !
hex: 1c8
float1: 78.900000
float2: 1.234000e+08
float3: 1.234000E+08
str1: "string"
str2: "string"
str3: 6865782074686973
pointer: 0x55e8f7e93bf0
width1: |    12|   345|
width2: |  1.20|  3.45|
width3: |1.20  |3.45  |
width4: |   foo|     b|
width5: |foo   |b     |
sprintf: a string
io: an error

This Perl script demonstrates various string formatting techniques similar to those in the original example. Here are some key differences and explanations:

  1. Perl uses printf for formatted output, similar to the original example.
  2. Perl doesn’t have built-in structs, so we used a simple object-oriented approach to create a Point class.
  3. The %v and %+v formatting verbs don’t exist in Perl, so we used custom formatting for the point structure.
  4. Perl doesn’t have a direct equivalent to Go’s %#v, so we omitted that example.
  5. For boolean values, Perl doesn’t have a built-in boolean type, so we used a ternary operator to display ’true’ or ‘false’.
  6. Perl’s sprintf function works similarly to the one in the original example.
  7. To print to STDERR, we used printf STDERR instead of fmt.Fprintf(os.Stderr, ...).

This example showcases Perl’s string formatting capabilities, which are quite flexible and powerful, although the syntax and some specific features differ from Go.