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.)


In reply to Re: How to call Module by Zed_Lopez
in thread How to call Module by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.