N
niall.macpherson
I have a sub routine which is passed a reference to a scalar. It opens
a file for writing and sets the scalar passed to the value of the
filehandle produced by the open.
I then print to the file from the calling function. This works as
expected
print $newfh 'somestring ' , $strs[0], ' another ' , $strs[1], "\n";
I assumed I should be able to use the same syntax to print to the file
within the subroutine , although of course I need to dereference the
filehandle using $$. However this gave me an error
String found where operator expected at
C:\develop\NiallPerlScripts\printtest1.p
l line 18, near "$r_newfh 'in function line 1 '"
(Missing operator before 'in function line 1 '?)
The print only succeeds if I enclose all the arguments in parentheses.
The parentheses are not required in the calling function
Sorry if this is not crystal clear - Here is a complete program which
demonstrates the problem
----------------------------------------------------------------------------------------------------------------------
use strict;
use warnings;
##------------------------------------------------
sub OpenAndPrint
{
my ($r_newfh, $text) = @_;
open ( $$r_newfh , '>', 'testout.txt' ) or die "Could not open file
$!";
### This print gives error
### String found where operator expected at
### C:\develop\NiallPerlScripts\printtest1.pl
### line 18, near "$r_newfh ' in function line 1 '"
### (Missing operator before 'in function line 1 '?)
#print $$r_newfh 'in function line 1 ' , $text , "\n";
### This print works as expected
print $$r_newfh ('in function line 2 ' , $text , "\n");
return;
}
##------------------------------------------------
my @strs = qw(lmn opq rst);
my $newfh;
OpenAndPrint(\$newfh, 'passed string');
## Both prints here work as expected
print $newfh 'somestring ' , $strs[0], ' another ' , $strs[1], "\n";
print $newfh ('yetmore ' , $strs[2]);
close $newfh or die "Could not close file $!";
exit(0);
------------------------------------------------------------------------------------------------------------------
I presume it is something to do with the parantheses forcing list
context but I don't understand why the behaviour is different depending
on whether I am using a file handle or a reference to a file handle.
Can someone explain ?
TIA
Niall
a file for writing and sets the scalar passed to the value of the
filehandle produced by the open.
I then print to the file from the calling function. This works as
expected
print $newfh 'somestring ' , $strs[0], ' another ' , $strs[1], "\n";
I assumed I should be able to use the same syntax to print to the file
within the subroutine , although of course I need to dereference the
filehandle using $$. However this gave me an error
String found where operator expected at
C:\develop\NiallPerlScripts\printtest1.p
l line 18, near "$r_newfh 'in function line 1 '"
(Missing operator before 'in function line 1 '?)
The print only succeeds if I enclose all the arguments in parentheses.
The parentheses are not required in the calling function
Sorry if this is not crystal clear - Here is a complete program which
demonstrates the problem
----------------------------------------------------------------------------------------------------------------------
use strict;
use warnings;
##------------------------------------------------
sub OpenAndPrint
{
my ($r_newfh, $text) = @_;
open ( $$r_newfh , '>', 'testout.txt' ) or die "Could not open file
$!";
### This print gives error
### String found where operator expected at
### C:\develop\NiallPerlScripts\printtest1.pl
### line 18, near "$r_newfh ' in function line 1 '"
### (Missing operator before 'in function line 1 '?)
#print $$r_newfh 'in function line 1 ' , $text , "\n";
### This print works as expected
print $$r_newfh ('in function line 2 ' , $text , "\n");
return;
}
##------------------------------------------------
my @strs = qw(lmn opq rst);
my $newfh;
OpenAndPrint(\$newfh, 'passed string');
## Both prints here work as expected
print $newfh 'somestring ' , $strs[0], ' another ' , $strs[1], "\n";
print $newfh ('yetmore ' , $strs[2]);
close $newfh or die "Could not close file $!";
exit(0);
------------------------------------------------------------------------------------------------------------------
I presume it is something to do with the parantheses forcing list
context but I don't understand why the behaviour is different depending
on whether I am using a file handle or a reference to a file handle.
Can someone explain ?
TIA
Niall