Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Creating a module

by TStanley (Canon)
on Feb 13, 2001 at 22:12 UTC ( #58174=perlquestion: print w/replies, xml ) Need Help??

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

I have been tasked to add some features to my company's Perl API to allow
for the encryption of credit card information. I am using the Perl implemenation
of the Blowfish encryption algorythm (Blowfish_PP) and Crypt::CBC modules to
do this. I am also using the ConfigFiles module to get the key information from
the .ini file. To make sure my module worked, I wrote a short script to test it,
but I received the following back:
Encryption.pm did not return a true value at test.pl line 3. BEGIN failed--compilation aborted at test.pl line 3

This is the test script I wrote:
#!/usr/bin/perl -w use strict; use Encryption; my $ENC=Encryption->new(); my $TestString="This is a test"; my $EncryptedString = $ENC->encrypt_acctnumber($TestString); print"\$EncryptedString is : $EncryptedString";

And here is the module:
#!/usr/bin/perl -w use strict; use ConfigFile; use CBC; use Blowfish_PP; use Env qw (INI); package Encryption; sub new{ my $pkg = shift; # Default to the .ini file in the current directory if(!defined($INI)){ $INI = "test.ini"; } # Read the .ini file for the key setting my $ini = ConfigFile->new($INI); my $KEY = $ini->Parameter('Key','1'); # Create the Encryption object my $CBC = new CBC($KEY,'Blowfish'); return bless $pkg; } sub encrypt_acctnumber{ my $encrypted=$CBC->encrypt_hex($_[0]); return $encrypted; }
From what I can tell by the error code being returned, it can't even find the module
in question. Any help would be appreciated. Thanks in advance.

TStanley
In the end, there can be only one!

Replies are listed 'Best First'.
Re: Creating a module
by japhy (Canon) on Feb 13, 2001 at 22:14 UTC
    Please check the documentation for further information about error messages. Here, I used the splain program, which comes with Perl:
    jpinyan@voyager-dev [12:12pm] ~/web #330> splain /usr/local/bin/splain: Reading from STDIN Encryption.pm did not return a true value at test.pl line 3. Encryption.pm did not return a true value at test.pl line 3 (#1) (F) A required (or used) file must return a true value to indicate + that it compiled correctly and ran its initialization code correctly. +It's traditional to end such a file with a "1;", though any true value +would do. See perlfunc/require.


    japhy -- Perl and Regex Hacker
      I added the 1; to the end of the module, and that solved that little problem. I've still
      got others, but I think I can figure them out by myself.

      TStanley
      In the end, there can be only one!
Re: Creating a module
by Crulx (Monk) on Feb 13, 2001 at 22:35 UTC
    Ahh, The flurry of posts on this one is going to be great. As the answer is 1; we should deleve into the depths of this problem in detail. The question you should be asking yourself, young one, is "Why didn't I believe the compiler?"

    Obviously, you have never learned that perl modules need to return a true value at the bottom of the code. This answer is obvious. Where then, could you have gotten this information from?

    1. You could go to perl monks and ask. Which you did... not a bad way, and it gives us something to talk about. But I'm sure right now that you asked on the chatbox.
    2. read perldoc perlmod It clearly states in there how to go about building a module including using the true value at the end.  1;  # don't forget to return a true value from the file
    3. You could have read perldoc -f use. This would have told you
      BEGIN { require Module; import Module LIST; }
      which hopefully would have sent you to perldoc -f require. There it states
      $result = do $realfilename;
      and
      delete $INC{$filename} if $@ || !$result;
    4. Read another perl module. As your syntax is fine (and easy to check with perl -c), you must have realized that you were missing something specific to modules. Another module would have answered that for you.
    Oh Monk! This is not a rebuke of your programming skill, you know now what you need.
    Oh Monk! This Sangha is here to help! But asking here is like making a wish, you may get what you asked for, and much more.
    Oh Monk! Strive unceasingly towards Perl Perfection. You have violated the Law of Laziness, far easier to ask a quick question than post a long code snipit.
    ---
    crulx
    crulx@iaxs.net
Re: Creating a module
by davorg (Chancellor) on Feb 13, 2001 at 22:17 UTC

    Perl modules need to return a true value. This is usually done by including

    1;

    at the end of the module file.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Now see, I spent the last half a day looking for this information. I should have come here first. :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-12-09 13:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?