Perl install guide


















In addition, home quotas are much lower than quotas for project directories. The following instructions are for a few commonly used software packages to help users install these packages without admin intervention.

Users can install R packages using the install. User can specify other directories to which they have access privileges, such as project directory. Installing R packages in a project directory is recommended as users working on the same project have access to the same environment.

To install A3 package issue the install. To see the library paths, issue. Sometimes even this will fail, saying that the module is already installed. In that case, you may need to type something like "install 1 -force -nofollow".

The PPM method for installing software is described above. In order for this to work, you need a "make" tool. Microsoft provides "nmake" for this purpose. I download and install the "Windows Essentials x86 " binary distribution. Then from the Windows command shell, you can type "mysql" and get in to the database directly. You could download a binary distribution of Apache for Windows from apache. However, in the Apache Win32 world it is particularly a good idea to use the latest version, for bug and security fixes.

If you have any Linux systems, you probably want to run the Subversion server software there. However, if you really want to set up Subversion as a server on a Windows machine, here are some links I found.

For more information on module installation, please visit the detailed CPAN module installation guide. Shortly we will see how we can assign values to arrays and hashes. A scalar is a single unit of data. That data might be an integer number, floating point, a character, a string, a paragraph, or an entire web page. Simply saying it could be anything, but only a single thing.

An array is a variable that stores an ordered list of scalar values. Other Perl will understand it as a variable and will print its value. To refer to a single element of a hash, you will use the hash variable name followed by the "key" associated with the value in curly brackets. Perl treats same variable differently based on Context, i. First we copied it into anyother array, i.

Next we used the same array and tried to store this array in a scalar, so in this case it returned just the number of elements in this array assuming that context is scalar context.

Boolean context is simply any place where an expression is being evaluated to see whether it's true or false.

This context not only doesn't care what the return value is, it doesn't even want a return value. A scalar is most often either a number or a string. Following example demonstrates the usage of various types of string scalars.

You will see a detail of various operators available in Perl in a separate chapter, but here we are going to list down few numeric and string operations. A literal of the form v1. This form is known as v-strings. They are any literal that begins with a v and is followed by one or more dot-separated elements. So far you must have a feeling about string scalars and its concatenation and interpolation opration.

They may be used only as separate tokens and will not be interpolated into strings. In Perl, List and Array terms are often used as if they're interchangeable.

But the list is the data, and the array is the variable. In this example, this leads to a four-element array; the first element is 'this' and last fourth is 'array'.

Array indices start from zero, so to access the first element you need to give 0 as indices. You can also give a negative index, in which case you select the element from the end, rather than the beginning, of the array.

Perl offers a shortcut for sequential numbers and letters. Here double dot.. The value returned will always be the physical size of the array, not the number of valid elements. There are only four elements in the array that contains information, but the array is 51 elements long, with a highest index of Perl provides a number of useful functions to add and remove elements in an array. You may have a question what is a function? So far you have used print function to print various values.

Similarly there are various other functions or sometime called sub-routines, which can be used for various other functionalities. Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. You can also extract a "slice" from an array - that is, you can select more than one item from an array in order to produce another array. The specification for a slice must have a list of valid indices, either positive or negative, each separated by a comma.

For speed, you can also use the.. Finally, it returns the elements removed from the array. Here, the actual replacement begins with the 6th number after that five elements are then replaced from 6 to 10 with the numbers 21, 22, 23, 24 and This function splits a string into an array of strings, and returns it.

We can use the join function to rejoin the array elements and form one long scalar string. This function sorts the LIST and returns the sorted array value. So the best option is to first transform every element of the array into lowercase letters and then perform the sort function.

The list notation is identical to that for arrays. Hashes are created in one of the two following ways. In the second case, you use a list, which is converted by taking individual pairs from the list: the first element of the pair is used as the key, and the second, as the value. But it is important to note that there is a single word, i.

You can extract slices of a hash just as you can extract slices from an array. This function returns an array of all the keys of the named hash. Similarly, you can use values function to get a list of all the values. This function returns a normal array consisting of all the values of the named hash. Here we have introduced the IF ELSE statement, which we will study in a separate chapter.

For now you just assume that if condition part will be executed only when the given condition is true otherwise else part will be executed. You can get the size - that is, the number of elements from a hash by using the scalar context on either keys or values. Perl conditional statements helps in the decision making, which require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

The number 0, the strings '0' and "" , the empty list , and undef are all false in a boolean context and all other values are true. Negation of a true value by! An if statement can be followed by an optional elsif statement and then by an optional else statement. An unless statement can be followed by an optional elsif statement and then by an optional else statement. With the latest versions of Perl, you can make use of the switch statement. Let's check the conditional operator?

The value of a? If it is true, then Exp2 is evaluated and becomes the value of the entire? If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression. There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Programming languages provide various control structures that allow for more complicated execution paths. Perl programming language provides the following types of loop to handle the looping requirements.

Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. Repeats a statement or group of statements until a given condition becomes true.

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. Loop control statements change the execution from its normal sequence.

When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. Terminates the loop statement and transfers execution to the statement immediately following the loop. The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed.

A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.

When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but as a programmer more commonly use the for ;; construct to signify an infinite loop. Show Example. These are also called relational operators. Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. Checks if the value of two operands are equal or not, and returns -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument.

Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. Below is a list of equity operators. Returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument.

Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand.

Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand. Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand. Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand.

Exponent AND assignment operator, Performs exponential power calculation on operators and assign value to the left operand. Bitwise operator works on bits and perform bit by bit operation. Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.

Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. There are following logical operators supported by Perl language. Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

There are following Quote-like operators supported by Perl language. There are following miscellaneous operators supported by Perl language. The repetition operator x returns a string consisting of the left operand repeated the number of times specified by the right operand. The range operator.. The arrow operator is mostly used in dereferencing a method or variable from an object or a class name.

This chapter will give you the basic understanding on how to process and manipulate dates and times in Perl. Let's start with localtime function, which returns values for the current date and time if given no arguments.

If you will use localtime function in scalar context, then it will return date and time from the current time zone set in the system. The function gmtime works just like localtime function but the returned values are localized for the standard Greenwich time zone. You should make a note on the fact that localtime will return the current local time on the machine that runs the script and gmtime will return the universal Greenwich Mean Time, or GMT or UTC.

You can use the time function to get epoch time, i. You can use the POSIX function strftime to format date and time with the help of the following table.

A Perl subroutine or function is a group of statements that together performs a task. You can divide up your code into separate subroutines. How you divide up your code among different subroutines is up to you, but logically the division usually is so each function performs a specific task.

In versions of Perl before 5. This still works in the newest versions of Perl, but it is not recommended since it bypasses the subroutine prototypes. Let's have a look into the following example, which defines a simple function and then call it. Because Perl compiles your program before executing it, it doesn't matter where you declare your subroutine.

You can pass arrays and hashes as arguments like any scalar but passing more than one array or hash normally causes them to lose their separate identities. So we will use references explained in the next chapter to pass any array or hash. You can return a value from subroutine like you do in any other programming language. If you are not returning a value from a subroutine then whatever calculation is last performed in a subroutine is automatically also the return value.

You can return arrays and hashes from the subroutine like any scalar but returning more than one array or hash normally causes them to lose their separate identities. So we will use references explained in the next chapter to return any array or hash from a function. By default, all variables in Perl are global variables, which means they can be accessed from anywhere in the program.

But you can create private variables called lexical variables at any time with the my operator. The my operator confines a variable to a particular region of code in which it can be used and accessed. Outside that region, this variable cannot be used or accessed. This region is called its scope. A lexical scope is usually a block of code with a set of braces around it, such as those defining the body of the subroutine or those marking the code blocks of if, while, for, foreach, and eval statements.

The local is mostly used when the current value of a variable must be visible to called subroutines. A local just gives temporary values to global meaning package variables. This is known as dynamic scoping.

Lexical scoping is done with my, which works more like C's auto declarations. If more than one variable or expression is given to local, they must be placed in parentheses.

This operator works by saving the current values of those variables in its argument list on a hidden stack and restoring them upon exiting the block, subroutine, or eval.

There are another type of lexical variables, which are similar to private variables but they maintain their state and they do not get reinitialized upon multiple calls of the subroutines. These variables are defined using the state operator and available starting from Perl 5.

The context of a subroutine or statement is defined as the type of return value that is expected. This allows you to use a single function that returns different values based on what the user is expecting to receive. For example, the following localtime returns a string when it is called in scalar context, but it returns a list when it is called in list context.

Now the individual variables contain the corresponding values returned by localtime subroutine. A Perl reference is a scalar data type that holds the location of another value which could be scalar, arrays, or hashes.

Because of its scalar nature, a reference can be used anywhere, a scalar can be used. You can construct lists containing references to other lists, which can contain references to hashes, and so on. This is how the nested data structures are built in Perl. Dereferencing returns the value from a reference point to the location. If you are not sure about a variable type, then its easy to know its type using ref , which returns one of the following strings if its argument is a reference.

A circular reference occurs when two references contain a reference to each other. You have to be careful while creating references otherwise a circular reference can lead to memory leaks. Perl uses a writing template called a 'format' to output reports. To use the format feature of Perl, you have to define a format first and then you can use that format to write formatted data. Here FormatName represents the name of the format. The fieldline is the specific way, the data should be formatted.

The values lines represent the values that will be entered into the field line. You end the format with a single period. Next fieldline can contain any text or fieldholders.

The fieldholders hold space for data that will be placed there at a later date. This fieldholder is left-justified, with a field space of 5. The problem is that the format name is usually the name of an open file handle, and the write statement will send the output to this file handle.

Remember: if you are going to write your report in any other file handle instead of STDOUT then you can use select function to select that file handle and rest of the logic will remain the same.

Let's take the following example. Here we have hard coded values just for showing the usage. In actual usage you will read values from a file or database to generate actual reports and you may need to write final report again into a file.

Everything looks fine. But you would be interested in adding a header to your report. This header will be printed on top of each page. It is very simple to do this. What about if your report is taking more than one page? For a complete set of variables related to formating, please refer to the Perl Special Variables section.

The basics of handling files are simple: you associate a filehandle with an external entity usually a file and then use a variety of operators and functions within Perl to read and update the data stored within the data stream associated with the filehandle.

A filehandle is a named internal Perl structure that associates a physical file with a name. However, when you associate a filehandle, you can specify the mode in which the filehandle is opened. There are following two functions with multiple forms, which can be used to open any new or existing file in Perl. Following is the syntax to open file.

Here DATA is the file handle, which will be used to read the file. Here is the example, which will open a file and will print its content over the screen. This example actually truncates empties the file before opening it for writing, which may not be the desired effect. You can open a file in the append mode. In this mode, writing point will be set to the end of the file. By default it takes 0x To close a filehandle, and therefore disassociate the filehandle from the corresponding file, you use the close function.

This flushes the filehandle's buffers and closes the system's file descriptor. It returns true only if it could successfully flush the buffers and close the file. Once you have an open filehandle, you need to be able to read and write information. There are a number of different ways of reading and writing data into the file. In a scalar context, it returns a single line from the filehandle. The read function reads a block of information from the buffered filehandle: This function is used to read binary data from the file.

The function returns the number of bytes read on success, zero at end of file, or undef if there was an error. For all the different methods used for reading information from filehandles, the main function for writing information back is the print function.

Here is the example, which opens an existing file file1. Here is an example, which shows how we can rename a file file1. You can use to tell function to know the current position of a file and seek function to point a particular position inside the file.

The function uses the fseek system function, and you have the same ability to position relative to three different points: the start, the end, and the current position. Zero sets the positioning relative to the start of the file. For example, the line sets the file pointer to the th byte in the file. You can test certain features very quickly within Perl using a series of test operators known collectively as -X tests.

There are various ways to list down all the files available in a particular directory. Here is another example, which opens a directory and list out all the files available inside this directory. You can use mkdir function to create a new directory. You will need to have the required permission to create a directory. You can use rmdir function to remove a directory. You will need to have the required permission to remove a directory. Additionally this directory should be empty before you try to remove it.

You can use chdir function to change a directory and go to a new location. You will need to have the required permission to change a directory and go inside the new directory. The execution and the errors always go together. If you are opening a file which does not exist. The program stops if an error occurs.

So a proper error handling is used to handle various type of errors, which may occur during a program execution and take appropriate action instead of halting program completely. You can identify and trap an error in a number of different ways. Its very easy to trap errors in Perl and then handling them properly. Here are few methods which can be used.

The unless function is the logical opposite to if: statements can completely bypass the success status and only be executed if the expression returns false. The unless statement is best used when you want to raise an error or alternative only if the expression fails. It's not quite so clear here what we are trying to achieve, but the effect is the same as using an if or unless statement.

The conditional operator is best used when you want to quickly return one of the two values within an expression or statement. The die function works just like warn, except that it also calls exit. Within a normal script, this function has the effect of immediately terminating execution. Reporting an error in a module that quotes the module's filename and line number - this is useful when debugging a module, or when you specifically want to raise a module-related, rather than script-related, error.

Reporting an error within a module that quotes the caller's information so that you can debug the line within the script that caused the error. Errors raised in this fashion are useful to the end-user, because they highlight the error in relation to the calling script's origination line.

The warn and die functions work slightly differently than you would expect when called from within a module. This is more or less what you might expected, but not necessarily what you want. From a module programmer's perspective, the information is useful because it helps to point to a bug within the module itself. For an end-user, the information provided is fairly useless, and for all but the hardened programmer, it is completely pointless.

The solution for such problems is the Carp module, which provides a simplified method for reporting errors within modules that return information about the calling script. The Carp module provides four functions: carp, cluck, croak, and confess. These functions are discussed below. The carp function is the basic equivalent of warn and prints the message to STDERR without actually exiting the script and printing the script name.

The cluck function is a sort of supercharged carp, it follows the same basic principle but also prints a stack trace of all the modules that led to the function being called, including the information on the original script.

The croak function is equivalent to die , except that it reports the caller one level up. As with carp, the same basic rules apply regarding the including of line and file information according to the warn and die functions. The confess function is like cluck ; it calls die and then prints a stack trace all the way up to the origination script.

There are some variables which have a predefined and special meaning in Perl. Most of the special variables have an english like long name, e.



0コメント

  • 1000 / 1000