Prev | Current Page 148 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"


Perl has also become popular for general-purpose programming, and the languages PHP, Ruby, and Python are
other languages useful for larger applications.
In general, these scripting languages are interpreted, so execution speed is not their main attraction. Instead,
scripting languages offer a compact, simplified syntax that speeds program development. In many cases, this
write-time advantage outweighs any run-time disadvantage. This is especially true for one-time systems tasks
like reformatting a text file or interpreting a system activity log.
Suppose one wanted to print a copy of a file, with line numbers added to the front of each line. Here??™s
a Perl program called lineNumberFile.pl to do that; run it by typing
>lineNumberFile.pl < fileToPrint.
#!/usr/local/bin/perl # Location of Perl interpreter
#
# Program to open the source file, read it in,
# print it with line numbers, and close it again.
$file = $0; # $0, the 1st cmd line arg
print "$0\n"; # Prints name of file & newLine
open(INFO, $file); # Open the file
while($line = ) # Read lines of file
{ # Add line number
print " $. ". "$line"; # $. has the input line no
}
close(INFO); # Close the file
>
That??™s only seven lines of code, and the job is done.


Pages:
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
gry on line życzenia ślubne gustowne meble katowice felgi aluminiowe poznań katalog firm