write to file not working

Discussion in 'Perl' started by rxl124@hehe.com, Feb 12, 2004.

  1. Guest

    someone please please help w/ this one.
    As I been working on this on and off and it just does not want to work.

    1 #!/usr/bin/perl -w
    2
    3 $file = "/home/user1/dothis";
    4 open(FILE, ">$file");
    5 while($line = <FILE>) {
    6 if ($line =~ /B/) {print FILE "A"};
    7 if ($line =~ /A/) {print FILE "B"};
    8 }

    and when i run above program, i get below error,


    Filehandle FILE opened only for output at ./pro1 line 5.

    Can someone please tell me what I am doing wrong?
    Or is this bug?

    Please tell me.

    thanks
    , Feb 12, 2004
    #1
    1. Advertising

  2. Guest

    wrote in message news:<>...
    > someone please please help w/ this one.
    > As I been working on this on and off and it just does not want to work.
    >
    > 1 #!/usr/bin/perl -w
    > 2
    > 3 $file = "/home/user1/dothis";
    > 4 open(FILE, ">$file");
    > 5 while($line = <FILE>) {
    > 6 if ($line =~ /B/) {print FILE "A"};
    > 7 if ($line =~ /A/) {print FILE "B"};
    > 8 }
    >
    > and when i run above program, i get below error,
    >
    >
    > Filehandle FILE opened only for output at ./pro1 line 5.
    >
    > Can someone please tell me what I am doing wrong?


    Is this a trick question?

    According to the error message you are trying, at line 5, to read form
    filehandle FILE that you opened only for output. Looking at the code
    would indeed confirm that's what you are doing.

    > Please tell me.


    OK, other things you are doing wrong:

    Posting to a non existant newsgroup (see FAQ).

    Posting with a subject "write to file not working" and example code
    that's crashing out on a read from file.

    Not explaining what you expect you code to do (so we can't give you
    any idea how fix it).

    Not checking for errors from open().

    Not declaring all your variables as lexically scoped in the smallest
    applicable scope.
    , Feb 12, 2004
    #2
    1. Advertising

  3. Jeff Dunn Guest

    wrote in message news:<>...
    > someone please please help w/ this one.
    > As I been working on this on and off and it just does not want to work.
    >
    > 1 #!/usr/bin/perl -w
    > 2
    > 3 $file = "/home/user1/dothis";
    > 4 open(FILE, ">$file");
    > 5 while($line = <FILE>) {
    > 6 if ($line =~ /B/) {print FILE "A"};
    > 7 if ($line =~ /A/) {print FILE "B"};
    > 8 }
    >
    > and when i run above program, i get below error,
    >
    >
    > Filehandle FILE opened only for output at ./pro1 line 5.
    >
    > Can someone please tell me what I am doing wrong?
    > Or is this bug?
    >
    > Please tell me.
    >
    > thanks



    You are trying to read from a file you have requested to opened for writing.
    Try this
    open(FILE, "+<$file");
    Jeff Dunn, Feb 12, 2004
    #3
  4. Guest

    (Jeff Dunn) wrote in message news:<>...
    > wrote in message news:<>...
    > > someone please please help w/ this one.
    > > As I been working on this on and off and it just does not want to work.
    > >
    > > 1 #!/usr/bin/perl -w
    > > 2
    > > 3 $file = "/home/user1/dothis";
    > > 4 open(FILE, ">$file");
    > > 5 while($line = <FILE>) {
    > > 6 if ($line =~ /B/) {print FILE "A"};
    > > 7 if ($line =~ /A/) {print FILE "B"};
    > > 8 }
    > >
    > > and when i run above program, i get below error,
    > >
    > >
    > > Filehandle FILE opened only for output at ./pro1 line 5.
    > >
    > > Can someone please tell me what I am doing wrong?
    > > Or is this bug?
    > >
    > > Please tell me.
    > >
    > > thanks

    >
    >
    > You are trying to read from a file you have requested to opened for writing.
    > Try this
    > open(FILE, "+<$file");



    THANK YOU Jeff,

    w/ your help, I was able to write this little program which basically
    just does one thing. It will record which server I have to work on(no
    I can't do this w/ cron. as it has to be done manually and also i am
    trying to learn perl here).

    #!/usr/bin/perl -w
    open(FH, "+<server_watch") || die ;
    $count = <FH>;
    if ($count == 1) {$count=2 ;}
    else {$count=1;}
    seek(FH, 0,0) || die;
    print FH "$count";
    close(FH);

    Thank you so much!!!!!
    Also, if anyone knows that there are already written little scripts
    like this. please send me some URL. I like to look at other people's
    codes...... to learn.
    , Feb 13, 2004
    #4
  5. Guest

    (Jeff Dunn) wrote in message news:<>...
    > wrote in message news:<>...
    > > someone please please help w/ this one.
    > > As I been working on this on and off and it just does not want to work.
    > >
    > > 1 #!/usr/bin/perl -w
    > > 2
    > > 3 $file = "/home/user1/dothis";
    > > 4 open(FILE, ">$file");
    > > 5 while($line = <FILE>) {
    > > 6 if ($line =~ /B/) {print FILE "A"};
    > > 7 if ($line =~ /A/) {print FILE "B"};
    > > 8 }
    > >
    > > and when i run above program, i get below error,
    > >
    > >
    > > Filehandle FILE opened only for output at ./pro1 line 5.
    > >
    > > Can someone please tell me what I am doing wrong?
    > > Or is this bug?
    > >
    > > Please tell me.
    > >
    > > thanks

    >
    >
    > You are trying to read from a file you have requested to opened for writing.
    > Try this
    > open(FILE, "+<$file");



    THANK YOU Jeff,

    w/ your help, I was able to write this little program which basically
    just does one thing. It will record which server I have to work on(no
    I can't do this w/ cron. as it has to be done manually and also i am
    trying to learn perl here).

    #!/usr/bin/perl -w
    open(FH, "+<server_watch") || die ;
    $count = <FH>;
    if ($count == 1) {$count=2 ;}
    else {$count=1;}
    seek(FH, 0,0) || die;
    print FH "$count";
    close(FH);

    Thank you so much!!!!!
    Also, if anyone knows that there are already written little scripts
    like this. please send me some URL. I like to look at other people's
    codes...... to learn.
    , Feb 13, 2004
    #5
  6. Guest

    wrote in message news:<>...
    > I was able to write this little program which basically
    > just does one thing. It will record which server I have to work on(no
    > I can't do this w/ cron. as it has to be done manually and also i am
    > trying to learn perl here).
    >
    > #!/usr/bin/perl -w
    > open(FH, "+<server_watch") || die ;
    > $count = <FH>;
    > if ($count == 1) {$count=2 ;}
    > else {$count=1;}
    > seek(FH, 0,0) || die;
    > print FH "$count";
    > close(FH);


    That's not bad at all, my only criticisms are quite minor because is a
    "quick ('n' dirty)" program. There are a number of techniques used in
    your code that are great in a "quick 'n' dirty" program such as this
    but would be bad in a real one. Since you say you want to learn Perl
    I'll point them out.

    Even in a quick program you should incude the error in your error
    messages!

    open(FH, "+<server_watch") || die $!;

    In serious programs you'd probably want to include more information:

    open(FH, "+<server_watch") || die "Error opening server_watch: $!";

    Note, since this is a quick program I'd have not bothered with the
    explicit close().

    In your program you use implicitly declared global variables. For
    programs below a certain size this can be a saving. For larger
    programs in becomes a pain. People disagree about where the line at
    which it becomes a pain is drawn but most would agree that by the time
    you get to 100 lines and half a dozen subroutines you are well past
    it. I, personally, am a very inaccurate typist so I draw the line at
    ~10 lines.

    However, if you are writing small programs not as quick programs in
    their own right but as a exercises in learning how to write real
    programs you should get into the habit of putting "use strict;" at the
    top of even your shortest programs and always declaring all variables
    as lexically scoped (using my) in the smallest applicable scope
    (unless there's a reson to do otherwise).

    In the case of your program there's only one variable so:

    $count = <FH>;

    Would become

    my $count = <FH>;

    Putting "use strict" will disable the implicit global variable
    declaration, it also disables two other features that you will rarely
    need and you really don't want enabled until you've understood what
    they do. (For details RTFM).

    Bare filehandles, IMHO, are also a feature that's best restricted to
    "quick" programs.

    In larger programs you are better off using a lexical variable.

    open(my $FH, "+<server_watch") || die "Error opening server_watch:
    $!";

    IMNSHO, the 2-arg open() in which the mode and file paramters are
    concatenated is largely a legacy feature. If you are just starting
    Perl now you may as well go straight to the newer syntax.

    open(my $FH,'+<','server_watch') || die "Error opening server_watch:
    $!";

    In larger programs you should always use the more flexible "use
    warnings" rather than the "-w" switch. In "quick" programs where the
    few keystokes is significant then use the -w by all means.

    In real (as opposed to Q&D) programs you should import the SEEK_SET
    constant from the Fcntl module rather than assume it is 0. That said
    I don't think there are any platforms on which Perl is ever likely to
    be implemented on which it is anything else!

    Oh and you may also wish to see FAQ: What's wrong with always quoting
    "$vars"?

    > Also, if anyone knows that there are already written little scripts
    > like this.


    The point about Q&D programs is you write them quickly to do one
    thing, there would be no point publishing them.

    Perhaps you should be looking at early examples in a Perl tutorial.

    Be aware that there are many bad tutorials out there. Most (but not
    all) of the bad ones can be spotted by the following features:

    They don't "use strict" and "use warnings".

    They either don't declare variables using my() or
    don't do so in the smallest applicable scope.

    They use & on all subroutine calls.

    > I like to look at other people's codes...... to learn.


    If you really want to look at other people's first attempts you should
    consider looking in some of the Perl newsgroups that exist in this
    hierachy (as opposed to this one that does not). That way you not
    only see the code but also the critique. Just looking at other
    people's first attempt would be a case of people who don't know what
    they are doing pooling their ignorance. Of course, if you really want
    to pool ignorance about Perl there's a newsgroup specially for that
    too: alt.perl!
    , Feb 13, 2004
    #6
  7. Guest

    wrote in message news:<>...
    > wrote in message news:<>...
    > > I was able to write this little program which basically
    > > just does one thing. It will record which server I have to work on(no
    > > I can't do this w/ cron. as it has to be done manually and also i am
    > > trying to learn perl here).
    > >
    > > #!/usr/bin/perl -w
    > > open(FH, "+<server_watch") || die ;
    > > $count = <FH>;
    > > if ($count == 1) {$count=2 ;}
    > > else {$count=1;}
    > > seek(FH, 0,0) || die;
    > > print FH "$count";
    > > close(FH);

    >
    > That's not bad at all, my only criticisms are quite minor because is a
    > "quick ('n' dirty)" program. There are a number of techniques used in
    > your code that are great in a "quick 'n' dirty" program such as this
    > but would be bad in a real one. Since you say you want to learn Perl
    > I'll point them out.
    >
    > Even in a quick program you should incude the error in your error
    > messages!
    >
    > open(FH, "+<server_watch") || die $!;
    >
    > In serious programs you'd probably want to include more information:
    >
    > open(FH, "+<server_watch") || die "Error opening server_watch: $!";
    >
    > Note, since this is a quick program I'd have not bothered with the
    > explicit close().
    >
    > In your program you use implicitly declared global variables. For
    > programs below a certain size this can be a saving. For larger
    > programs in becomes a pain. People disagree about where the line at
    > which it becomes a pain is drawn but most would agree that by the time
    > you get to 100 lines and half a dozen subroutines you are well past
    > it. I, personally, am a very inaccurate typist so I draw the line at
    > ~10 lines.
    >
    > However, if you are writing small programs not as quick programs in
    > their own right but as a exercises in learning how to write real
    > programs you should get into the habit of putting "use strict;" at the
    > top of even your shortest programs and always declaring all variables
    > as lexically scoped (using my) in the smallest applicable scope
    > (unless there's a reson to do otherwise).
    >
    > In the case of your program there's only one variable so:
    >
    > $count = <FH>;
    >
    > Would become
    >
    > my $count = <FH>;
    >
    > Putting "use strict" will disable the implicit global variable
    > declaration, it also disables two other features that you will rarely
    > need and you really don't want enabled until you've understood what
    > they do. (For details RTFM).
    >
    > Bare filehandles, IMHO, are also a feature that's best restricted to
    > "quick" programs.
    >
    > In larger programs you are better off using a lexical variable.
    >
    > open(my $FH, "+<server_watch") || die "Error opening server_watch:
    > $!";
    >
    > IMNSHO, the 2-arg open() in which the mode and file paramters are
    > concatenated is largely a legacy feature. If you are just starting
    > Perl now you may as well go straight to the newer syntax.
    >
    > open(my $FH,'+<','server_watch') || die "Error opening server_watch:
    > $!";
    >
    > In larger programs you should always use the more flexible "use
    > warnings" rather than the "-w" switch. In "quick" programs where the
    > few keystokes is significant then use the -w by all means.
    >
    > In real (as opposed to Q&D) programs you should import the SEEK_SET
    > constant from the Fcntl module rather than assume it is 0. That said
    > I don't think there are any platforms on which Perl is ever likely to
    > be implemented on which it is anything else!
    >
    > Oh and you may also wish to see FAQ: What's wrong with always quoting
    > "$vars"?
    >
    > > Also, if anyone knows that there are already written little scripts
    > > like this.

    >
    > The point about Q&D programs is you write them quickly to do one
    > thing, there would be no point publishing them.
    >
    > Perhaps you should be looking at early examples in a Perl tutorial.
    >
    > Be aware that there are many bad tutorials out there. Most (but not
    > all) of the bad ones can be spotted by the following features:
    >
    > They don't "use strict" and "use warnings".
    >
    > They either don't declare variables using my() or
    > don't do so in the smallest applicable scope.
    >
    > They use & on all subroutine calls.
    >
    > > I like to look at other people's codes...... to learn.

    >
    > If you really want to look at other people's first attempts you should
    > consider looking in some of the Perl newsgroups that exist in this
    > hierachy (as opposed to this one that does not). That way you not
    > only see the code but also the critique. Just looking at other
    > people's first attempt would be a case of people who don't know what
    > they are doing pooling their ignorance. Of course, if you really want
    > to pool ignorance about Perl there's a newsgroup specially for that
    > too: alt.perl!


    thank you for your words.

    I will take your advise & I will show improvements next time I post.

    thank you!!
    , Feb 16, 2004
    #7
    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. Rich

    Response.Write not working

    Rich, Jul 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    4,051
    Marina
    Jul 22, 2003
  2. klub
    Replies:
    8
    Views:
    1,322
    Roedy Green
    Oct 31, 2005
  3. Joey Martin
    Replies:
    0
    Views:
    169
    Joey Martin
    Mar 15, 2006
  4. Iulian Ilea
    Replies:
    1
    Views:
    292
    pcx99
    Dec 21, 2006
  5. Tim Chase
    Replies:
    0
    Views:
    85
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page