The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Name

SPVM::Format - Format Utilities

Usage

  use Format;
  
  # Foo 123 abc 1.115 Bar
  my $result = Format->sprintf("Foo %d %s %.3f Bar", 123, "abc", 1.115);
  
  # %d - "123"
  my $result = Format->sprintf("%d", 123);
  
  # %5d - "  123"
  my $result = Format->sprintf("%5d", 123);

  # %05d - "00123"
  my $result = Format->sprintf("%05d", 123);
  
  # %+d - "+123"
  my $result = Format->sprintf("%+d", 123);
  
  # %-5d - "123  "
  my $result = Format->sprintf("%-5d", 123);
  
  # %d - "x"
  my $result = Format->sprintf("%c", 'x');
  
  # %c - "あ"
  my $result = Format->sprintf("%c", Fn->ord("あ"));

  # %s - "ABC"
  my $result = Format->sprintf("%s", "ABC");
  
  # %.2s - "AB"
  my $result = Format->sprintf("%.2s", "ABC");
  
  # %u - "4294967295"
  my $result = Format->sprintf("%u", -1);
  
  # %f - "3.141500"
  my $result = Format->sprintf("%f", 3.1415);
  
  # %.2f - "3.14"
  my $result = Format->sprintf("%.2f", 3.1415);
  
  # %g - "3.14"
  my $result = Format->sprintf("%g", 3.14);
  
  # %x - "ff"
  my $result = Format->sprintf("%x", 255);
    
  # %x - "ffffffff"
  my $result = Format->sprintf("%x", -1);

  # %p - "0x8000000000000000"
  my $result = Format->sprintf("%p", $object);

Description

Format is a formatting utilities for sprintf method.

Class Methods

sprintf

  static method sprintf : string ($format : string, $args : object[]...);

Creates a formatted string form the $format and the $args.

Specifiers

SpecifiersDescriptionsAcceptable Types
%cAn UTF-8 characterByte, Int
%dSigned 32-bit integerInt
%f64bit floating pointDouble, Float
%g64bit floating pointDouble, Float
%xUnsiged 32-bit integer represented by hexadecima characters 0-9a-zInt
%XUnsiged 32-bit integer represented by hexadecima characters 0-9A-ZInt
%lXUnsiged 64-bit integer represented by hexadecima characters 0-9A-ZLong
%ldSigned 64bit integerLong
%luUnsigned 64bit integerLong
%lxUnsiged 64-bit integer represented by hexadecima characters 0-9a-zLong
%sStringString Type
%pAddressObject Type
%uUnsigned 32-bit integerInt

Specifier Options

Specifier options can be written between % and the character of specifier such as d, f.

Specifier OptionsDescriptions
0[DECIMAL_NUMBERS]Zero padding
+Adding a plus sign
-Left justified
.[DECIMAL_NUMBERS]Precision(Maximam width in %s)