Perl can be compiled, or it can be used as an interpreted scripting language. Some are built into Perl, and community libraries provide others. Of course, Perl has many subroutines you never have to create yourself. You create a subroutine using the keyword sub, followed by a subroutine name of your choosing, and then the code block: #!/usr/bin/env perl In Perl, you can create a subroutine that takes inputs (stored in a special array variable called and may return an output. This practice reduces errors (or consolidates errors into one code block, so you only have to fix it once), makes your program easier to maintain, simplifies your program's logic, and makes it easier for other developers to understand. Reusing code as often as possible is a helpful programming habit. Print("Hello Perl\n") if($var = 1) Functions and subroutines Perl also features a short form of the if statement: $var = 1 The syntax is similar to C or Java: my $var = 1 The if statement is one of the most intuitive: Perl can test for a specific condition, and Perl decides how the program proceeds based on that condition. Most programs require a decision to be made, and those choices are defined and controlled by conditional statements and loops. These simple statements create a variable $var containing the strings "Hello" and "Perl" and then prints the contents of the variable to your terminal: $ perl -e '$var = "hello perl" print ("$var\n") ' Flow control Variable names in Perl are always preceded by a dollar sign ( $). In Perl, you create variables by declaring a variable name and then its contents. Variables are how computers identify and track data. "Find my phone and bring it to me" really means "Find my phone and bring my phone to me."įor computers, variables aren't a convenience but a necessity. For instance, the word "it" can refer to any noun, so we use it as a convenient placeholder. Humans use variables every day without thinking about it. Python defines scope with whitespace, LISP uses parentheses, while C and Java use braces. Blocks are a useful organizational tool, but they also provide scope for data that you may only need to use for a small section of your program. To try running your own Perl statement, open a terminal and type: $ perl -e 'print ("Hello Perl\n") ' Perl blocksĪ block of Perl statements can be grouped together with braces ( ). Perl statementsĪ Perl statement is made up of expressions. The expression $a $b contains variables (placeholders for data) and the plus symbol ( ), which is a math operator. The expression 2 returns the value of 2, and a returns the letter a.Įxpressions can be more complex. The basic unit of Perl source code is an expression, which is anything that returns a value.įor instance, 1 is an expression. On Windows, download and install it from the Perl website. On Linux and macOS, you already have Perl installed. eBook: An introduction to programming with Bash.Try for free: Red Hat Learning Subscription.
0 Comments
Leave a Reply. |