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!

Execute a module's sub-routine by default

Status
Not open for further replies.

varakal

IS-IT--Management
Mar 18, 2004
114
0
0
US
I need to execute a sub-routine from a module everytime I run a perl script. This sub-routine sets the library path depending on the base path of the script.

Easier way would be modify the script and add code to execute this sub-routine. But we have hundreds of scripts to edit which we are not supposed to.

Is there a way to always execute this subroutine whenever I call perl interpreter.

Thanks.
 
not that I know of.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Maybe...

Code:
perl -MMyModule script.pl

Code:
package MyModule;

# do stuff here

It'd assume that MyModule is in a default @INC location though.

-------------
Cuvou.com | The NEW Kirsle.net
 
This will only include the module for the script. It will not actually execute any sub-routine with in it.
 
Correction:

Code:
#!/usr/bin/perl -w

use MyModule;

Code:
package MyModule;

print "Hello, world!\n";

When running the first code, it would print "Hello, world!", as the MyModule code was instructed to do.

Code:
package MyModule;

&mySubroutine(); # call this sub

sub mySubroutine {
   print "Hello, world!\n";
}

-------------
Cuvou.com | The NEW Kirsle.net
 
but he said he can't edit the scripts.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
That's right. I can't edit the code. I can, but I am looking for a solution such that I don't have to modify hundreds of scripts.
 
You could just write a script to modify the hundreds of scripts for you, it'd take a minute to write and a second to run, and all hundreds of your scripts get modified in no time.

Code:
# one of my old but useful functions
sub scanDir {
   my ($dir) = @_;

   my @files = ();

   opendir (DIR, $dir);
   foreach my $file (readdir(DIR)) {
      next if $file eq '.' || $file eq '..';
      if (-d "$dir/$file") {
        # This file is another directory, so scan this one too
        push (@files,&scanDir("$dir/$file"));
      }
      else {
        # Only check for Perl scripts.
        next unless $file =~ /\.(pl|cgi)$/i;
        push (@files,"$dir/$file");
      }
   }
   closedir (DIR);

   return @files;
}

# recursively scan directories
my @scripts = &scanDir("/var/[URL unfurl="true"]www/html/cgi-bin");[/URL]

# open each script
foreach (@scripts) {
   open (SCRIPT, $_);
   my @script = <SCRIPT>;
   close (SCRIPT);
   chomp @script;

   if ($script[0] =~ /^#!\/usr\/bin\/perl/i) {
      $script[0] .= "\n\n"
        . "use MyModule;";
   }

   open (WRITE, ">$_");
   print WRITE join ("\n",@script);
   close (WRITE);
}

That'd basically get an array of paths to Perl scripts, open each one, make sure that the first line is a proper-looking shebang line, and if so, add "use MyModule;" below it, then resave the file.

You'd wanna back up all your scripts first though.. copy them somewhere that this script won't get them, so that if something goes wrong your scripts aren't totally screwed.

-------------
Cuvou.com | The NEW Kirsle.net
 
The scripts are called from cron.

I could modify all the scripts writing one more perl script, that's not a problem. But scripts once modified, it's the testing that takes lots of time.

 
Once modified, you could run "perl -c script.pl" (or it might be -s... at any rate, it checks the scripts for syntax errors). If the "use module" line was added correctly, there shouldn't be any syntax errors, and if you later find an error that has to do with the module... you just have to edit the module, not each individual script.

-------------
Cuvou.com | The NEW Kirsle.net
 
You might be able to use a newer feature and place the auto exec sub-routine in:

$Config{siteperl}/sitecustomize.pl

This was added around 5.8.7 I think. You can diasable it with the -f flag.

Hope that helps.

Usige
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top