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

I have a problem in a package that is trying to use a subroutine from a file that has been imported using "require". for some reason I cannot make a call to the sub. The Structrue goes somehting like this:

main.pl

use mypack; require /somepath/myperlscript.pl; my $x= new mypack(); print $x->get_y();
mypack.pm
my $y = &subroutinefrom_myperlscript(); $self->set_y($y);
can anyone tell me how I can get the &subroutinefrom_myperlscript() to be invoked - I am sure I have the syntax wrong and am convinced I can use libraries of perl pl files within a package (or this a plbcak?) many thanks, in desparation considertheant

update (broquaint): added <code> tags

Replies are listed 'Best First'.
Re: using pl/pm and require HELP !
by Aristotle (Chancellor) on Jul 16, 2003 at 16:39 UTC
    You need to prepend the package name to the function name. Is there a package statement in myperlscript.pl? If not, you'll need to write main::subroutinefrom_myperlscript(), if yes, you'll have to substitute that package name for main. You probably want to have another look at perldoc perlmod.

    Makeshifts last the longest.

      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
      Fantastic ! it worked first time (of course!) thanks muchly, I had actually tried $main:: to no avail so thanks for that .
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

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.

      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
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.
Re: using pl/pm and require HELP !
by nofernandes (Beadle) on Jul 17, 2003 at 12:11 UTC

    Hi..

    At the end of your myperlscript.pl you must put "1;" in order to return a true value!!

    Example:

    #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
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();
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.