www.perl.com
Perl Programming
Declaring Subroutines
  • Subroutines are declared with the sub keyword
  • Subroutines return values
    • Explicitly with the return command
    • Implicitly as the value of the last executed statement
  • Return values can be a scalar or a flat list
    • wantarray describes what context was used
    • Unused values are just lost

        sub ten {
            return wantarray() ? (1 .. 10) : 10;
        }

        @ten = ten();          # (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        $ten = ten();          # 10
        ($ten) = ten();        # (1)
        ($one, $two) = ten();  # (1, 2)
      
Previous http://stuff.mit.edu/iap/perl/