How to get the character representation of a number in Perl

Overview

We can get the character representation of a number by using the chr() method in Perl.

Syntax

chr(number)

Parameters

number: This is the number that we want to get the string representation for.

Return value

number: The value returned is the string representation of the number value.

Code example

# create number variables
# and initialize
$no1 = 65;
$no2 = 66;
$no3 = 100;
$no4 = 200;
# get character representations
$ch1 = chr($no1);
$ch2 = chr($no2);
$ch3 = chr($no3);
$ch4 = chr($no4);
# print results
print "$ch1\n";
print "$ch2\n";
print "$ch3\n";
print $ch4;

Explanation

  • Lines 3-6: We create some variables and initialize them with number values.
  • Lines 9-12: We invoke the chr() method on the number values we create. We store the results in the variables $ch1, $ch2, $ch3, amd $ch4.
  • Lines 15-18: We print the results.

Free Resources