Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write PPMs or reuseable code

Status
Not open for further replies.

jray2003

Programmer
Aug 12, 2003
253
0
0
US
I am finding that in creating my project, I am using the same functions over and over again and I was wondering how hard it is to create either a PPM or some that could do some thing along the line like this.

Code:
# Check to see if file exsit, if so delete it.
if (-e "$DestFile"){
    unlink($DestFile);
}

This is only one example that I do. Now what i would like to do is this.

CheckFile($DestFile);

and do it.

Does this make sense and possible to do?

Thanks

Jim
 
Hi,

I think from your posting you're looking at making your own package or module which is very easy to do.

The difference between the two being that while using a package, you have to specify the name of the package,

i.e. my_package::my_function()

While in a module you don't have to, you could just call my_function(). However, you should be aware of possible variable name conflicts within your script when using modules.




Matthew King
MaKing@nbpower.com
Computer Design & System Performance,
Pt. Lepreau Nuclear Generating Station,
NB Power,
Maces Bay, N.B.,
 
This is true. Here is what I am doing but getting errors. I found using the "require" works better for me. But I am having problems gettings this one to work. Am I missing something here?
------------------------------------
Calling Code:

Code:
require "Common.pl"

Check_File($DestFile);

Required Code:

Common.pl

Code:
Sub Check_File {

my ($myFile) = @_;

  # Check to see if file exsit
  if (-e "$myFile"){ <<< This is giving me an error.
      unlink($myFile);
  }
}

1;
 
Hi,

For packages you have to use the package name to call the function. As well you have to use the keyword package in your common.pl

So you should do this

Script
Code:
require "Common.pl"

Common::Check_File($DestFile);

Common.pl
Code:
package Common;

sub Check_File {

my ($myFile) = @_;

  # Check to see if file exsit
  if (-e "$myFile"){ <<< This is giving me an error.
      unlink($myFile);
  }
}

return 1;

Pretty sure that should work, don't have time to test it for you right now. Sorry.


Matthew King
MaKing@nbpower.com
Computer Design & System Performance,
Pt. Lepreau Nuclear Generating Station,
NB Power,
Maces Bay, N.B.,
 
you could use a module or just make it a sub routine of your script and call the sub routine:

Code:
my $filename = 'foo.txt';
if ( bonk($filename) ) {
   print "$filename was deleted";
}
else {
   print"Unable to delete $filename";
}


sub bonk {
   local $_ = shift;
   unlink($_) || return(0);
}

or more compactly:

Code:
my $filename = 'foo.txt';
bonk($filename) ? print "$filename was deleted!" : print "Unable to delete $filename";
sub bonk {
   local $_ = shift;
   unlink($_) || return(0);
}
 
Kevin,

That should work and what I want to be able to do is use this code for all my other PL's as well and that is why I want to create a common.pl. This way all I would have to do is pass the file name and let the common do it's job.

I have a few commons SUBS that I call a lot and want to put them in a common file.

Thanks again,

Jim
 
I'd go with "use" as its more robust than require.

Code:
#!perl -w
use strict;
use MyCommon;

my $filename = 'foo.txt';
if ( bonk($filename) ) {
   print "$filename was deleted";
}
else {
   print "unable to  delete $filename";
}

MyCommon.pm

Code:
sub bonk {
   local $_ = shift;
   unlink($_) || return(0);
}
1;

MyCommon.pm must be in the same folder as the script to work like this. Also give it a .pm extension so you can write it like:

use MyCommon;
 
Cool, then I can add all my commons SUBs in the file correct?

Thanks and I will do just that.

Jim
 
Yes but you should export any subs that you want to use externally.


Trojan.
 
eer, Export.. Oh boy something else for me to read up on. Also, if I do use it as PM file, do I have to add the PM to perl like I do other PM mods or, just call it as a PM with the USE command?

Thanks and this is getting fun.

Jim
 
Can ask one more question. The code I am working with requires me to return 2 variables. I got everything working great but how is this done.

return $myvar1 $myvar2

Is this the way and then do I have to do a SPLIT from there?

Thanks
 
Code:
return ($myvar1, $myvar2);
And when you call the function (called say "myfn") you call it like this:
Code:
($result1, $result2) = myfn();


Trojan.
 
Ah, I forgot the ( )... That makes sense!

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top