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

Greetings, I am inquring as to the semantics of a gracefull completeion to this script.Also, how would I bumm this down to OO? ..apending: this request for help getting the code here where it should be Thanks monks...
#! /usr/bin/perl our $username; our $name; our $passwd; our $password; our $rot; print ":::this is passwd stickum:::\n"; print "to recover and hide passwords\n"; print "---press any key to begin---\n"; if (<STDIN>){&user}; sub user{ print "Account name:\n"; while (<>){ $username=$_; chomp $username; &query; } sub query{ print "Restore saved?\n"; while (<>){ my $mode=$_; if ($mode=~/yes|y/){ &recordsread; } elsif($mode=~/no|n/){ &recordswrite; } } } sub recordsread{ open(PASSWD, "+</.stickum.txt"); LINE:while (<PASSWD>){ $/="\n"; ($name, $passwrd)=split (/:/); next LINE if($name ne $username); feistal($passwrd); } print "The plaintext is:$rot\n"; close PASSWD; } sub recordswrite{ print "What is the new password?\n"; $passwd=<STDIN>; feistal($password); open (PASSWD "+</.stickum.txt"); EXISITS: while (<PASSWD>){ $\="/n"; $line=$_; next EXISTS unless $line=undef; } print PASSWD "$username:$rot"; close PASSWD; print "Recorded, go ahead and forget\n"; } sub feistal{ $rot=shift@_; $rot=~tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/; return $rot; }

edited: Mon Jun 21 17:58:39 2004 by jeffa - pasted code from user's scratchpad ...

Replies are listed 'Best First'.
Re: Gracefull completion..
by davidj (Priest) on Jun 19, 2004 at 17:16 UTC
    The version you have posted in your scratchpad has a few problems with it:

    2 compile errors:
    First, sub user does not have a closing brace.
    Second, your open statement in sub recordswrite does not have a comma:
    you have:
    open (PASSWD "+</.stickum.txt");
    you should have:
    open (PASSWD, "+</.stickum.txt");

    furthermore, the mode in which you are opening the file will not work unless the file already exists. The mode you are using will NOT create a file if it does not exist.
    you have:
    "+</.stickum.txt"
    you might want to try
    "+>>/.stickum.txt"
    That will open the file if it exists (without clobbering it) or create it if it doesn't exist. That way you can write to it.

    finally, in recordswrite you are asking for input, but you are not getting it.
    you have:
    print "What is the new passord?\n"; feistal($password);
    you should have:
    print "What is the new passord?\n"; $password = <STDIN>; feistal($password);
    Finally, a way to exit gracefully: rewrite sub user
    instead of:
    sub user{ print "Account name:\n"; while (<>){ $username=$_; chomp $username; &query; } } #corrected comile error
    try this:
    sub user{ print "Account name: "; $username = <STDIN>; while ($username == ""){ print "Account name: "; $username = <STDIN>; } &query }

    hope this helps,
    davidj
Re: Gracefull completion..
by Roger (Parson) on Jun 19, 2004 at 16:49 UTC
    Press "Ctrl+D" followed by "Enter" on unix terminals, or "Ctrl+Z" followed by "Enter" under DOS. This indicates that there is no more data from the STDIN.

    I'll leave the OO part of your question to other monks. :-)