Perl advice desperately needed..... 
Author Message
 Perl advice desperately needed.....

I have an assignment where we have to write a UNIX shell using perl.  The shell is to
accept various commands that will list all files in the current directory, delete files,
change the working directory, etc.  I am using pattern matching and if/elsif statements
to read in user input from the keyboard.  I have one problem, however.  In the shell,
one of the commands is to rename a file with a new name.  Here is what I have for the
"rename" command:

                  sub RENAME {
                    rename($OLDNAME, $NEWNAME) ||
                      die "Could not rename $OLDNAME to $NEWNAME: !$";  
                  .
                  .
                  .
                  elsif ($command =~ /rename/) {
                    &RENAME;    
                  }

Now, the command line is supposed to allow the user to type 'rename OLDNAME NEWNAME'.  
How can I accept the filenames and place them into the variables '$OLDNAME' and
'$NEWNAME' using pattern matching?  Perhaps I'm going about it all wrong?!?!?  Any help
would greatly be appreciated!

One more question...using the 'rename' example above, once the die function is carried
out in the RENAME subroutine, how do I return to the command-line prompt?  This is how I
display the command-line prompt:  

                while ($command !=~ /exit/) {
                  print "> ";
                  $command = <STDIN>;
                  .
                  .
                  .
                  if
                  .
                  .
                  .
                  elsif
                  .
                  .
                  .
                  elsif
                  .
                  .
                  .

Thanks again!
Jeff



Thu, 03 Jun 1999 03:00:00 GMT  
 Perl advice desperately needed.....

Quote:

> I have an assignment where we have to write a UNIX shell using perl.

Doesn't this work? :-)

    #!/usr/bin/perl
    exec '/bin/sh';

Quote:
>                   elsif ($command =~ /rename/) {

So, what would that do if your user types "I want to rename a file"? Maybe
you don't want to use a pattern match. The usual way would be to parse the
line into tokens, then usually look (in a hash?) for the first token.

   $commands{'rename'} = \&RENAME;
    ...
   if (defined($action = $commands{$token}) ) {

   } else {
        ...
   }

Quote:
> One more question...using the 'rename' example above, once the die
> function is carried out in the RENAME subroutine, how do I return to the
> command-line prompt?  

Maybe you want to use something other than die(), since death is so nearly
irreversable. :-)  I'd build a different failure mechanism into your
operations. Maybe they could return an error string upon failure and undef
upon success?

Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/

Randal Schwartz Case:     http://www.lightlink.com/fors/



Fri, 04 Jun 1999 03:00:00 GMT  
 Perl advice desperately needed.....

Quote:

> I have an assignment where we have to write a UNIX shell using perl.  

You know, there's something refreshing about a poster who starts
off a question with "I have an assignment ..."

        -joseph

--

Proprietor, 5 Sigma Productions          P.O. Box 6250 Chandler AZ 85246
Perl instruction (http://www.5sigma.com/perl/), C++/C/Perl software,



Sat, 05 Jun 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. NEED PERL EXPERTISE, DESPERATELY!

2. Need Perl (4.0 or higher) binaries desperately for Xenix System V

3. need DBM help desperately!

4. Desperately need help on installing GD.pm

5. Desperately need help with Image::Magick

6. Password script needed desperately.

7. desperately needing help

8. Desperately need help with HLists

9. Need advice for DB for web/perl

10. Need advice: Persistant data structures with Perl

11. Need advice on Perl->GPIB problem

12. Need Advice/Comment about Installing Perl in Cray

 

 
Powered by phpBB® Forum Software