Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Using Modules

by naikonta (Curate)
on Oct 14, 2007 at 02:40 UTC ( #644698=note: print w/ replies, xml ) Need Help??


in reply to Using Modules

This is one way to do so,

my $_madfile = "MA::$_mad->{name}"; my $_cardCharged = eval qq{ require $_madfile; $_madfile::chargeCard(); }; die $@ if $@;

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


Comment on Re: Using Modules
Download Code
Re^2: Using Modules
by Anonymous Monk on Oct 21, 2007 at 01:16 UTC
    I could not get either one of those to work... I have tried EVERYTHING I can find, and none of them work. I think it is because of the explanations, that doing an eval puts that file ahead of everything else, therefore any variables in there are not yet set... That file depends upon a bunch of variables that are set before that is pulled in.

    I guess I have to hard code the whole thing, then everytime we add new ones or take them away, I'll have to change the code :(

    I do see this UNIVERSAL::require, however I don't see how I can use the $_madfile::chargeCard(); part of the module I need. I think it would just let me use the chargeCard as a single sub, but if I pulled in several of the modules, the subroutines would be called the same way, thus I don't think it would work.

    If anyone thinks of a way this could work, I would really appreciate it if you'd tell me on here.

    Richard
      Ok, I did find something that works, partly...

      UNIVERSAL::require, did work partly
      Here is how I did it:
      use UNIVERSAL::require; my $module = 'MA::' . $_madType; $module->require; my $_cardCharged = $module->chargeCard($_mad_merid);
      That does run that subroutine, chargeCard.

      The problem is that it runs it before anything and everything, so even my $dbh database connection is not set, so I get errors like this:
      Can't call method "prepare" on an undefined value at /path/to/modules/ +MA/nm.pm line 18.
      so I know it is like it is making my module execute before EVERYTHING else.

      Is there a way to make this work, where it executes it right where it is at, so that the rest of my code will work with those in there?

      I don't usually use my own modules, normally I do everything locally. Am I allowed to use variables I have in my main perl program in modules? Or am I supposed to import them all in, some way?

      Thank you all very much for any advice you can be.

      Richard
        I figured it out...

        I'm not going to use them as modules... Since I have to build the gateway subroutine named chargeCard, I would just name it the name of the file + chargeCard, then when I pull the gateway to use out of the database, I'll call that subroutine that way:
        my ($_merid,$_mertype) = $dbh->selectrow_array(qq{SELECT `id`,`type` F +ROM `merchants` WHERE #SomeWhereStatement#}); require "/path/to/modules/$_mertype.conf"; my $_subvar = $_mertype . "chargeCard"; my $cardCharged = &$_subvar($_merid);
        and that does work... so now all I have to do is when I program each gateway, I just have to add it to the database first, figure out what type I made it then add that type to the forefront of the chargeCard subroutine name. And that will make them all dynamic, where I can use more than one at the same time... in case one gateway declines the transaction for some dumb reason other than no funds available.

        Just thought I'd update this so people do not have to waste their time reading and trying to help me.

        thx,
        Richard

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://644698]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (13)
As of 2014-09-18 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls