ellem has asked for the wisdom of the Perl Monks concerning the following question:

I get no errors. The code does create the file. (I mean to open and append... >> may be the wrong thing.)
Anyway here's the code:
#! /usr/bin/perl -w use strict ; #Adress Book Program #ver .01 #Lou Moran 01.28.02 my ($cat, $fname, $lname, $cname, $addr1, $addr2, $city, $state, $zip, + $wphone, $mphone, $hphone, $ophone, $bday, $anni, $email, $icq, $not +es) ; open (ADDRESS, '>> addbook.txt') or die "Can't open file; $!" ; while (<ADDRESS>) { print "Is this a Personal or Business contact? (Enter P or B)" ; my $perbus = <STDIN> ; chomp $perbus ; $perbus =~ tr/A-Za-z/a-z/ ; if ($perbus eq 'b'){ $cat = "Business" ; } else { $cat = "Personal" ; } print "Enter a first name: " ; $fname = <STDIN> ; chomp $fname ; print "Enter a last name: " ; $lname = <STDIN> ; chomp $lname ; print "Enter a company name: " ; $cname = <STDIN> ; chomp $cname ; print "Enter address information: " ; $addr1 = <STDIN> ; chomp $addr1 ; print "Enter address information: " ; $addr2 = <STDIN> ; chomp $addr2 ; print "Enter city: " ; $city = <STDIN> ; chomp $city ; print "Enter state (Use 2 letter abbreviation): " ; $state = <STDIN> ; chomp $state ; print "Enter Zip code: " ; $zip = <STDIN> ; chomp $zip ; print "Enter work phone: " ; $wphone = <STDIN> ; chomp $wphone ; print "Enter mobile phone: " ; $mphone = <STDIN> ; chomp $mphone ; print "Enter home phone: " ; $hphone = <STDIN> ; chomp $hphone ; print "Enter other phone: " ; $ophone = <STDIN> ; chomp $ophone ; print "Enter a birthday: " ; $bday = <STDIN> ; chomp $bday ; print "Enter an anniversary: " ; $anni = <STDIN> ; chomp $anni ; print "Enter email address: " ; $email = <STDIN> ; chomp $email ; print "Enter ICQ or AIM name: " ; $icq = <STDIN> ; chomp $icq ; print "Enter a note about the entry: " ; $notes = <STDIN> ; chomp $notes ; write ADDRESS ; } close ADDRESS or die "Can't close file; $!" ; format ADDRESS_TOP = AddressBook version 0.01 ___________________ ______________________________________________ +________ . format ADDRESS = @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<< "NAME", $lname, $fname "ADDRESS", $addr1 "ADDRESS", $addr2 "CITY", $city "STATE", $state "ZIP", $zip "WORK PHONE", $wphone "MOBILE PHONE", $mphone "HOME PHONE", $hphone "OTHER PHONE", $ophone "BIRTHDAY", $bday "ANNIVERSARY", $anni "EMAIL", $email "ICQ-AIM NAME", $icq "NOTE", $note "CATEGORY", $cat .
Any ideas?
--
ellem@optonline.net
There's more than one way to do it, just don't use my way.

Replies are listed 'Best First'.
Re: Why does this code quit?
by adamsj (Hermit) on Feb 01, 2002 at 08:12 UTC
    You also have an unpleasant surprise waiting in this line here

    $perbus =~ tr/A-Za-z/a-z/ ;

    which is not going to do what you think it will. See perlop for more guidance.

    adamsj

    They laughed at Joan of Arc, but she went right ahead and built it. --Gracie Allen

      GOOD Point! or was that good pzzzz! The lc lower case function is a shorter faster way to do it than tr/A-Za-z/a-za-z/ which is redundant as you only require tr/A-Z/a-z/ to do the same as lc cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Why does this code quit?
by busunsl (Vicar) on Feb 01, 2002 at 07:44 UTC
    You read from ADDRESS in a loop, but it is opened for writing.
    So the loop ends after the first round and the program exits.

    I think you should use something else to control that loop.

    Change the while (<ADDRESS>) { to while (1) { and add something like:

    print 'Enter another address?'; my $answer = <>; last if $answer !~ /[yY]/;
    at the end of the loop.
Re: Why does this code quit?
by japhy (Canon) on Feb 01, 2002 at 14:20 UTC
    Your format is busted. That's not the way formats work. You'll have to be quite explicit:
    format ADDRESS = @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< "NAME", "$first $last" @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< "THIS", $that .
    etc.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Yikes! You are right. Is there a better way?
      --
      ellem@optonline.net
      There's more than one way to do it, just don't use my way.
        You could do something like:
        my ($field, $value); # then, inside your loop... ... { ... my @lines = ( [ "NAME", "$fname $lname" ], [ "AGE", $age ], [ "WHATEVER", $whenever ], # ... ); for (@lines) { ($field, $value) = @$_; write FORMAT; } ... } ... format FORMAT = @<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $field, $value .

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Why does this code quit?
by dmmiller2k (Chaplain) on Feb 01, 2002 at 19:11 UTC

    Quite directly, because you've opened the filehandle ADDRESS for writing, and then you try to read from it, as busunsl points out:

    open (ADDRESS, '>> addbook.txt') or die "Can't open file; $!" ; while (<ADDRESS>) { # ...

    Your while loop terminates immediately.

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime