Help with perl subroutines

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with perl subroutines
# 1  
Old 12-13-2011
Help with perl subroutines

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

This subroutine needs to check if there was a file name given on the command line. If so, return that. Otherwise get a file name from the user and return it if the file exists. If it does not exist stay in an input loop until the user enters the name of a file that does exist.

2. Relevant commands, code, scripts, algorithms:

Variables I want to use are $fileName for any user input and the default variable $_ or <> for if the filename is included in the command line.

3. The attempts at a solution (include all code and scripts):

This is what I have so far.

Code:
sub getFileName {

   while ($_ eq " ") {
   print ("Please enter a filename that exists. ") ;
   $fileName = <STDIN> ;
      if ( 
   last if -e $fileName ;
   }
   
   if ( -e $_ ) {
   return $_ ;
   }

   elsif ( -e $fileName ) {
   return $fileName ;
   }

}


4. Complete Name of School (University of Maryland University College), (MD), USA, Name of Professor: Cameron Bunch, CMIS325 (Computer and Information Science - UMUC Europe


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 12-13-2011
Please see the comments for explanation.

Code:
#!/usr/bin/perl

# This helps you to write better code and save time on debugging
use strict;
use warnings;

# Pass the command line argument(s) to the function
getFileName(@ARGV);

sub getFileName {
    # The argument list @ARGV is an array, read the first element if exists
    my $fileName = defined($_[0]) ? $_[0] : '';

    # Prompt for user input until the file exists
    while (! -e $fileName) {
        print "Please enter a filename that exists. ";
        $fileName = <STDIN>;
        chomp($fileName); #Remove the newline char at the end of input
    }

    return $fileName;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Subroutines; am I capturing them correctly?

HI Folks - Again, I'm very sorry for the amateur post. I'm reletively new to shell scripting so please bear with me. I have a script that execute another script which stops a particular set of services for my application. IF the execution is successful, I want to check for any hung ESSSVR... (4 Replies)
Discussion started by: SIMMS7400
4 Replies

3. Shell Programming and Scripting

how to obtain a variable between subroutines

#!/usr/bin/bash sub1 () { for ((i=0;i<10;i++)) do export a=$i; echo "value of a is $a"; sleep 1 done } sub1 & sub2 () { for ((j=0;j<10;j++)) do echo "value of a is $a"; sleep 1 done } (5 Replies)
Discussion started by: Arun_Linux
5 Replies

4. Shell Programming and Scripting

How can i use switches type arguments for subroutines in perl

i want to call subroutines in perl like: sub temp { ---- some code ----- } temp(-switchName, value1, --switchName2, value2) Like i know getoptions::Long is there for command line switches type arguments. So i want to know for subroutine type arguments. (1 Reply)
Discussion started by: Navrattan Bansa
1 Replies

5. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

6. Shell Programming and Scripting

How to get the return code of subroutines executed as standalone as command line in Perl ?

How to do I get the return code of a subroutine in a perl module if invoke the subroutine as standalone, I have an module say TestExit.pm and in that i have a subroutine say myTest() which is returns 12, if i were to call the subroutine from command line like CASE:1 ( Without an explict... (2 Replies)
Discussion started by: ennstate
2 Replies

7. Shell Programming and Scripting

Finding Subroutines

I'm maintaining a variety of programs (mostly in Perl) I didn't build, and the perllib is huge on the server. When I run across a user-defined subroutine it takes me at least an hour to look through all the logical places, and sometimes I still can't find the definition. Is there an easy way to... (2 Replies)
Discussion started by: pdownes
2 Replies

8. Shell Programming and Scripting

Passing date formats in Perl: i.e. Jul/10/2007 -> 20070710 (yyyymmdd) - Perl

Hi , This script working for fine if pass script-name.sh Jul/10/2007 ,I want to pass 20070710(yyyymmdd) .Please any help it should be appereciated. use Time::Local; my $d = $ARGV; my $t = $ARGV; my $m = ""; @d = split /\//, $d; @t = split /:/, $t; if ( $d eq "Jan" ) { $m = 0 }... (7 Replies)
Discussion started by: akil
7 Replies

9. UNIX for Dummies Questions & Answers

Passing Hash Tables to Subroutines

Hi: How do I pass a hash table down to a subroutine along with some other variables? For example, I have say a subroutine play_with_hash: sub play_with_hash { my( $var1, $var2, %my_hash ) = @_; #do stuff with %my_hash ........... } Then I want to call the subroutine... (1 Reply)
Discussion started by: mirzabhai
1 Replies

10. UNIX for Dummies Questions & Answers

shell scripting: calling subroutines

How do I define and call a subroutine in a ksh script (or any shell)? I'm writing a script that uses a lot of the same code over and over and I don't want to waste time/space. Thanks, Chuck (2 Replies)
Discussion started by: 98_1LE
2 Replies
Login or Register to Ask a Question