Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Making/Using a module.

by Andrew_Levenson (Hermit)
on Mar 09, 2006 at 01:12 UTC ( [id://535300]=perlquestion: print w/replies, xml ) Need Help??

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

I was told that to create a routine that any program can use, I need to make a module, so I tried my hand at that. But for the life of my, I can't get it to work. Can anyone glance over my scripts to tell me what it is i'm doing wrong?
This is the script:
use strict; use warnings; my @file; my $file1; my $file2; print "Please enter the name of the file to copy.\n"; use FileNamePrep qw(&func1); open FILE, "<$file1"; while(<FILE>){ push @file, $_; } close FILE; print "\nPlease enter the name of the copy destination.\n"; use FileNamePrep qw(&func2); if($file2 eq "\n"){ $file2='C:\Documents and Settings\Copy Destination.txt'; print "\nIf you did not give a destination, your new file will be found under C:\ Documents and Settings, with the name Copy Destination.txt\n"; } open LOG, ">$file2"; print LOG @file; close LOG;
And here is my 'module':
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(func1 func2); %EXPORT_TAGS = ( DEFAULT => [qw(&func1)], Both => [qw(&func1 &func2)]); sub func1 { for(my $file1=<>){ chomp($file1); if(m/\"/){ chop($file1); $file1=reverse($file1); chop($file1); $file1=reverse($file1); } } } sub func2 { for(my $file2=<>){ chomp($file2); if(m/\"/){ chop($file2); $file2=reverse($file2); chop($file2); $file2=reverse($file2); } } } 1;
Thanks in advance.

Replies are listed 'Best First'.
Re: Making/Using a module.
by davidrw (Prior) on Mar 09, 2006 at 01:21 UTC
    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
      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.
      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?
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://535300]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-19 15:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found