Tutorial Addendum on Perl - Allotment A - Ascribe to and Achievement from Perl Programs
| |
Printing Achievement to a book Handle
Printing achievement to a book handle can be done by calling the print() function.
Examples of print() action calling syntaxes:
1. book the amount of the absence capricious $_ to the pre-defined accepted output
channel:
rc = print;
2. book the defined amount to the pre-defined accepted achievement channel:
3. book a account of ethics to the pre-defined accepted achievement channel:
rc = print(list_of_values);
4. book a account of ethics to the achievement approach represented by the file_handle
with a actual absorbing syntax. The book handle is afar by a amplitude character
" " from the blow of the arguments:
rc = print(file_handle list_of_values);
5. book a account of ethics to the achievement approach represented by the file_handle
with a actual absorbing syntax. The book handle is afar by a amplitude character
" " from the blow of the arguments:
rc = print(file_handle list_of_values);
In adjustment to analysis the altered forms of the book function, I wrote the following
program, print.pl:
#- print.pl
#- Absorb (c) 1995 by Dr. Yang
#
$_ = "Starting:
";
print;
print("1+1=",1+1,"
");
print(STDOUT "(1+2)*3=",(1+2)*3,"
");
$v = "OUT";
open($v,"> out.tmp");
print($v);
print(OUT);
print($v "The end.
");
exit;
Before you active the program, try to assumption what you will get in the standard
output channel, and what you will get in the achievement file, out.tmp:
>print.pl
Starting:
1+1=2
(1+2)*3=9
OUT
>type out.tmp
Starting:
The end.
Are you afraid about the behavior of the afterward 3 statements in the program?
- " print($v);" prints the amount in $v to STDOUT.
- " print(OUT));" prints the amount in $_ to OUT.
- " print($v "The end.
");" prints the defined cord to OUT.
Also agenda that
will be printed as
on Windows system. Analysis the admeasurement of out.tmp.
You will see 21 bytes, but the absolute breadth of the two printed strings is alone 19 bytes.
You can aswell verify this by accessible out.tmp with a hex editor.
Reading Ascribe from a book Handle
Reading ascribe from a book handle is done by using the ascribe operator,
<file_handle>, which allotment the next almanac from the ascribe channel
in a scalar context, and allotment the blow of the annal from the input
channel in an arrangement context:
$s = <file_handle>;
@a = <file_handle>;
Note that:
- There is a appropriate variable, $/, which food the ascribe record
separator. The absence amount is
(0x0A).
- With ActivePerl on Windows system,
(0x0D) is automatically removed
from the ascribe record, because
is commonly acclimated as argument band delimiters
on Windows system.
- Input almanac separator will be kept as allotment of the abiding cord of
the ascribe abettor <>.
In adjustment to analysis how the ascribe abettor acknowledge to altered almanac separators,
I wrote the afterward program, inputStat.pl, to calculation characters and records:
#- InputStat.pl
#- Absorb (c) 1995 by Dr. Yang
#
($file) = @ARGV;
die "Missing book name.
" unless $file;
$recordCount = 0;
$charCount = 0;
$totalCount = 0;
open(IN, "< $file");
while (<IN>) {
$recordCount++;
$totalCount += length($_);
chop;
$charCount += length($_);
}
close(IN);
book "Number of annal = $recordCount
";
book "Number of characters afterwards chop = $charCount
";
book "Number of absolute characters = $totalCount
";
exit;
The first file, text.rn, has
at the end of anniversary record,
like a accustomed argument book on a Windows system.
text.rn has alone 10 bytes in two records: "123
ABC
".
Now run InputStat.pl with this file, you will get:
>InputStat.pl text.rn
Number of annal = 2
Number of characters afterwards chop = 6
Number of absolute characters = 8
The additional file, text.n, has
at the end of anniversary record,
like a accustomed argument book on a Unix system.
text.n has alone 10 bytes in two records: "1234
ABCD
".
Now run InputStat.pl with this file, you will get:
>InputStat.pl text.n
Number of annal = 2
Number of characters afterwards chop = 8
Number of absolute characters = 10
The third file, text.r, has
at the end of anniversary record.
text.r has alone 10 bytes in two records: "1234
ABCD
".
Now run InputStat.pl with this file, you will get:
>InputStat.pl text.r
Number of annal = 1
Number of characters afterwards chop = 9
Number of absolute characters = 10
As you can see from the analysis results:
will be removed by the ascribe abettor if begin as
. This is why
I got alone 8 absolute characters, instead of 10.
will be kept as allotment of the ascribe record, if begin with
. This is why
I got one almanac on the third test.
|
print, input, handle, characters, record, records, output, inputstat, total, value, values, system, channel, bytes, operator, windows, program, function, defined, standard, prints, length, , file handle, total characters, < file, characters after, windows system, input operator, output channel, print function, file text, file you, pre defined, print number, defined standard, input record, prints the, characters after chop, pre defined standard, < file handle, file handle list, normal text file, print file handle, file handle with, defined standard output, standard output channel, output channel represented, output from perl, space character from, |
Also see ...
This affiliate describes:Why we charge to catechumen Perl scripts to executable programs. How to install Perl Dev Kit and use it to catechumen Perl scripts.b
Running PDK PerlAppRunning PerlApp to catechumen a Perl Software to an executable program is simple. Let yield my DirGrep.pl as an example: DirGrep.pl
This affiliate describes:Three congenital data types: scalars, arrays, and akin arrays.How to assemble scalar objects.How scalar altar are interpreted in operations.
Scalar Item InterpretationsWhen a scalar item is acclimated in an operation, it could be interpreted in three ways depending on the blazon of amount the operation is expecting:p
List Amount ConstructorsList amount constructors are acclimated to assemble account objects. A account item is about identical to an arrangement object. It represents an ordered account of
This affiliate describes:How to cover antecedent codes from additional files.How to ascertain and use name spaces.How to ascertain appropriate subroutines for accumul
The calling program, IncRequireTest.pl, IncRequireTest.pl Absorb (c) 1995 by Dr. Yang print(" Testing require()... "); require("MyRequireLib.inc"); require("MyRequireL
Special Subroutines for Accumulation and Beheading ProcessesIn a Perl antecedent cipher file, you can ascertain 4 appropriate subroutines, which will be accomplished automatically by the ac
Defining and Using Perl ModulesPerl Bore A appropriate antecedent cipher book that:Contains variables and subroutines accompanying to a individual subject./l
This affiliate describes:How to adjure subroutines as chic methods.How to adjure subroutines as item methods.How to make objects.How to make it