200 likes | 324 Views
Dive deep into Advanced Perl programming with this comprehensive guide! Explore crucial concepts such as function definition and invocation, control structures, and filehandles. Learn how to implement user-defined subroutines and manage process operations effectively. With clear examples, including argument handling and nested subroutine calls, this resource simplifies complex topics and enhances your coding skills. Perfect for intermediate Perl programmers ready to take their expertise to the next level!
E N D
Perl - Advanced More Advanced Perl • Functions • Control Structures • Filehandles • Process Management
Perl Functions • Function – Sub routine Definition • Keyword “sub” – Definition of a sub routine • Name of the routine “subname” • Block Of statements {…} sub subname { statement_1; statement_2; statement_3; }
Invoking a User Function • We invoke a user function from within an expression by following the subroutine name with parentheses. • say_what(); or • &say_what(); • A subroutine can invoke another subroutine, and that subroutine can in turn invoke another subroutine, and so on.
Example on Arguments sub add { $sum = 0; # initialize the sum foreach $_ (@_) { $sum += $_; # add each element } return $sum; # last expression evaluated: sum of all elements } • We invoke the above function by typing add($a, $b, …) where inside the parentheses we put as many variables as we like. • The @_ variable is privateto the subroutine. This also means that a subroutine can pass arguments to another subroutine without fear of losing its own @_ variable; The nested subroutine invocation gets its own @_ in the same way.