Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Learning Exercise

by debiandude (Scribe)
on Jul 18, 2002 at 18:55 UTC ( [id://182977]=note: print w/replies, xml ) Need Help??


in reply to Learning Exercise

Okay. Ill play. You really shouldn't need all those externs, they're completely unnecessary. Also their is more than one type of loop, you should have to rely on setting that $x variable. Here is a quick hack I wrote up:
#!/usr/bin/perl use strict; do { cls(); print "Address Book\n"; $_ = <>; if (/l/) { entry_list(); } elsif (/a/) { entry_add(); } elsif (/r/) { entry_rem(); } elsif (/s/) { entry_sea(); } else { print "Invalid Entry\n" if not /[lars(q|quit)]/; } } until(/q|quit/); exit; sub entry_list { cls(); open ADBK, "<adbk.db" || die "Datebase file does not exist\n"; while(<ADBK>) { print join(" ", split /:/); } close ADBK; print "Press any key to contine...\n"; $_ = <>; } sub entry_rem { my $tmp = (); my $num = 0; print "Enter the infomation:\n"; foreach("First Name: ", "Last Name: ", "Email: ") { print; chomp(my $line = <>); $tmp .= "$line:"; } cls(); open ADBK, "<adbk.db" || die "Could not open database to read\n"; my @file = <ADBK>; close ADBK; open ADBK, ">adbk.db" || die "Could not open database to open\n"; foreach ( @file) { next if /$tmp/; $num++; print ADBK $_; } close ABDK; print "No entries found\n" if $num == 0; print "Press any key to contine...\n"; $_ = <>; } sub entry_sea { my $tmp = (); my $num = 0; print "Enter the infomation:\n"; foreach("First Name: ", "Last Name: ", "Email: ") { print; chomp(my $line = <>); $tmp .= "$line:"; } cls(); open ADBK, "<adbk.db" || die "Could not open database to read\n"; while(<ADBK>) { if(/$tmp/) { print "Entry ", ++$num, ":\n", join("\n", split /:/) +; } } close ABDK; print "No entries found\n" if $num == 0; print "Press any key to contine...\n"; $_ = <>; } sub entry_add { my $tmp = (); foreach("First Name: ", "Last Name: ", "Email: ") { print; chomp(my $input = <>); $tmp .= $input.":"; } open ADBK, ">>adbk.db" || die "Could not open database to append\n"; print ADBK $tmp, "\n"; close ABDK; } sub cls { print "\033[2J"; print "\033[0;0f"; }

Replies are listed 'Best First'.
Re: Re: Learning Exercise
by runrig (Abbot) on Jul 18, 2002 at 20:16 UTC
    I like a function dispatch hash better:
    # Before the loop my %action = ( l => \&entry_list, a => \&entry_add, r => \&entry_rem, s => \&entry_sea, o => sub { print "Invalid Entry\n" }, ); # Within the loop ($action{lc()} || $action{o})->();
Re: Re: Learning Exercise
by Foncé (Scribe) on Jul 18, 2002 at 19:45 UTC
    Hmm...that makes a whole lot of sense. I do, however, have some questions:

    1. I understand how if (/l/) { entry_list(); } works, but can you explain the premise behind /l/? Is that just how you list things in perl? Also...I knew there were cases when you could leave the & off when calling a sub, but how does that work?

    2. || is equilivent to or?

    3. Kind of like #2, does | just allow you to specify more than one option in an 'or' fashion?

    4. What's the deal with subroutine cls?!

    5. my $tmp = (); just sets $tmp to undef, right?

    Alright, I believe that's all I have for now. Forgive the inexperience, please!
      • 1a. /l/ says if there is an 'l' in $_ return true. This means that if you type ' l ' ('l' with some spaces) it'll still match. It will also match on 'list', 'look' and 'isn't that a nice whale over there?'. Personally I'd have done:
        s/\s//; # remove all whitespace from $_ if($_ eq "l") { entry_list(); } elsif($_ eq "a") { addentry();} #etc
        $_ is equivalent to your $option.
      • 1b. &s can be left off subroutine calls if the call is unambiguously a subroutine call. In this case (calling the subroutine with a pair of parens) the parens make it unambiguous. Calling the subroutine without parens but with & means that the subroutine gets given the contents of @_ as it's argument list. This is discouraged because it often frightens people.

        So there are these ways to call a subroutine (ignoring object methods):

        subroutine; # doesn't pass strict. &subroutine; # passes @_ as argument list &subroutine(); # looks weird, passes arguments in () subroutine(); # generally preferred method
      • 2. || does mean "or" but they are not equivalent in associativity. Look at "perldoc perlop" for more information.
      • 3. | allows alternation in regular expressions (those things between the //s). So yes, it does allow you to specify more than one option in an or-like fashion.
      • 4. Looks like it probably clears the screen/terminal. Have you tried using it?
      • 5. Yep. I think debiandude had originally intended tmp to be an array. my $tmp; has essentially the same effect. Of you can use my $tmp = undef;.
      Hope it helps

      jarich

      >>2. || is equilivent to or? Yes also, && is and, ne is !=, not is ! and so on...

      >>. Kind of like #2, does | just allow you to specify more than one option in an 'or' fashion? | is the regex is the aleternation operator. Its pretty cool. You should check it out, very powerful :-)

      >> 4. What's the deal with subroutine cls?! Well you were using system("clear") or something like that which is not really portable. Those prints are the ansi escape sequence for clearing the screen and putting the cursor on the upper left corner.

      >> 5. my $tmp = (); just sets $tmp to undef, right? Yes. I was orgianally going to do something with it, besides being just a scalar but I didn't have enough time. So I tured it back into a scalar and never changed the parens, they're unnecessary. As I said it was a quick hack.
        I think I understand now. I need to continue trying these things out so I'll really get them.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://182977]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-18 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found