Newbie question - create a file

Discussion in 'Perl Misc' started by Blue Cat, Nov 4, 2003.

  1. Blue Cat

    Blue Cat Guest

    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?
     
    Blue Cat, Nov 4, 2003
    #1
    1. Advertising

  2. In article <>, 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
     
    Andreas Kahari, Nov 4, 2003
    #2
    1. Advertising

  3. Blue Cat

    Ben Morrow Guest

    "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 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)
    * *
     
    Ben Morrow, Nov 4, 2003
    #3
  4. 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
     
    Jürgen Exner, Nov 4, 2003
    #4
  5. Blue Cat

    Pedro Guest

    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.
     
    Pedro, Nov 4, 2003
    #5
  6. Blue Cat

    Helgi Briem Guest

    On Tue, 4 Nov 2003 09:30:57 -0500, "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?


    #!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__
     
    Helgi Briem, Nov 4, 2003
    #6
  7. Blue Cat

    Richard Voss Guest

    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>
     
    Richard Voss, Nov 4, 2003
    #7
  8. Blue Cat

    Tintin Guest

    "Pedro" <> wrote in message
    news:bo8f0t$1aueih$-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.
     
    Tintin, Nov 5, 2003
    #8
  9. Blue Cat

    Blue Cat Guest

    "Ben Morrow" <> wrote in message
    news:bo8eo3$jhd$...
    > "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 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
     
    Blue Cat, Nov 5, 2003
    #9
  10. [This followup was posted to comp.lang.perl.misc]

    In article <>, 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;
     
    Master Web Surfer, Nov 10, 2003
    #10
  11. Blue Cat

    Brad Baxter Guest

    On Mon, 10 Nov 2003, Master Web Surfer wrote:

    > #!/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
     
    Brad Baxter, Nov 10, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Engler
    Replies:
    0
    Views:
    834
    Daniel Engler
    Aug 25, 2003
  2. Piet
    Replies:
    2
    Views:
    334
  3. Dave
    Replies:
    1
    Views:
    996
    Juan T. Llibre
    Jun 8, 2007
  4. Sonu
    Replies:
    1
    Views:
    806
  5. Jerry C.
    Replies:
    8
    Views:
    244
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page