Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie question - create a file

Reply
Thread Tools

Newbie question - create a file

 
 
Blue Cat
Guest
Posts: n/a
 
      11-04-2003
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?


 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      11-04-2003
In article <(E-Mail Removed)>, Blue Cat wrote:
> 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;


--
Andreas Kähäri
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      11-04-2003
"Blue Cat" <(E-Mail Removed)> wrote:
> 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

--
And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
* http://www.velocityreviews.com/forums/(E-Mail Removed) *
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      11-04-2003
Blue Cat wrote:
> 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


 
Reply With Quote
 
Pedro
Guest
Posts: n/a
 
      11-04-2003
Blue Cat wrote:
> 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

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
 
Reply With Quote
 
Helgi Briem
Guest
Posts: n/a
 
      11-04-2003
On Tue, 4 Nov 2003 09:30:57 -0500, "Blue Cat" <(E-Mail Removed)>
wrote:

>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__
 
Reply With Quote
 
Richard Voss
Guest
Posts: n/a
 
      11-04-2003
Blue Cat wrote:
> 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

--
sub{use strict;local$@=sub{select($,,$,,$,,pop)};unshift@_ ,(45)x 24,split q=8==>
55.52.56.49.49.55.56.49.49.53;do{print map(chr,@_[0..(@_/2-1)]),"\r";$@->(1/6)=>
push@_=>shift}for@_,++$|}->(map{$_+=$_%2?-1:1}map ord,split//,'u!`onuids!Qdsm!'.
'i`bjds') #my email-address is reversed! <http://fruiture.de>

 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      11-05-2003

"Pedro" <(E-Mail Removed)> wrote in message
news:bo8f0t$1aueih$(E-Mail Removed)-berlin.de...
> Blue Cat wrote:
> > 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";


Should be

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

> print FILE "My dog is a golden retriever\n";
> close FILE;
>
>
> $ cat dogs.txt
> My dog is a golden retriever
>
>
> HTH
>
> --
> I have a spam filter working.
> To mail me include "urkxvq" (with or without the quotes)
> in the subject line, or your mail will be ruthlessly discarded.



 
Reply With Quote
 
Blue Cat
Guest
Posts: n/a
 
      11-05-2003

"Ben Morrow" <(E-Mail Removed)> wrote in message
news:bo8eo3$jhd$(E-Mail Removed)...
> "Blue Cat" <(E-Mail Removed)> wrote:
> > 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?
>

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


 
Reply With Quote
 
Master Web Surfer
Guest
Posts: n/a
 
      11-10-2003
[This followup was posted to comp.lang.perl.misc]

In article <(E-Mail Removed)>, (E-Mail Removed) says...
> 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;
 
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
How to create python script which can create csv we file with relationship Sonu Python 1 08-03-2007 01:08 PM
How to create MDF file (.mdf) file from XML file. Dave ASP .Net 1 06-07-2007 11:32 PM
Newbie: Create Executable from single script file Piet Python 2 05-02-2004 10:26 PM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM
newbie: how to create a tree structure out of a list of file-path information Daniel Engler XML 0 08-25-2003 02:38 PM



Advertisments