-
-
10:00
»
Open Source Web Development Tutorials - RSS Feeds
Lists Are One Dimensional Recall that all lists and all arrays are one dimensional. If we have this list: (@a, @b) it becomes a one-dimensional list containing the contents of @a followed by the contents of @b . This is an important rule when it comes to passing arrays into functions, since they will be passed in as a one-dimensional list. This is illustrated in the following example: #!/usr/bin/perl -w # passarrays.pl use strict; my(@nums1, @nums2); @nums1 = (2, 4, 6); @nums2 = (8, 10, 12); process_arrays(@nums1, @nums2); sub process_arrays This program creates two 3-element ar...
-
-
10:30
»
Open Source Web Development Tutorials - RSS Feeds
Lexical Variables (aka Local Variables) The range of effect that a variable has is called its scope, and lexical variables declared with my() are said to have lexical scope. This is also known as local scope. That is, they exist from the point where they're declared until the end of the enclosing block. The name lexical comes from the fact that they're confined to a well-defined chunk of text. my $x; $x = 30; print $x; # This $x is, and always has been, 30. Great. We can now use variables in our subroutines in the knowledge that we're not going to upset any behavior outside them. Let...
-
-
9:30
»
Open Source Web Development Tutorials - RSS Feeds
Understanding Scope It's now time to have a look at what we're doing when we declare a variable with my() . The truth, as we've briefly glimpsed it, is that Perl has two types of variable. One type is the global variable (or package variable), which can be accessed anywhere in the program, and the second type is the lexical variable (or local variable), which we declare with my() . Global Variables All variables in the program are global by default. Consider this code: #!/usr/bin/perl -w $x = 10; $x is a global variable. It is available in every subroutine in the program. For instance, ...
-
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
Passing Arguments into Functions As well as being set pieces of code to be executed whenever we need them, we can also use our user-defined functions just like Perl's built-in functions-we can pass arguments (aka parameters) to the subroutine and expect an answer back. Just like with Perl's built-ins, we pass parameters by placing them between the parentheses: my_sub(10,15); What happens to them there? Well, they end up in one of Perl's special variables, the array @_ , and from there we can get at them. We'll illustrate this with a subroutine that takes a list of values, adds them up, a...
-
-
11:00
»
Open Source Web Development Tutorials - RSS Feeds
Invoking a Subroutine The conventional way to invoke a function is to follow the function name with parentheses. This invokes the example_subroutine() function: example_subroutine(); If the function takes arguments (more on passing arguments later in this chapter), then drop them within the parentheses: example_subroutine('Perl is', 'my favorite', $language); Let's look at a complete example. It's traditional for programs to tell you their version and name either when they start up or when you ask them with a special option. It's also convenient to put the code that prints this infor...
-
-
7:00
»
Open Source Web Development Tutorials - RSS Feeds
When programming, there will naturally be activities we will want to do again and again: adding up the values in an array, stripping extraneous blank spaces from a string, getting infor mation into a hash in a particular format, and so on. It would be tedious to write out the code for each of these little processes every time we need to use one, and maintaining each code segment would be horrific: if there's a bug in the way we've coded the activity, we'll have to go through and find each one of them and fix it. It would be better if we could define a particular process just once, and then be ...
-
-
10:00
»
Open Source Web Development Tutorials - RSS Feeds
Escape Sequences UTF8 gives us 65536 characters, and ASCII gives us 256 characters, but on the average keyboard, there's only a hundred or so keys. Even using the shift keys, there will still be some characters that you aren't going to be able to type. There'll also be some things that you don't want to stick in the middle of your program, because they would make it messy or confusing. However, you'll want to refer to some of these characters in strings that you output. Perl provides us with mechanisms called escape sequences as an alternative way of getting to them. We've already seen the us...
-
-
10:00
»
Open Source Web Development Tutorials - RSS Feeds
Program Structure One of the things we want to develop throughout this book is a sense of good programming practice. Obviously this will not only benefit you while using Perl, but in almost every other programming language too. The most fundamental notion is how to structure and lay out the code in your source files. By keeping this tidy and easy to understand, you'll make your own life as a programmer easier. Documenting Your Programs As we mentioned earlier, a line starting with a hash, or pound sign ( # ), is treated as a comment, and ignored. This allows you to provide comments about ...
-
-
9:30
»
Open Source Web Development Tutorials - RSS Feeds
Every programming language has a number of things in common. The fundamental concepts of programming are going to be the same, no matter what language in which you do them. In this chapter, we'll investigate the things you need to know before you start writing any programs at all. For instance: What is programming anyway? What does it mean to program? How do we structure programs, and make them easy to understand? How do computers see numbers and letters? How do we find and eliminate errors in our programs? Of course, we'll be looking at these from a Perl perspective, and we'll...