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

Hi,
I am trying write a module for encrypt and decrypt. And in my main perl program , i would like to call the encrypt and decrypt method. First time i am writing a module. So can any one help me to do how should i call the encrypt and decrypt function.
Following are my module code

package Config::Confmanager; use Exporter; #export all the methods @EXPORT=qw(&Encrypt,&Decrypt); use warnings; use strict "refs"; use Crypt::CBC; #CPAN Module For Encryption And Decryption #Creating Constructor; sub new { my($self) = {}; bless ($self); return ($self); } #--------------------------------------------------------------------- +---------------------------------------- #Encrypt And Decrypt Method #--------------------------------------------------------------------- +--------------------------------------- sub CreateEncryptDecryptObject { $cipher = Crypt::CBC->new( {'key' => 'passport', 'cipher' => 'DES', 'iv' => '$KJh#(}q', 'regenerate_key' => 0, # default true 'padding' => 'space', 'prepend_iv' => 0 }); } #--------------------------------------------------------------------- +---------------------------------------- #Encrypt Method #Takes An Option Value As Plain String And Encrypts It #--------------------------------------------------------------------- +--------------------------------------- sub Encrypt { CreateEncryptDecryptObject(); my($encryptstring) = @_; my $cipherencryptstring = $cipher->encrypt($encryptstring); if (!$cipherencryptstring) { $log->error("Could not Encrypt requested string\n",); return ; } else { return($cipherencryptstring); } } #--------------------------------------------------------------------- +------------------------------------------- # End Of Encrypt Method #--------------------------------------------------------------------- +--------------------------------------------- #--------------------------------------------------------------------- +---------------------------------------- #Decrypt Method #Takes An Encrypted String And Decrypts It #--------------------------------------------------------------------- +--------------------------------------- sub Decrypt { CreateEncryptDecryptObject(); my($decryptstring) = @_; my $cipherdecryptstring = $cipher->decrypt($decryptstring); if (!$cipherdecryptstring) { $log->error("Could not Decrypt requested string\n",); exit; } else { return($cipherencryptstring); } } 1; #--------------------------------------------------------------------- +------------------------------------------- # End Of Decrypt Method #--------------------------------------------------------------------- +------------------------------------------- #--------------------------------------------------------------------- +------------------------------------------ =head1 NAME cut #--------------------------------------------------------------------- +------------------------------------------


Main program i would like to call Encrypt and decrypt mehtod and pass the string.
Help me
Regards,
Jh

janitored by ybiC: Balanced <readmore> tags around longish codeblock, to reduce vertical scrolling

Replies are listed 'Best First'.
Re: How to call Module
by ambrus (Abbot) on Aug 24, 2004 at 16:16 UTC
    @EXPORT=qw(&Encrypt,&Decrypt);

    You probably want

    @EXPORT=qw(Encrypt Decrypt);
    instead. And turn on warnings.

    Update: ok, you have use warnings, just too late to catch that mistake.

      Actually he shouldn't want to use @EXPORT at all. Use @EXPORT_OK and be explicit in the import. Always.

      This avoids confusion in tracking down where a function named foo came from.

        Well, that's a matter of taste. Also, one should say

        use base "Exporter";
        which is a better style imo than
        use Exporter; @ISA = "Exporter";

        There are other style errors too in the code, like not storing $cipher in an our variable, or at least declaring it; create the $cipher object only once (if the module allows that); either not having the useless new function or converting Encrypt and Decrypt to methods; not using the one-argument bless in the new method; using strict; calling die instead of exit on an error; etc. These are, however, minor errors, and we should probably first help the OP correcting the more serious errors of the code (ie making the code work)

        Sorry if this insulted you.

Re: How to call Module
by Grygonos (Chaplain) on Aug 24, 2004 at 16:16 UTC

    edit:ah yes... I would pass the sub names as bareword into the @EXPORT array.

    edit2: tilly's right. it should be in @EXPORT_OK, otherwise you risk namespace collision, and as tilly said you have no idea where the sub originated from in your main program, unless you do explicit importing of sub names when you  use your module

    What does your client look like? You must import the function names via

    use Config::Confmanager qw{Encrypt Decrypt};
    Also you may need to put
    require Exporter; our @ISA = ("Exporter");
    in your module code.

      Hi all,
      Thanks a lot for your help.
      I made the above said changes in the module. But i am not able to call the function from module to my main program.
      Can you pls help me to solve.
      Thanks & Regards
Re: How to call Module
by Zed_Lopez (Chaplain) on Aug 24, 2004 at 17:22 UTC

    Package != Module != Object class

    perlmod perlmodlib perlmodstyle perlboot

    Your code suggests that you really want to just use a module for library routines, rather than to create an object class... you're pretty much ignoring the point of object-oriented programming by having your methods try to create disposable objects to perform single operations (and you're not storing the results of CreateEncryptDecryptObject, so it's a no-op, anyway.) Further, if your methods were actually invoked through objects, they'd have the object itself as the first item in @_, meaning that, as written, your Encrypt and Decrypt methods, would be trying to encrypt and decrypt the object reference. (And your uses of $log are going to fail... they'll be trying to use $Config::Confmanager::log which will be undefined.)

    How you would use these in a program would depend on whether you go with OOP or not.

    Here's one easy way with a closure and an ordinary, non-object-oriented module:

    package Config::Confmanager; use warnings; use strict; require Exporter; our @ISA = 'Exporter'; our @EXPORT=qw(Encrypt Decrypt); use Crypt::CBC; #CPAN Module For Encryption And Decryption { my $crypto = Crypt::CBC->new( {'key' => 'passport', 'cipher' => 'DES', 'iv' => '$KJh#(}q', 'regenerate_key' => 0, # default true 'padding' => 'space', 'prepend_iv' => 0 }); sub Encrypt { my($encryptstring) = @_; my $cipherencryptstring = $crypto->encrypt($encryptstring); die "Could not Encrypt requested string" unless $cipherencryptstri +ng; return $cipherencryptstring; } sub Decrypt { my($decryptstring) = @_; my $cipherdecryptstring = $crypto->decrypt($decryptstring); die "Could not Decrypt requested string" unless $cipherdecryptstri +ng; return $cipherdecryptstring; } } 1;

    And here's how you'd use it.

    #!/usr/bin/perl use strict; use warnings; use Config::Confmanager; my $cleartext = "la dee dah"; my $encrypted = Encrypt($cleartext); my $decrypted = Decrypt($encrypted); print "$cleartext\n"; print "$encrypted\n"; print "$decrypted\n";

    (I agree with tilly about @EXPORT_OK, but didn't change it in this example to keep it just a little closer to your original.)

Re: How to call Module
by gaal (Parson) on Aug 24, 2004 at 16:58 UTC
    Your question has been answered, but if this is the first time you're writing a module, you should really take a look at perlmod.

      I remember reading perlmod when I was trying to learn how to write modules. And I also remember the deep feeling of dread and terror it left me with. It's a great reference document, but I'm not so sure a module newbie will appreciate all the detail. Instead, I'd point new module writers to 'Simple Module Tutorial', in our very own Tutorials section.

        Hmmm, okay, I update my recommendation:

        Keep the whole perlmod doc handy, and at least read the Perl Modules section in it.

        Tutorials can also be very effective, of course!