Perl Subroutine

From GM-RKB
(Redirected from Perl Function)
Jump to navigation Jump to search

A Perl Subroutine is a software function within a Perl program.

  • Example(s):
    • sub get_one() {return "one"}
    • sub get_two() {return ("one", "two")}
    • sub get_max($,$) {if ($_[0] > $_[1]) {return $_[0]} else {return $_[1]}}
    • sub get_max() {my($max) = shift @_; foreach (@_) {if ($_ > $max_so_far) {$max = $_}} return $max}
    • a Perl GetOptions Subroutine.
  • Counter-Example(s)
  • See: Perl Data Structure.


References

2005


Examples

Return two or more variables
   my ($one, $two) = get_two();

   sub get_two() {
      return ("val_1", "val_2")
   }
Receive a predetermined and finite number variables
   sub receive_two($$) {
      print "WARNING! &receive_two should receive exactly two arguments!\n" if (@_ != 2) ;
      my ($a,$b) = @_;
   }
Receive an indeterminate number variables
   sub receive_indeterminate_number() {
      foreach (@_) { 
          ...
      }
   }