writing to file

H

Huub

Hi,

From what I've read I should be able to write to a file by just this:

$OUTPUT_FILE = UITBESTAND;
open($OUTPUT_FILE, ">UITBESTAND");
close($OUTPUT_FILE);

Yet, nothing is written so I assume I do something wrong. I just can't
make out what.

Thanks for helping.

Huub
 
M

Mark Clements

Huub said:
Hi,

From what I've read I should be able to write to a file by just this:

$OUTPUT_FILE = UITBESTAND;
open($OUTPUT_FILE, ">UITBESTAND");
close($OUTPUT_FILE);

Yet, nothing is written so I assume I do something wrong. I just can't make out what.

Quite a few things, though your main problem is you aren't writing anything.

use strict;
use warnings;

my $OUTFH;

open $OUTFH,">UITBESTAND" or die "could not open file: $!";
print $OUTFH "this is some text\n";
close $OUTFH or die "problem closing file: $!";

You need to:

read the posting guidelines
check out learn.perl.org

Mark
 
J

Josef Moellers

Huub said:
Hi,

From what I've read I should be able to write to a file by just this:

$OUTPUT_FILE = UITBESTAND;
open($OUTPUT_FILE, ">UITBESTAND");

Auto-vivifying of filehandles only works if the variable is undefined:

my $OUTPUT_FILE = undef;
open($OUTPUT_FILE, ">UITBESTAND");
print $OUTPUT_FILE "Hallo, beste mensen\n";
close $OUTPUT_FILE;

Also note that your code definitely doesn't compile under strict
(Bareword "UITBESTAND" not allowed while "strict subs"), so your posting
violates a major rule of this ng.
close($OUTPUT_FILE);

Yet, nothing is written so I assume I do something wrong. I just can't
make out what.

You don't write anything!

Please post a minimal non-working code sequence that compiles and shows
your problem.
 
B

Bart Lateur

Josef Moellers wrote:

Auto-vivifying of filehandles only works if the variable is undefined:

my $OUTPUT_FILE = undef;
open($OUTPUT_FILE, ">UITBESTAND");

Eh, right. Now the OP's code will use a global variable with the name of
"UITBESTAND" (in the current package) as a handle. It actually counts as
a symbolic reference.
 
M

Mark Clements

Bart said:
Josef Moellers wrote:



Eh, right. Now the OP's code will use a global variable with the name of
"UITBESTAND" (in the current package) as a handle. It actually counts as
a symbolic reference.

It's a lexical variable, not a global. This is a common way of opening a
file, though setting it to undef in the declaration is unneccessary.

Mark
 
J

Josef Moellers

Mark said:
Bart Lateur wrote:




It's a lexical variable, not a global. This is a common way of opening a
file, though setting it to undef in the declaration is unneccessary.

ACK, but
1. one never knows,
2. I have set it to undef explicitly to document the fact
3. see 1.

Josef
 
M

Mark Clements

Josef said:
ACK, but
1. one never knows,
2. I have set it to undef explicitly to document the fact
3. see 1.
Understood :), though I also neglected to mention that it's not
a symbolic reference.

Mark
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top