Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Using Modules

by Anonymous Monk
on Oct 13, 2007 at 01:43 UTC ( #644582=perlquestion: print w/replies, xml ) Need Help??

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

I built our website to be 100% automated and dynamic, including adding additional merchant accounts.

I created an administrative back office to manage the website, including adding new merchant accounts.

I was building it where they had the same subroutine in the file, named: chargeCard.

Then I named the file the name of the database Name, and then when it picked the Merchant Account to use I would do a require:
require "/home/path/to/ma/files/$_fileName.conf";
however, we have a 25M limit with one bank that the gateway is a custom gateway that does not support discover cards yet, so I have my programming pick one of the authorize.net accounts we have if it is a discover card and then all others go to the main merchant account.

Now we discovered that we are getting about 30 declines per day, which when we force them to one of the authorize.net accounts it is approved 9 out of 10 times.

So now I want to use BOTH in the instance that the first one is declined. In view of that I made them modules.

My question is this, how can I pull them in, if the file to use is a variable:
use strict; my ($sth, $_mad); $sth = $dbh->prepare(qq{SELECT * FROM `merchantAccounts` WHERE `u` = " +1" AND (`volume_capable` < (`current_volume` + $_GrandTotal))}); $sth->execute(); $_mad = $sth->fetchrow_hashref(); $sth->finish(); my $_madFile = $_mad->{name}; use MA::$_madFile;
Of course with that I get an error: syntax error at /path/to/subs.data line 174, near "use MA::$_madType"

I've also tried that code with this 'use' statement:
my $_madFile = 'MA::' . $_mad->{name}; use $_madFile; # AND I've tried it this way: use "$_madFile";
I get the same errors every time.

So How can I do this with a variable name, so that I can pull in several files at one time and call them like this:
$_cardCharged = MA::$_madFile::chargeCard();

I'd appreciate any assistance or pointers you have for me.

Richard

Replies are listed 'Best First'.
Re: Using Modules
by chromatic (Archbishop) on Oct 13, 2007 at 02:29 UTC

    The easiest solution is probably:

    eval "use $_madFile"; # or... eval "use MA::$_madFile";

    use statements execute right after Perl compiles them, before it starts running the rest of the program. If they contain variables, those variables will contain undefined values (because they didn't get set yet).

Re: Using Modules
by naikonta (Curate) on Oct 14, 2007 at 02:40 UTC
    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!

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2019-08-24 18:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?