From the course: Perl 5 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Blocks and scope

Blocks and scope - Perl Tutorial

From the course: Perl 5 Essential Training

Start my 1-month free trial

Blocks and scope

- [Voiceover] In Perl, a block of code is enclosed in curly braces, here's a working copy of blocks.pl from chapter 3 of the exercise files, and you'll notice that there are two blocks in this file. The function, which begins on line 13, you notice, it has an open curly brace on line 13, and a closed curly brace on line 17. And this foreach loop, which has an open curly brace on line 14 and a closed curly brace on line 16. So these are two different blocks. Now there's three variable that are declared outside of any of these blocks, and they are declared here on line 7, 8, and 9, and they're named alpha, beta, and charlie. Now you'll notice that they have a my keyword. The my keyword is used to give scope to a variable. And, it gives local scope to the variable to the innermost file or block where the variable is declared. So, for example, this alpha, beta, and charlie the scope of these is the file because they are not inside of a block. So if I go ahead and run this, and I'm going…

Contents