Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem using print with a reference to a filehandle

Reply
Thread Tools

Problem using print with a reference to a filehandle

 
 
niall.macpherson@ntlworld.com
Guest
Posts: n/a
 
      06-01-2006
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

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      06-01-2006
Thus spoke http://www.velocityreviews.com/forums/(E-Mail Removed) (on 2006-06-01 16:10):

> # print $$r_newfh 'in function line 1 ' , $text , "\n";

print {$$r_newfh} 'in function line 1 ' , $text , "\n";

.... works here.

Regards

Mirco
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      06-01-2006
(E-Mail Removed) wrote:
> 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 $!";


You are not passing a value filehandle to the sub so why not just return the
valid filehandle after creating it.

sub OpenAndPrint {
my ( $text ) = @_;
open my $fh, '>', 'testout.txt' or die "Could not open 'testout.txt' $!";
print $fh "in function line 2 $text\n";
return $fh;
}

my $newfh = OpenAndPrint( 'passed string' );



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
niall.macpherson@ntlworld.com
Guest
Posts: n/a
 
      06-02-2006

John W. Krahn wrote:


>
> You are not passing a value filehandle to the sub so why not just return the
> valid filehandle after creating it.
>



A good point - however in the real program where the function is far
more complex I would prefer not to return the value.

I guess it is a throwback to my days as a 'C' programmer, but I tend to
make most of my subs either return 0 (success) or -1 (failure) , and
any values I require are returned by populating a variable which I pass
by reference to the sub.

Must try to start thinking in Perl not C

 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      06-02-2006
On 2 Jun 2006, (E-Mail Removed) wrote:

> I guess it is a throwback to my days as a 'C' programmer, but I tend to
> make most of my subs either return 0 (success) or -1 (failure) , and
> any values I require are returned by populating a variable which I pass
> by reference to the sub.


It may be easier to return undef for failure. Then you can return 0
if needed, a file handle, or pretty much anything else as a valid
value. You just have to change "if !$result" to "if defined $result".

Ted
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function like print with optional filehandle? Steve Roscio Perl Misc 8 01-23-2009 04:34 PM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
use print to write binary data to a filehandle seems not correct quakewang@mail.whut.edu.cn Perl Misc 6 11-22-2006 06:30 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
Write to a filehandle and to STDOUT without double print statements? dnrg Perl Misc 5 06-26-2003 04:39 PM



Advertisments