Lexical filehandles must be simple scalars (not eg hash elements) ?

R

Richard Gration

Hi All,

I've happened on this situation and I'm wondering if I'm correct. The code
below illustrates my point, in the sub log_write. The commented out print
line (#1) generates a "String found where operator expected" error, but I
don't see why it has to. I don't see why print #1 shouldn't parse and
indeed execute, it seems to be a deficiency in the parser. Can anybody
please explain?

Cheers
Rick

------------------------------------
#!/usr/bin/perl

use strict;
use warnings;

my $c = config();
log_write($c,'Start run');
log_write($c,'End run');

sub config {
my $config = {};
open my $fh,'>','log.log' or die "Pants: $!\n";
$config->{log_fh} = $fh;
$config->{log_to_file} = 1;
return $config;
}

sub log_write {
my ($config,@log) = @_;
return unless $config->{log_to_file};

# print #1
# This is what I would like to be able to do
# but it generates an error
# print $config->{log_fh} "$_\n" for @log;

# print #2
# This is what I must do
my $fh = $config->{log_fh};
print $fh "$_\n" for @log;
}
 
X

xhoster

Richard Gration said:
Hi All,

I've happened on this situation and I'm wondering if I'm correct.

Don't wonder. Just read the documentation for "print".

Xho
 
A

Anno Siegel

Richard Gration said:
Hi All,

I've happened on this situation and I'm wondering if I'm correct. The code
below illustrates my point, in the sub log_write. The commented out print
line (#1) generates a "String found where operator expected" error, but I
don't see why it has to. I don't see why print #1 shouldn't parse and
indeed execute, it seems to be a deficiency in the parser. Can anybody
please explain?

Cheers
Rick

------------------------------------
#!/usr/bin/perl

use strict;
use warnings;

my $c = config();
log_write($c,'Start run');
log_write($c,'End run');

sub config {
my $config = {};
open my $fh,'>','log.log' or die "Pants: $!\n";
$config->{log_fh} = $fh;
$config->{log_to_file} = 1;
return $config;
}

sub log_write {
my ($config,@log) = @_;
return unless $config->{log_to_file};

# print #1
# This is what I would like to be able to do
# but it generates an error
# print $config->{log_fh} "$_\n" for @log;

If the file handle isn't a simple variable, the simplest thing is to put
block braces around it (untested):

print { $config->{log_fh} } "$_\n" for @log;
# print #2
# This is what I must do
my $fh = $config->{log_fh};
print $fh "$_\n" for @log;
}

That's another possibility. The last paragraph of "perldoc -f print"
points this out.

The parser has a hard time with things in the "indirect object slot".
It must parse what could be the concatenation of two expressions.
Without a comma (or other keyword) to indicate the end of the first
one, only very simple expressions can be parsed.

Anno
 
T

Todd W

Anno Siegel said:
If the file handle isn't a simple variable, the simplest thing is to put
block braces around it (untested):

print { $config->{log_fh} } "$_\n" for @log;


That's another possibility. The last paragraph of "perldoc -f print"
points this out.

The parser has a hard time with things in the "indirect object slot".
It must parse what could be the concatenation of two expressions.
Without a comma (or other keyword) to indicate the end of the first
one, only very simple expressions can be parsed.

Thats why I like to use standard method notation:

becaue $fh is an IO::File object, you can just say:

$fh->print( ... );

from the example, I'd use:

looks alot "OO"yer to me ;0)

Todd W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top