·  PERL – Syntax – Variables – String

 

- Info:

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

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

  - If string can't be interpreted as number it must be enclosed between single or double qoutes.

 

- Create – Single Qoutes:

  - If single-qouted string contains variable name it will not be replaced with it's value.

    $a = 'Test';

    $b = 'Variable name is $a.'#Result is: Variable name is $a.

    print $b;                   

  - In single-qouted strings you can only escape backslash and single qoute by using '\\' or '\''.

    $a = 'Escaping \\ and \'.';   #Result is: Escaping \ and '.

    print $a;                   

 

- Create – Double Qoutes:

  - If double-qouted string contains variable name it will be replaced with it's value.

    $a = 'Test';

    $b = "Variable value is $a."#Result is: Variable value is Test.

    print $b 

  - In double-qouted strings you can use following escape sequences:

    

Esc. Seq.

Escaped Character

 

\t

tab            

(HT, TAB)

 

\n

newline

(NL)

 

\r

return

(CR)

 

\f

form feed

(FF)

 

\b

backspace

(BS)

 

\a

alarm (bell)

(BEL)

 

\e

escape

(ESC)

 

\053

octal ascii char

print "\053";       #Displays '+'.

 

\x41

hex ascii char

print "\x41";       #Displays 'A'.

 

\x{263a}

wide hex char

 

 

\c[

control char

 

 

\N{name}

named Unicode character

print "\N{U+0041}"; #Displays 'A'.

    

- Analyze:

  $test    = ("Hello" =~ /lo/);    #Result is 1 if "lo"  appears in $text.

  $test    = ("Hello" =~ /hel/i);  #Result is 1 if "hel" appears in $text ignoring case. 

  if ( $test) { print("String found.");     }

  if (!$test) { print("String NOT found."); }

 

  $length  = length("hello");      #5. Returns number of characters in a string.

 

- Modify:

  $text    = "Test";

  $changed = chop ($text);     #Removes last character from $text and returns it.

  $status  = chomp($text);     #If last character is \n removes it from $text returning 1. Else returns 0

 

  $text    = "aBCde-BC-bc";

  $text    =~ s/BC/12/;        #a12de-BC-bc. Substitute FIRST occurence of BC with 12.

  $text    =~ s/BC/12/g;       #a12de-12-bc. Substitute ALL   occurence of BC with 12.

  $text    =~ s/BC/12/gi;      #a12de-12-12. Substitute ALL   occurence of BC with 12 ignoring case.

  $text    =~ s/[Bb][Cc]/12/g; #a12de-12-12. Substitute ALL   occurence of BC with 12 ignoring case.

 

  $text    = "  test  ";

  $text    =~ s/\s/A/;         #A test  .    Substitute first space with A.

  $text    =~ s/\s//;          # test  .     Substitute first space with nothing removing it.

  $text    =~ s/^\s//;         # test  .     Remove first space.

  $text    =~ s/^\s+//;        #test  .      Remove all leading  spaces.

  $text    =~ s/\s+$//;        #  test.      Remove all trailing spaces.

    

- Get Substrings:

  $sub     = substr("Hello!", 0,2); #He. Starting index, from 0,       and number of characters to take.

  $sub     = substr("Hello!",-4,2); #ll. Starting index, from the end, and number of characters to take.

  @tokens  = split (/:/  ,"Split:this:string"  );  #[Split,this,string]. Split over double point.

  @tokens  = split (/:/  ,"Split:this:string",2);  #[Splitthis,string].  Split to maximum of 2 elements. 
  @tokens  = split (/\s+/,"Split this string"  );  #[Split,this,string]. Split over spaces. 
  ($t1,$t2)= split (/:/  ,"Split:this:string"  );  #$t1="Split",$t2="this".