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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling function in script 2

Status
Not open for further replies.

cryptoadm

MIS
Nov 6, 2008
71
US
My script has a number of separate functions and each function needs to be ran from another script. I don't want to put each function into another standalone script which would give me something like eight scripts.

example code:
#!/usr/bin/perl

sub x {
print "hello\n";
}

sub z {
print "next\n";
}

exit (0);

In another unrelated script I want to run function x and in another part of the unrelated script I want to run function z. Is it possible like passing arguments to do something like /scripts/my.pl(x) ?
 
Well, the proper way to do it would probably be to implement them in a module, and use that module from your other scripts.

But you could also achieve it by passing the function name as the first parameter of the script and calling the appropriate function.

Code:
#!/usr/bin/perl
use strict;

sub x {
    print "hello\n";
}

sub z {
    print "next\n";
}

eval "$ARGV[0]";

exit (0);

I suspect that would be considered evil and error-prone though, and adding function parameters could be messy.

Annihilannic.
 
You can run functions that are in another script using "require" but the functions should have unique names.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top