·  PERL – Syntax – Variables – Number

 

- Info:

  - PERL has only one type of variable, called scalar.

  - This tutorial shows possible ways of loading scalar with constant number.

 

- Integers:

  - Integers can be defind in four different formats.

    $a = 0b010110111111;     #Binary      format begins with zero followed by 'b' or 'B'.

    $a02677;             #Octal       format begin with zero.

    $a =   1471;             #Decimal     format.

    $a = 0x5BF;              #Hexadecimal format begins with zero followed by 'x' or 'X'.

    

- Decimal Numbers:

  - Decimal numbers can be defind in normal and exponantional form.

    $a = 12345.67;           #Normal form.  

    $a =     0.67;          

    $a =     -.67;         

    $a = 12.23E-10;          #Exponential form.  

    $a0.23E-10;        

    $a =  -.23E-10;        

    

- Underscore:

  - Underscores are ignored and are used to make integers easier to read.

    $a =   1_471_;           #Integer.

    $a = 0x_5B_F_;           #Hexadecimal.

    $a = 0b_01011_0111111_#Binary.

    $a0_2_677_;          #Octal.

    $a = 12_3.14_15_92;      #Decimal.      

    $a = 1_2_.2_3_E-1_0_;    #Decimal.

    

- Quotations:

  - Integers in base 10 and decimal numbers are implicitly stored in memory as numbers if you use qoutes.

    $a = "9";               #Same as 9.

    $a = '9';               #Same as 9.

    $a9;

  - Integers in binary, octal or hexadecimal form are NOT implicitly stored in memory as numbers if you use qoutes.

    To force such conversion you can use functions hex and oct functions.

    $a = hex('  AF');       #Convert from hex    to base 10.

    $a = hex('0xAF');       #Convert from hex    to base 10.

    $a = oct('0xAF');       #Convert from hex    to base 10.

    $a = oct('0257');       #Convert from octal  to base 10.

    $a = oct('0b10101111'); #Convert from binary to base 10.

 

- Display:

  - You can display scalar value by using print function.

    print $a;               #Integers are displayed in base 10.