I'm trying to wrap my head around the way Perl handles the parsing of arguments to print
.
Why does this
print $fh $stufftowrite
write to the file handle as expected, but
print($fh, $stufftowrite)
writes the file handle to STDOUT
instead?
My guess is that it has something to do with the warning in the documentation of print
:
Be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print; put parentheses around all arguments (or interpose a + , but that doesn't look as good).
Should I just get used to the first form (which just doesn't seem right to me, coming from languages that all use parentheses around function arguments), or is there a way to tell Perl to do what I want?
So far I've tried a lot of combination of parentheses around the first, second and both parameters, without success.
Step 1: Opening 2 files Source. txt in read mode and Destination. txt in write mode. Step 2: Reading the content from the FHR which is filehandle to read content while FHW is a filehandle to write content to file.
You need to print the $_ variable, or assign it to a defined variable as you are doing in the second code. You could rewrite the first: print; And it would work, because print uses $_ if you don't give it anything.
In Perl, a FileHandle associates a name to an external file, that can be used until the end of the program or until the FileHandle is closed.
The structure bareword (LIST1), LIST2
means "apply the function bareword
to the arguments LIST1
", while bareword +(LIST1), LIST2
can, but doesn't neccessarily mean "apply bareword
to the arguments of the combined list LIST1, LIST2
". This is important for grouping arguments:
my ($a, $b, $c) = (0..2);
print ($a or $b), $c; # print $b
print +($a or $b), $c; # print $b, $c
The prefix +
can also be used to distinguish hashrefs from blocks, and functions from barewords, e.g. when subscripting an hash: $hash{shift}
returns the shift
element, while $hash{+shift}
calls the function shift
and returns the hash element of the value of shift
.
In object oriented Perl, you normally call methods on an object with the arrow syntax:
$object->method(LIST); # call `method` on `$object` with args `LIST`.
However, it is possible, but not recommended, to use an indirect notation that puts the verb first:
method $object (LIST); # the same, but stupid.
Because classes are just instances of themselves (in a syntactic sense), you can also call methods on them. This is why
new Class (ARGS); # bad style, but pretty
is the same as
Class->new(ARGS); # good style, but ugly
However, this can sometimes confuse the parser, so indirect style is not recommended.
But it does hint on what print does:
print $fh ARGS
is the same as
$fh->print(ARGS)
Indeed, the filehandle $fh
is treated as an object of the class IO::Handle
.
(While this is a valid syntactic explanation, it is not quite true. The source of IO::Handle
itself uses the line print $this @_;
. The print function is just defined this way.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With