1 / 8

Mastering Perl: Conditional Execution, Loops, and Subroutines

This guide delves into essential Perl programming concepts, including conditional execution, loops, and subroutines. Discover how to utilize 'if', 'unless', 'while', 'for', and 'do' constructs for efficient control flow. Learn to work with arrays and complex loop structures, including using labels for flow control. Subroutines will be explained, highlighting how to manage arguments and local variables effectively. Gain a solid foundation in these core programming techniques to elevate your Perl skills.

faye
Télécharger la présentation

Mastering Perl: Conditional Execution, Loops, and Subroutines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in PerlConditional Execution,Loops,Subroutines Peter Verhás January 2002.

  2. Conditional execution • if( condition ){ command(s) } • if( condition ){ command(s) }else{ command(s) } • command if condition; • unless( condition ){ command(s) } • unless( condition ){ command(s) }else{ command(s) } • command unless condition; • { and } MUST be usedfollowing if/unless.

  3. Loop constructs • while(condition){ command(s) } • do{ command(s) }while(condition) • for( exp1 ; exp2 ; expr3 ){ command(s) }

  4. Looping over a list for $i (@list){} foreach $i (@list){} for $i (n..m) {} for $i (n...m){}

  5. Make loops more complex $i = 0; while( $i < 7 ){ $i ++; print "start $i\n"; next if $i == 1; redo if $i == 2; last if $i == 4; print "end $i\n"; }continue{ print "$i countinue\n"; } OUTPUT: start 1 1 countinue start 2 start 3 end 3 3 countinue start 4 next executes the continue part. redo does not. last gets out of the loop. You can use LABELs and specify the label, which loop to next, redo or last?

  6. Subroutines sub name { command(s) } • Arguments are put into the global array@_ • You can $_[$i]or$v = shift • Return value is return expressionor just the last expression • Local variables are created using keywordmyor local

  7. localor my? $my = 'global'; $local = 'global'; &subi; &bubi; sub subi { my $my ='my'; local $local = 'local'; &bubi; } sub bubi { print "$my\n$local\n"; } OUTPUT: global local global global my is really local. local is actually global, but saves old value.

  8. Thank you for your kind attention.

More Related