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

"Perl For Dummies" Help Requested

Expand Messages
  • Mike Frank
    I have been teaching myself Perl via the Perl For Dummies book, and there is one program that will not run as the book indicates it should. The program uses
    Message 1 of 4 , Oct 12, 2010
    • 0 Attachment
      I have been teaching myself Perl via the "Perl For Dummies" book, and there
      is one program that will not run as the book indicates it should.

      The program uses a flat file database, and I can choose between accessing
      the file, or quitting. However, neither work.

      I have Googled for an answer to my problem, but I could not find the
      solution.

      I ran the debugger, and I found that when the program reached "$DoSearch
      =~ tr/A-Z/a-z/c;", the letter that is stored in $DoSearch changes from 'i'
      or 'q' to 'z'.

      I don't understand why the stored value in $DoSearch gets changed, and I
      don't know how to prevent that from happening.

      Below is the program (filename: empdata1.pl)

      Below the program is the data file (filename: edata.txt)

      Any assistance would be greatly appreciated. I have been trying to figure
      this out for a long time.

      ############################################################################
      ##########################

      #!/usr/bin/perl -w

      use strict;
      use warnings;

      # empdata1.pl: a very simple employee database application.
      # This program reads a text file that is an employee
      # database and lets you query it by ID number.
      #
      # Each record is on a single line. Each field in the
      # record is separated by a single tab character
      # ("\t"). The database has four fields:
      # - Last name
      # - First name
      # - ID number
      # - Telephone extension

      # The name of the database file
      my $TheDB = 'edata.txt';

      # Open the database file but quit if it doesn't exist
      open(INDB, $TheDB) or die "The database $TheDB could " .
      "not be found.\n";

      while (1) { # Loop forever
      print "\nDo you want to search by employee ID (I)," .
      "or quit (Q): ";
      my $DoSearch = <STDIN>;
      chomp($DoSearch);
      $DoSearch =~ tr/A-Z/a-z/c;
      # Check if they want to quit
      if($DoSearch eq 'q') { last }
      # Check if they did *not* say i or I
      unless($DoSearch eq 'i') {
      print "You must enter either I or Q.\n";
      next; # Go out to the while loop
      }

      # Ask them what ID they want to search for
      print "Search for ID number: ";
      my $SearchFor = <STDIN>;
      chomp($SearchFor);
      # Go to the top of the database in case this isn't
      # the first time they are searching
      seek(INDB, 0, 0);
      # Reset the count of successes
      my $SuccessCount = 0;
      # Loop through the records in the file
      while(<INDB>) {
      my $TheRec = $_;
      chomp($TheRec);
      (my $LastName, my $FirstName, my $ID, my $Tel) =
      split(/\t/, $TheRec);
      if($ID eq $SearchFor) {
      $SuccessCount = $SuccessCount + 1;
      print "$ID: $FirstName $LastName, ext. " .
      "$Tel\n";
      } # End of if
      } # End of while(<INDB>)
      if($SuccessCount == 0) { print "No records found.\n" }
      else { print "$SuccessCount records found.\n" }
      } # End of while(1)

      print "Program finished.\n";

      ############################################################################
      ##########################

      Anastasio Tony 12 143
      Manzanera Phil 15 156
      Stewart Dave 17 154
      Thompson Richard 20 112
    • Shlomi Fish
      ... The For Dummies books and their ilk have very bad reputations in the technical community. Furthermore, the 1st and 2nd editions of Perl for Dummies
      Message 2 of 4 , Oct 12, 2010
      • 0 Attachment
        On Wednesday 13 October 2010 04:23:11 Mike Frank wrote:
        > I have been teaching myself Perl via the "Perl For Dummies" book, and there
        > is one program that will not run as the book indicates it should.
        >

        The "For Dummies" books and their ilk have very bad reputations in the
        technical community. Furthermore, the 1st and 2nd editions of "Perl for
        Dummies" received a very negative review from Mark Jason Dominus:

        http://perl.plover.com/reviews/p54d.html

        May I suggest you head over to http://perl-begin.org/ and pursue one of the
        books or tutorials there instead of reading "Perl for Dummies"?

        > The program uses a flat file database, and I can choose between accessing
        > the file, or quitting. However, neither work.
        >
        > I have Googled for an answer to my problem, but I could not find the
        > solution.
        >
        > I ran the debugger, and I found that when the program reached "$DoSearch
        > =~ tr/A-Z/a-z/c;", the letter that is stored in $DoSearch changes from 'i'
        > or 'q' to 'z'.

        A better way to do it would be:

        [code]
        $DoSearch = lc($DoSearch);
        [/code]

        As for why it doens't work properly - reading from
        http://perldoc.perl.org/perlop.html :

        {{{
        Options:

        c Complement the SEARCHLIST.
        d Delete found but unreplaced characters.
        s Squash duplicate replaced characters.

        If the "/c" modifier is specified, the SEARCHLIST character set is
        complemented.
        }}}

        What the /c flag means that *anything* that is not included in the SEARCHLIST
        is matches. So you turn all the characters that are *not* inside A-Z into
        something else.

        I still think it's good that you know how to use the perl debugger, and that
        you're adhering to some good practices such as "use strict;" and "use
        warnings;". But otherwise, I suggest you follow my advice regarding
        http://perl-begin.org/ .

        Regards,

        Shlomi Fish

        --
        -----------------------------------------------------------------
        Shlomi Fish http://www.shlomifish.org/
        http://www.shlomifish.org/humour/ways_to_do_it.html

        <rindolf> She's a hot chick. But she smokes.
        <go|dfish> She can smoke as long as she's smokin'.

        Please reply to list if it's a mailing list post - http://shlom.in/reply .
      • Mike Frank
        Thank you for your suggestions. I can t understand why this book was marketed by the For Dummies publisher if it doesn t do the job of teaching somebody how
        Message 3 of 4 , Oct 13, 2010
        • 0 Attachment
          Thank you for your suggestions.

          I can't understand why this book was marketed by the "For Dummies" publisher
          if it doesn't do the job of teaching somebody how to use Perl.

          I like it because it has some good examples, and the descriptions are
          straight forward and understandable.

          Mike



          -----Original Message-----
          From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com]
          On Behalf Of Shlomi Fish
          Sent: Wednesday, October 13, 2010 2:51 AM
          To: perl-beginner@yahoogroups.com
          Subject: Re: [PBML] "Perl For Dummies" Help Requested

          On Wednesday 13 October 2010 04:23:11 Mike Frank wrote:
          > I have been teaching myself Perl via the "Perl For Dummies" book, and
          there
          > is one program that will not run as the book indicates it should.
          >

          The "For Dummies" books and their ilk have very bad reputations in the
          technical community. Furthermore, the 1st and 2nd editions of "Perl for
          Dummies" received a very negative review from Mark Jason Dominus:

          http://perl.plover.com/reviews/p54d.html

          May I suggest you head over to http://perl-begin.org/ and pursue one of the
          books or tutorials there instead of reading "Perl for Dummies"?

          > The program uses a flat file database, and I can choose between accessing
          > the file, or quitting. However, neither work.
          >
          > I have Googled for an answer to my problem, but I could not find the
          > solution.
          >
          > I ran the debugger, and I found that when the program reached "$DoSearch
          > =~ tr/A-Z/a-z/c;", the letter that is stored in $DoSearch changes from 'i'
          > or 'q' to 'z'.

          A better way to do it would be:

          [code]
          $DoSearch = lc($DoSearch);
          [/code]

          As for why it doens't work properly - reading from
          http://perldoc.perl.org/perlop.html :

          {{{
          Options:

          c Complement the SEARCHLIST.
          d Delete found but unreplaced characters.
          s Squash duplicate replaced characters.

          If the "/c" modifier is specified, the SEARCHLIST character set
          is
          complemented.
          }}}

          What the /c flag means that *anything* that is not included in the
          SEARCHLIST
          is matches. So you turn all the characters that are *not* inside A-Z into
          something else.

          I still think it's good that you know how to use the perl debugger, and that

          you're adhering to some good practices such as "use strict;" and "use
          warnings;". But otherwise, I suggest you follow my advice regarding
          http://perl-begin.org/ .

          Regards,

          Shlomi Fish

          --
          -----------------------------------------------------------------
          Shlomi Fish http://www.shlomifish.org/
          http://www.shlomifish.org/humour/ways_to_do_it.html

          <rindolf> She's a hot chick. But she smokes.
          <go|dfish> She can smoke as long as she's smokin'.

          Please reply to list if it's a mailing list post - http://shlom.in/reply .


          ------------------------------------

          Unsubscribing info is here:
          http://help.yahoo.com/help/us/groups/groups-32.htmlYahoo! Groups Links
        • merlyn@stonehenge.com
          ... Mike I can t understand why this book was marketed by the For Dummies Mike publisher if it doesn t do the job of teaching somebody how to use Mike
          Message 4 of 4 , Oct 13, 2010
          • 0 Attachment
            >>>>> "Mike" == Mike Frank <m-frank@...> writes:

            Mike> I can't understand why this book was marketed by the "For Dummies"
            Mike> publisher if it doesn't do the job of teaching somebody how to use
            Mike> Perl.

            I sat across the dinner table one night from the guy who wrote that
            book. From what I recall, he had just learned Perl himself the weekend
            before he started writing it (I think from "Learning Perl", in fact).
            No kidding. He was looking for something to write after he had just
            completed "Netscape for Dummies", and the editor offered him this book.

            --
            Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
            <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
            Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
            See http://methodsandmessages.posterous.com/ for Smalltalk discussion
          Your message has been successfully submitted and would be delivered to recipients shortly.