in reply to Making/Using a module.

ah. you were close -- you said with the use FileNamePrep qw(&func1); and use FileNamePrep qw(&func2); that you wanted func1 and func2 available, but you never actually called them ...
use FileNamePrep qw( func1 func2 ); ... print "Please enter the name of the file to copy.\n"; my $file1 = func1(); ... print "\nPlease enter the name of the copy destination.\n"; my $file2 = func2();
Update: Hmm.. also, i see that func1() and func2() are nearly identical, and also that neither returns anything, and they both localize, with 'my', $file1 and $file2, so really they don't do anything.. i think what you mean is something like this:
use FileNamePrep qw( promptFilename ); ... my $file1 = promptFilename("Please enter the name of the file to copy. +"); ... my $file2 = promptFilename("Please enter the name of the copy destinat +ion."); ######################### package FileNamePrep; use strict; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 0.1; @ISA = qw(Exporter); @EXPORT = (); @EXPORT_OK = qw(promptFilename); %EXPORT_TAGS = ( DEFAULT => [qw(promptFilename)], Both => [qw(promptFilename)]); sub promptFilename { my $prompt = shift; print $prompt . "\n"; my $file; while(! $file ){ # fixed this loop $file = <>; chomp($file); $file =~ s/^"(.+)"$/$1/; # unquote } return $file; } 1;
Update: I assume this was a module excercise, but also be aware of the standard File::Copy module. Update: Fixed the bad loop in promptFilename -- thanks to runrig

Replies are listed 'Best First'.
Re^2: Making/Using a module.
by Andrew_Levenson (Hermit) on Mar 09, 2006 at 01:26 UTC
    Wow, thanks. So I open with program with a 'use' command and declare which functions I want to use, and later call them with the variable? That makes so much sense, haha. Thanks again.
Re^2: Making/Using a module.
by runrig (Abbot) on Mar 09, 2006 at 01:50 UTC
    You are reading in file names in a loop, but only returning one file name. You don't need the loop in promptFilename(), and only need to prompt for the one file name (and I believe it is an infinite loop).
      Okay, so I use the =~ because that tells whatever i'm doing to apply the next command to the preceding scalar?
Re^2: Making/Using a module.
by Andrew_Levenson (Hermit) on Mar 09, 2006 at 01:40 UTC
    Thanks for the additional info, and I hate to be a bother, but could you possibly explain some of the script you changed in the module? I think it may be a little ahead of where I am.
      no prob -- here's the promptFilename() disected.. note this was a replacement for both func1() and func2() :
      sub promptFilename { my $prompt = shift;
      First, declare the function name with 'sub' .. then store the first argument in $prompt. Since in your original you always displayed a string to user before reading in the filename, I thought it made sense to pull it into the function.
      print $prompt . "\n";
      Use the given string to display a prompt to the user.
      my $file; while(! $file){ $file = <>;
      NOTE: reading through this, i realized an error and updated my code here and above.
      Declare a $file to hold the result. Start a loop that goes until we have something in $file. Each iteration will start by reading a line from STDIN.
      chomp($file);
      Strip the newline.
      $file =~ s/^"(.+)"$/$1/; # unquote
      This a regex substituion (see perlre) -- tries to match a quote at the beginning, anything in the middle, and a quote at the end. If it matches, it replaces everything with the middle part (i.e. it strips the quotes). If it doesn't match, it does nothing (cause the string wasn't quoted).
      } return $file; }
      Close the loop, return the $file value, and close the function.
        Alright, final question and i'll be done with this bit for now. In the search and replace bit, what is the $1?
        Thank you, it works like a charm now.