Hello monks!

I'm still a Perl newb, but am having lots of fun learning. I've created a few projects for myself, just as exercises, but I find myself stuck at the following one.

Basically, the script will accept two commands: "!read" and "!write", with proper arguments, and get or set the email address for a specific username (check out the code comments for details). It will read the information from (or save it to) an external file.
dbhandler is the subroutine that I'd like to handle the actual reading and writing.

Anyway, the code below is as far as I've gotten. I'm really stuck. Among other things, I'm not too sure what the most efficient way is to scan the file for the correct entry, nor how to update an existing entry. Oh yes, and commands must be case-insensitive, so "!READ John" and "!read john" should be the same.

#!/usr/bin/perl -w # "!read username" will read username's e-mail address. # "!write username user@example.com" will set username's e-mail addres +s # to user@example.com. # # Commands must be handled case-insensitively. use strict; print ("Hello there, what would you like to do? "); my $input_command = <STDIN>; chomp ($input_command); # Break $input_command into a space-delimited array and assign only th +e # first element (which will be the command) to $command. my $command = (split /\s/, $input_command)[0]; $command =~ tr/A-Z/a-z/; # lowercase everything # Take the second element (the name) and assign it to $name my $name = (split /\s/, $input_command)[1]; # Take the third element and assign it to $address my $address = (split /\s/, $input_command)[2]; if ($command =~ /!read\b/ || $command =~ /!write\b/) { print ("The command you entered is $command\n"); if ($name) { print ("The name you entered is $name\n"); } if ($address) { print ("The address you entered is $address\n"); } dbhandler($command, $name, $address); } else { print ("Sorry, only !read and !write are valid commands.\n"); } sub dbhandler { my ($subcommand, $subname, $subaddress) = @_; # lowercase everything. $subcommand is already received lowercase $subname =~ tr/A-Z/a-z/; $subaddress =~ tr/A-Z/a-z/; if ($subcommand =~ /!read\b/ { open ADDYFILE, "addyfile" || die "Couldn't open file: $!"; # rest of the subroutine should go here

Any hints, advice (and code! :)) would be appreciated.
Thanks!


In reply to Read from and write to file by Cenobite

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.