Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] Need help with mysterious code!!

Expand Messages
  • Dan Boger
    what s wrong with it? why can t you do what program #1 does? the In typeglob is a global one, no? if it is, it should work just the same in both cases? Dan
    Message 1 of 6 , Apr 21 4:09 PM
      what's wrong with it? why can't you do what program #1 does? the In
      typeglob is a global one, no? if it is, it should work just the same in
      both cases?

      Dan


      Ingenue wrote:
      >
      > Program 1 is wrong. You do not open a file in one sub, then close the file
      > handle in another.
      >
      > Using ">file" sets the fp to the begging of the file for write.
      >
      > If you wish to keep the data in the file, use append ">>file" instead.
      >
      > ----- Original Message -----
      > From: "Antonio A R N" <antonio.neves@...>
      > To: <perl-beginner@egroups.com>
      > Sent: Friday, April 21, 2000 2:10 PM
      > Subject: [PBML] Need help with mysterious code!!
      >
      > Hi,
      > Supose that I have a file called "t.txt" which is not empty. After
      > I run ###Program 1### it empties the file "t.txt", but when I run
      > ###Program 2### it doesn´t. What is happening here????
      > Could anyone help me with a program with functions like ###Program
      > 1### that doesn´t empties the file????
      >
      > Thanks...
      > Antonio
      >
      > ###Program 1###
      > &aa();
      > &bb();
      >
      > sub aa { open (In,">t.txt"); }
      > sub bb { close (In); }
      > ###End of Program1###
      >
      > ###Program 2###
      > open (In,">t.txt");
      > close (In);
      > ###End of Program2###
      >
    • Ingenue
      It s wrong because the structure isn t correct -- for the exact reason that I gave. If you had ever contributed to a program project, you would know this as a
      Message 2 of 6 , Apr 21 4:22 PM
        It's wrong because the structure isn't correct -- for the exact reason that
        I gave.

        If you had ever contributed to a program project, you would know this as a
        basic rule.

        Functions are to be self-contained in that they do not do "half a task".
        This leaves the reader of the source guessing why a file is opened in sub A
        and closed in sub B when it's a thousand times easier just to open and close
        it in the main source. Readability, Functionality, and Maintainability are
        the 3 main problems with that.



        ----- Original Message -----
        From: "Dan Boger" <dan@...>
        To: <perl-beginner@egroups.com>
        Sent: Friday, April 21, 2000 4:09 PM
        Subject: Re: [PBML] Need help with mysterious code!!


        > what's wrong with it? why can't you do what program #1 does? the In
        > typeglob is a global one, no? if it is, it should work just the same in
        > both cases?
        >
        > Dan
        >
        >
        > Ingenue wrote:
        > >
        > > Program 1 is wrong. You do not open a file in one sub, then close the
        file
        > > handle in another.
        > >
        > > Using ">file" sets the fp to the begging of the file for write.
        > >
        > > If you wish to keep the data in the file, use append ">>file" instead.
        > >
        > > ----- Original Message -----
        > > From: "Antonio A R N" <antonio.neves@...>
        > > To: <perl-beginner@egroups.com>
        > > Sent: Friday, April 21, 2000 2:10 PM
        > > Subject: [PBML] Need help with mysterious code!!
        > >
        > > Hi,
        > > Supose that I have a file called "t.txt" which is not empty. After
        > > I run ###Program 1### it empties the file "t.txt", but when I run
        > > ###Program 2### it doesn´t. What is happening here????
        > > Could anyone help me with a program with functions like ###Program
        > > 1### that doesn´t empties the file????
        > >
        > > Thanks...
        > > Antonio
        > >
        > > ###Program 1###
        > > &aa();
        > > &bb();
        > >
        > > sub aa { open (In,">t.txt"); }
        > > sub bb { close (In); }
        > > ###End of Program1###
        > >
        > > ###Program 2###
        > > open (In,">t.txt");
        > > close (In);
        > > ###End of Program2###
        > >
        >
        > ------------------------------------------------------------------------
        > Your high school sweetheart-where is he now? With 4.4 million alumni
        > already registered at Classmates.com, there's a good chance you'll
        > find her here. Visit your online high school class reunion at:
        > http://click.egroups.com/1/3139/1/_/12898/_/956358545/
        > ------------------------------------------------------------------------
        >
        >
      • Dan Boger
        ... yes, that s all very true - I don t think you should write code like this. However, I still don t know why the two programs work in different ways..
        Message 3 of 6 , Apr 21 4:30 PM
          Ingenue wrote:
          >
          > It's wrong because the structure isn't correct -- for the exact reason that
          > I gave.
          >
          > If you had ever contributed to a program project, you would know this as a
          > basic rule.
          >
          > Functions are to be self-contained in that they do not do "half a task".
          > This leaves the reader of the source guessing why a file is opened in sub A
          > and closed in sub B when it's a thousand times easier just to open and close
          > it in the main source. Readability, Functionality, and Maintainability are
          > the 3 main problems with that.

          yes, that's all very true - I don't think you should write code like
          this. However, I still don't know why the two programs work in
          different ways..
        • Tom Barron
          ... They don t work in different ways on my machine. With both programs, t.txt winds up truncated to 0 bytes. One possibility I can think of is that perhaps
          Message 4 of 6 , Apr 21 4:45 PM
            Dan Boger wrote:
            >
            > Ingenue wrote:
            > > ...
            > yes, that's all very true - I don't think you should write code like
            > this. However, I still don't know why the two programs work in
            > different ways..

            They don't work in different ways on my machine. With both programs,
            t.txt winds up truncated to 0 bytes. One possibility I can think of is
            that perhaps in program 2, Antonio had left off the final line
            terminator and Perl was not processing the last line so that the close
            never happened. Something like that might explain it.

            In any case, Antonio, if you're wanting to add information to t.txt
            without losing what's already there, you need to use the append operator
            ">>" when you open the file as Ingenue pointed out.

            rgds,
            Tom
          • Ingenue
            They don t work in different ways -- they both delete file s contents and create an empty file (not literally, but to the end-user). He just didn t run his
            Message 5 of 6 , Apr 21 4:57 PM
              They don't work in different ways -- they both delete file's contents and
              create an empty file (not literally, but to the end-user).

              He just didn't run his program correctly or is on drugs.

              [root@operations /home/nikita]# more t.txt
              1
              2
              3
              [root@operations /home/nikita]# more foo2.pl
              #!/usr/bin/perl
              open (In,">t.txt");
              close (In);
              [root@operations /home/nikita]# perl foo2.pl
              [root@operations /home/nikita]# more t.txt
              [root@operations /home/nikita]#

              other-

              [root@operations /home/nikita]# more t.txt
              1
              2
              3
              [root@operations /home/nikita]# more foo.pl
              #!/usr/bin/perl -w
              &aa();
              &bb();
              sub aa{ open (In,">t.txt"); }
              sub bb { close (In); }
              [root@operations /home/nikita]# perl foo.pl
              [root@operations /home/nikita]# more t.txt
              [root@operations /home/nikita]#


              ----- Original Message -----
              From: "Tom Barron" <tbarron@...>
              To: <perl-beginner@egroups.com>
              Sent: Friday, April 21, 2000 4:45 PM
              Subject: Re: [PBML] Need help with mysterious code!!


              > Dan Boger wrote:
              > >
              > > Ingenue wrote:
              > > > ...
              > > yes, that's all very true - I don't think you should write code like
              > > this. However, I still don't know why the two programs work in
              > > different ways..
              >
              > They don't work in different ways on my machine. With both programs,
              > t.txt winds up truncated to 0 bytes. One possibility I can think of is
              > that perhaps in program 2, Antonio had left off the final line
              > terminator and Perl was not processing the last line so that the close
              > never happened. Something like that might explain it.
              >
              > In any case, Antonio, if you're wanting to add information to t.txt
              > without losing what's already there, you need to use the append operator
              > ">>" when you open the file as Ingenue pointed out.
              >
              > rgds,
              > Tom
              >
              > ------------------------------------------------------------------------
              > Good friends, school spirit, hair-dos you'd like to forget.
              > Classmates.com has them all. And with 4.4 million alumni already
              > registered, there's a good chance you'll find your friends here:
              > http://click.egroups.com/1/2885/1/_/12898/_/956360745/
              > ------------------------------------------------------------------------
              >
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.