·  PERL – Syntax – Other – Functions

 

- Info:

  - This tutorial shows how to create and call functions in PERL

  -  Function is created using sub keyword which stands for subroutine.

  - When function is called PERL stores input parameters into built in array "@_".

  - Older versions of Perl required to call a function with a prefix '&' like &test();.

 

- With No Parameters:

  - This example shows how to create and call function that has no input parameters.

 

    Test.pl

    test();                           #Call function test with no parameters.

        

    sub test {                        #Define function test that has no input parameters.

      print("Hello from test.");

    }

 

- With Parameters – Load from @_:

  - This example shows how to create and call function that has input parameters.

 

    Test.pl

    test("First","Second");           #Call function with two parameters.

      

    sub test {                        #Define function.

      my ($firstArg, $secArg) = @_;   #Load function arguments from input array into scalars.

      print($firstArg, $secArg);      #Displays: FirstSecond.

    }

 

- With Parameters – Load using shift:

  - This example shows how to create and call function that has input parameters.

  - Each call to shift function returns next element from @_ array.

 

    Test.pl

    test("First","Second");           #Call function.

    

    sub test{                         #Define function.

      my $firstArg = shift;           #Take & remove first  element from input array @_.

      my $secArg   = shift;           #Take & remove second element from input array @_.

      print($firstArg, $secArg);      #Disaplays: FirstSecond

    }

 

- With Return Value:

  - This example shows how to return value from a function.

 

    Test.pl

    $result = test(10,5);             #Store function return value into scalar $result.

    print($result);

      

    sub test {                        #Define function.

      my ($firstArg, $secArg) = @_;   #Load function arguments from input array into scalars.

      return $firstArg + $secArg;     #Return result of adding two arguments.

    }

 

- Call it from another package:

  - This example shows how to call function which is in another package.

  - Depending on the syntax of function call PERL will automaticly insert package name from which function was called

    as first argument.

 
  Test3.pl

  #PACKAGE IVOR.--------------------------------------

  package test;

 

  sub func1{

    $arg1 = shift;

    $arg2 = shift;

    print $arg1."-".$arg2."\n";

  }

 

  #PACKAGE MAIN.--------------------------------------

  package main;

 

  #Package name is automaticly inserted as first argument. Result of function is "test-1".

  test->func1 (1,2);    #Call function from from another package by defining package  name first.

  func1 test  (1,2);    #Call function from from another package by defining function name first.

  func1 test   1,2 ;    #Curly brackets are optional in function calls.

   

  #Package name is NOT forwarded as function parameter.    Result of function is "1-2".

  &test::func1(1,2);

      test::func1(1,2);