Newbie question - create a file

B

Blue Cat

After toiling over "open" in the Perl docs and the Camel Book with no
success, I am asking for help:

How do I create a file named "dogs.txt" and write "My dog is a golden
retriever." into it?
 
A

Andreas Kahari

After toiling over "open" in the Perl docs and the Camel Book with no
success, I am asking for help:

How do I create a file named "dogs.txt" and write "My dog is a golden
retriever." into it?

open FH, ">dogs.txt" or die "Could not open file: $!";
print FH "My dog is a golden retriever\n";
close FH;
 
B

Ben Morrow

Blue Cat said:
After toiling over "open" in the Perl docs and the Camel Book with no
success, I am asking for help:

How do I create a file named "dogs.txt" and write "My dog is a golden
retriever." into it?

open my $DOGS, "> dogs.txt" or die "can't open dogs.txt: $!";
print $DOGS "My dog is a golden retriever.";

What did you try, and in what way did it fail?

Ben
 
J

Jürgen Exner

Blue said:
How do I create a file named "dogs.txt" and write "My dog is a golden
retriever." into it?

Hi Blue

Quite simple

use warnings;
use strict;
my $F;
open F, ">dogs.txt" or die "Cannot open 'dogs.txt':$!\n";
print F "My dog is a golden retriever.\n";
close F;

jue
 
P

Pedro

Blue said:
After toiling over "open" in the Perl docs and the Camel Book with no
success, I am asking for help:

How do I create a file named "dogs.txt" and write "My dog is a golden
retriever." into it?

Newbie answer:

$ cat dogs.pl
#!/usr/bin/perl
use strict;
use warnings;

open FILE, "> dogs.txt";
print FILE "My dog is a golden retriever\n";
close FILE;


$ cat dogs.txt
My dog is a golden retriever


HTH
 
H

Helgi Briem

After toiling over "open" in the Perl docs and the Camel Book with no
success, I am asking for help:

How do I create a file named "dogs.txt" and write "My dog is a golden
retriever." into it?

#!perl
use warnings;
use strict;
my $path = '/path/to/where/you/want/to/keep/file';
my $file = "$path/dogs.txt";
my $text = "My dog is a golden retriever.";

open OUT, ">", $file or die "Cannot open $file for writing:$!";
print OUT $text;
close OUT or die "Cannot close $file:$!";
__END__
 
R

Richard Voss

Blue said:
After toiling over "open" in the Perl docs and the Camel Book with no
success, I am asking for help:

How do I create a file named "dogs.txt" and write "My dog is a golden
retriever." into it?

Because it's so much fun, another version. I prefer it like that:

my $file = 'dogs.txt';

open my $fh,'>', $file
or die "Could not open '$file': $!\n";

print $fh "My dog is a golden retriever."

close $fh
or die "Could not write to '$file': $!\n";

See
$ perldoc -f open
$ perldoc -f close
$ perldoc perlopentut
 
T

Tintin

Pedro said:
Newbie answer:

$ cat dogs.pl
#!/usr/bin/perl
use strict;
use warnings;

open FILE, "> dogs.txt";

Should be

open FILE, ">dogs.txt" or die "Could not open dogs.txt because $!\n";
 
B

Blue Cat

Ben Morrow said:
open my $DOGS, "> dogs.txt" or die "can't open dogs.txt: $!";
print $DOGS "My dog is a golden retriever.";

What did you try, and in what way did it fail?
I had the syntax all messed up. I was using a number for a filehandle (like
in BASIC). The script would execute without error messages, but no file
would be created.

The advise from you and the others who answered helped me a great deal.
Thanks
 
M

Master Web Surfer

[This followup was posted to comp.lang.perl.misc]

After toiling over "open" in the Perl docs and the Camel Book with no
success, I am asking for help:

How do I create a file named "dogs.txt" and write "My dog is a golden
retriever." into it?

#!/usr/bin/perl -w

$filename = "dogs.txt";
open(GOLDEN,">$filename") or
die("Can't write \"$filename\" : $!\n");
print GOLDEN "My dog is a golden retriever.\n";
close GOLDEN;

exit 0;
 
B

Brad Baxter

#!/usr/bin/perl -w

$filename = "dogs.txt";
open(GOLDEN,">$filename") or
die("Can't write \"$filename\" : $!\n");
print GOLDEN "My dog is a golden retriever.\n";
close GOLDEN;

exit 0;

Don't forget:

use strict;

Regards,

Brad
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top