Re: using pl/pm and require HELP !
by Aristotle (Chancellor) on Jul 16, 2003 at 16:39 UTC
|
| [reply] |
|
|
thank you so much Aristotle, I shall try it first thing in the morning !, meanwhile, now where is that perldoc...
I shall let you know how I get on.
what an excellent monastery this is
| [reply] |
|
|
Fantastic ! it worked first time (of course!) thanks muchly,
I had actually tried $main:: to no avail so thanks for that .
| [reply] |
Re: using pl/pm and require HELP !
by antirice (Priest) on Jul 17, 2003 at 08:23 UTC
|
You're not requiring the script in the pm where you need the subroutine. You can either call it as main::subroutinefrom_myperlscript() or require "/somepath/myperlscript.pl" at the top of mypack.pm. In either case, you really need to review what you're doing. If you want your module (pm) to be at all portable, build your pm's within a somewhat sane manner. The requiring of a particular sub to exist from within the main namespace (the executing script) is fairly interesting. Of course, anytime the sub doesn't exist their script will break. With adding the require within the module, you are introducing some installation nightmares. Modules are supposed to be fairly easy to install without any (well, at least not a lot of) editing. You could probably make myperlscript.pl into a module that exports the sub you're looking to use. Then you can just use myperlscript; and get the functionality for which you're looking. Some suggested reading: require, use, perlmod, and perlmodstyle.
If you have a sub in myperlscript.pl that must be shared with legacy code, that's all right. Just don't use it anymore. Please do not repeat the mistakes of a previous programmer. If you come along something that isn't correct, note it so you can fix it later. When you're programming, please consider the poor sap coming in behind you. If you are the poor sap, do not make things harder on yourself. You're presently updating this code and in the future you may need to update it again. Just make myperlscript.pl into a module that can export the sub you need.
antirice The first rule of Perl club is - use Perl The ith rule of Perl club is - follow rule i - 1 for i > 1
| [reply] |
Re: using pl/pm and require HELP !
by dragonchild (Archbishop) on Jul 16, 2003 at 17:08 UTC
|
If you can, I would rewrite the file to be a perl module that inherits from Exporter. That would make the interface a lot simpler.
No matter what, I would do the require in the file that actually is using the subroutine. So, regardless of what you choose to do, put the require in mypack.pm.
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] |
|
|
Thank you so much for your reply - yes I would gladly put the code into another package - unfortunately the sub I need is shared elsewhere - and I haven't the time for refactoring at this stage... legacy code and all that!
but I may be trying the main:: which might be of use.
thanks,
all the best
| [reply] |
Re: using pl/pm and require HELP !
by bart (Canon) on Jul 17, 2003 at 12:24 UTC
|
If that one little thing fixed your problem, this can't be your real code. You see,
require /somepath/myperlscript.pl;
isn't valid perl. You need to quote the path.
require "/somepath/myperlscript.pl";
... or use barewords for the filename.
| [reply] [d/l] [select] |
Re: using pl/pm and require HELP !
by nofernandes (Beadle) on Jul 17, 2003 at 12:11 UTC
|
#sum.pl
sub sum{
my($a,$b)=@_;
my $res=$a+$b;
return $res;
}
1;
Calling this file with require
require "sum.pl";
$num1=5;
$num2=10;
$result=&sub($num1,$num2);
print "$result";
I hope this helps and that i had understand the correctaly the question!!
Nuno | [reply] [d/l] [select] |
Re: using pl/pm and require HELP !
by tcf22 (Priest) on Jul 17, 2003 at 12:14 UTC
|
First of all I think that your design is flawed, because you shouldn't require a script in order to make a module work, but that is besides the point.
My guess is that, require puts &subroutinefrom_myperlscript();, in the main package, and the .pm file is using another package. Try
&main::subroutinefrom_myperlscript(); | [reply] [d/l] [select] |
Re: using pl/pm and require HELP !
by hmerrill (Friar) on Jul 17, 2003 at 15:00 UTC
|
I've never done it that way, and I'm honestly not sure if it *can* be done that way.
The way I've always done it is to put subroutines in a package, and then 'use mypkg' the package, and then access the subroutine by doing 'mypkg::mysub();'. It's been a while, but I think that works :)
HTH. | [reply] |