Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to call Module

by Zed_Lopez (Chaplain)
on Aug 24, 2004 at 17:22 UTC ( [id://385461]=note: print w/replies, xml ) Need Help??


in reply to How to call Module

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-24 01:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found