The Perl Programming Language. In the preceding two chapters we've examined the basic language structure. Perl, its components, and the powerful pattern matching subsystem that. In this chapter we will examine Perl's flow- of- control. Perl provides. Variables can only store a given type of data: for example. It is usually difficult to interchange data of. Because Perl is an interpreted language and the Perl DBI module uses. Perl sample programs demonstrate how to connect and run database. Programming Perl Kindle Edition. Perl is a powerful programming language that has grown in popularity since it first appeared in 1988. Perl has a single catch- all container for all unitary. A scalar can be of any necessary length and can. Numerical programming You are likely to want to program some numerical code at some point in your PhD. If you are using a compiled programming language (unlike Perl or Python). Scripting and Programming Languages. Perl is starting to move towards it's C heritage by adding the ability to program using objects. It also is added the ability to be as structured, or unstructured, as you want it to be. Here are some sample perl programs. Perl is a well-established programming language that has been developed through the time and effort of countless free software programmers into an. If you want to learn how to program with Perl and then find out how. You can sometimes force Perl to treat a. While most Perl operators, functions, and subroutines will try. For example. if (! The result is negated, and used by if(); because open() only returns FALSE if it fails, this if() statement only triggers the associated block if the open() failed. For. statement. 1 . To show how this is used. MYFILE, . Use of a logical- OR (or. Keep an eye on the. Perl 5's. English- language ``or'' and ``and'' operators have a much lower precedence. For example, we can use a comma in. We'll see. how blocks affect variable scope later (when we deal with context.). The flow- of- control primitives aren't the only ways of structuring Perl. This takes a scalar and a list as parameters. For. each item in the list, the scalar is set to the item's value and the block. For example. foreach $color (. Instead of a straight. Thus, the perl. statements in brackets evaluate to a list, which is what foreach() expects to see. That's what $i is for in the example above; it's a counter. You can also say. However it's unique in that it's. In fact, if you. get into the habit of pronouncing ``$. Suppose you do something like this. We then iterated through. This is because when we say the scalar variable. After all, doesn't this tell Perl implicitly that the. Well, yes: and we then act on the. This is an important point for programmers. C or similar languages - - scalars are not integers, char, float, or. If you're really. C data structure called an SV. But what Perl does with an SV has nothing whatsoever to do. C would do with a string or integer or whatever. At that point either you will achieve. Pascal and Prolog. Clue: like. the dromedary, Perl ain't pretty - - but it works. Like the C language for(). For example. for ($bananas = 1; $bananas < 8; $bananas++) . While the second (stop) condition is true, the. Thus. when $bananas exceeds 7, the loop is no longer executed. It's quite valid to say. Weird, but possibly useful if you want to extract one. Readability is usually more important than raw performance. Obfuscated Perl contest. Operators. are primitives which are used to build statements; we've already seen quite. Get. these wrong and they won't do what you expect them to. They also have a. Associativity is one of those algebraic. In mathematics, it's a way of expressing the idea that brackets. A * B * C = (A * B) * C = A * (B * C). In Perl, associativity takes on a subtly different meaning; it indicates. For example, the += operator is. It assumes that you want to grab whatever is to the. For example, $i += 1 grabs the ``1'' and adds it to $i. If you have a more complex term on the right, you may need to bracket it. For example. $my. The logical- OR operator. Did you want this statement to evaluate to $butter every time, or only when $guns was undefined? If the latter, you need to bracket it. It makes it easier to. Perl. Even if you think you. Which is what you thought Perl thought you meant to begin with, isn't it? In general, these are a superset of the operators you get with. C; the main exceptions are the * and & operators from C (to do with. Perl. Be warned, however, that Perl is magical. When you. call a function, you use brackets. When you indicate that a collection of. When you try to. clarify expressions involving operators, you use brackets. Perl is smart. about brackets; in general, if something looks like a function it treats it. Perl's interpretation of brackets is thus complex, and although it. C) trying to define it in. Otherwise you will be confused. All the. operators and functions are grouped by category in an easy- to- find format. However, this isn't a very sensible way of structuring a program. A subroutine is a mini- programmette. You declare a. subroutine like this. You. specify them (as above) in brackets; from inside the subroutine, they look. You can pass multiple arrays to a subroutine, but they'll be jammed into @. The same goes for hashes; as a hash looks like an unordered list. One size fits all, on the receiving end. There're two ways round this. For now, just. remember: parameters to a subroutine end up in @. Originally (in the dim. Perl) Perl had only global variables - - a. This was unsatisfactory. You. declare a variable as being local to a function like this. You can. think of a symbol table as being a special kind of hash; for a given. The first instruction in & change. Perl saves the current (global) value of $number in a safe location until it exits the enclosing block (by leaving & change. At this point, it throws away whatever was stored in $number and restores the old value. It's not. very fast, and it has drawbacks (which we'll see later), but it was the. Perl 4 and it's still used. The important point to note is that the contents of a. Perl leaves the block in which the variable is. For example, here's a. It's comminly used for getting at the first item in a parameter list. Every time we invoke fact() we take the current value (say, 'n'), and multiply it by fact(n - 1), while n > 0. If 'n' is no longer greater than zero, we return the. If they weren't local, this subroutine couldn't work; it. For one thing, it would be better to use my(), as we'll see later. For another thing, it makes no checks to ensure that. For a. third thing, it uses recursion freely. Perl can often warn you if you're in. Suffice to. say, all this subroutine demonstrates is that Perl can handle recursion. You use grep to iterate through an. But you can also use. To illustrate. @days = qw(monday tuesday wednesday thursday friday saturday sunday). Calling grep() in scalar context rather than array context doesn't cause a run- time. Consider the following. Firstly, there's the built- in function. So you could write a 'safe' grep, which always. We'll see the reason for using my() later. Note also that this could be improved considerably by using some. In most sane high- level languages you can declare a subroutine like. Function Safe. Grep (Expr : string. Array: array . It's not mandatory - - in. You use it like this. The. prototype string is a set of symbols which indicate the required parameters. A '$' symbol means ``expect a scalar at this point''; an. There are some other symbols you can use; '*' for a. So a subroutine. declaration like. A Perl subroutine isn't insulated from the outside. Unless it declares all its variables to be local, other subroutines. And seeing the. outside world only in terms of a single list (@. First, the Perl 4 way; use local() to dynamically save the global state of a variable while in a local. Next, the Perl 5 way; use. There's something to be said for each technique, so. I'll explain them both. You. can actually have more than one symbol table in use in a Perl program. Chapter 7 (data encapsulation). Thus, the value. associated with the variable within that subroutine is insulated from its. For. example, suppose we have a hash called %my. We can take a typeglob to. The first parameter is a scalar, which tells print. Thus, for a hash called %myvars, you would pass print. We use shift() to pull typeglobs off the @. A reference is simply the actual. You can store the location of a variable in a. So if you have a hash. Perl where it is. But if we print them out we see something like. HASH(0x. 80be. 8d. ARRAY(0x. 80be. 8d. This tells us that we're looking, not at a scalar's value, but at a. The string in brackets is the item's. Perl's symbol table - - but it's no use to us, as it's just a. A reference is simply the. Perl. We can talk about the underlying hash by. The ``%'' prefix indicates that we're talking about it in a hash context. More seriously, suppose we want to use a. In C, we can refer to. In Perl, we can have an array of. But how do we talk about. For this to work we are using . This relies on the fact that a block of code. If we enclose a scalar containing a reference in. Let's define it. @array. We can get the reference we need like this. So we need to prefix it with an ``@'' sign. For a trivial example. If we had passed the arrays through, rather than references to them. In addition to a very powerful language. Perl has a plethora of built- in functions for doing various things. Perl is like a Swiss army knife: you can never. If we did, this chapter would be as long as a book! If you need to. examine them in detail, see the perlfunc manual page, or Chapter 3 of. Programming Perl'' (both of which run to about a hundred pages). This is all well and good, but by now you. As it happens, you still don't know enough to do anything. Perl/Tk .. While they aren't interactive, don't have. UNIX filter. commands typically read from their standard input (the file handle STDIN). STDOUT); warnings. STDERR), which is usually. STDOUT if you're running them on a terminal. Filters may also be given the. But in general, the same principle applies; we use. These are passed to it in the array @ARGV. If there. are arguments, it modifies its behaviour appropriately and (if files are. The usual result of processing some data is to. One file of commands. There are circumstances where we need a much more complex. For now, the point to remember is that Perl lets. Elaboration. comes later. A two dimensional grid of cells is used; each cell can be. The grid is updated in in accordance with some. At each turn, lonely cells (ones with fewer than. For each cell in the current array, you.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |