ower_own_perl_wiki
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| ower_own_perl_wiki [2007/09/25 21:43] – roumani | ower_own_perl_wiki [2008/01/31 21:29] (current) – typo fix cs243050 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Our Own Perl Wiki ====== | ====== Our Own Perl Wiki ====== | ||
| + | |||
| + | |||
| + | |||
| ===== Differences from Java ===== | ===== Differences from Java ===== | ||
| - | * Type: Variables are either **scalars** prefixed by **$**; **arrays** of scalars prefixed by **@**; or **hashes** of scalar pairs prefixed by **%**. | + | * Type: Variables are either **scalars** prefixed by **$**, **arrays** of scalars prefixed by **@**, or **hashes** of scalar pairs prefixed by **%**. |
| - | * Operators: same as Java except **eq**, **gt**, ... for strings. | + | * Operators: same as Java except **eq**, **gt**, **lt**, ... for string comparisons. |
| - | * Control Structures (selection and iteration): same plus **foreach** for hashes. | + | * Control Structures (selection and iteration): same as Java plus **foreach** for hashes. |
| - | * Cool stuff: interpolation, | + | * Cool stuff: interpolation, |
| ---- | ---- | ||
| + | |||
| + | ===== File I/O in Perl ===== | ||
| + | **Default file handlers:** | ||
| + | * STDIN: Standard input from console | ||
| + | * STDOUT: Standard output to console | ||
| + | * STDERR: Standard error output to console | ||
| + | |||
| + | **Opening a file** | ||
| + | <code perl> | ||
| + | open(FILEHANDLE, | ||
| + | </ | ||
| + | Where FILEHANDLE specifes the name for the file handler in which you will refer to your file and filename specifies the location of your file. | ||
| + | |||
| + | In the file name one can specify the various read/write modes. | ||
| + | ^ Example | ||
| + | | open(FILEHANDLE, | ||
| + | | open(FILEHANDLE, | ||
| + | | open(FILEHANDLE, | ||
| + | | open(FILEHANDLE, | ||
| + | | open(FILEHANDLE, | ||
| + | | open(FILEHANDLE, | ||
| + | |||
| + | File locking for I/O is provided using the **flock()** function. It is suggested to do the lock on a seperate file to prevent race conditions. | ||
| + | <code perl> | ||
| + | open(LOCK, "> | ||
| + | flock(LOCK, LOCK_EX); | ||
| + | open(DATA, myFile); | ||
| + | #...do work with the DATA file... | ||
| + | close LOCK; | ||
| + | close DATA; | ||
| + | </ | ||
| + | Closing the **LOCK** handler automatically unlocks the semaphore. One can also use **flock(LOCK, | ||
| + | ^ Operation | ||
| + | | LOCK_SH | ||
| + | | LOCK_EX | ||
| + | | LOCK_UN | ||
| + | | LOCK_NB | ||
ower_own_perl_wiki.1190756616.txt.gz · Last modified: by roumani
