Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Copying complete module code into a script

by Daddio (Chaplain)
on Aug 01, 2001 at 19:33 UTC ( [id://101417]=perlquestion: print w/replies, xml ) Need Help??

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

The company at which I work, being the funny place that it is, still doesn't have Perl on the "accepted language" list.

I wrote some project code to do MIME-attachment bulk emailing (legitimate bulk email to clients, not spam). Yesterday, the Project Manager asked if the tool used modules, and of course I said "Yes." I shouldn't have. He then asked if I could somehow include the module code in the script, since getting a production waiver (imagine that) to put one Perl script in production would be easier than getting three, one for the script, and one each for the modules I am using (I think that is all I need; the rest are already loaded - 5.005_02).

I tried just copying the module code into the script, but that doesn't seem to work. I looked through everything I could find in Super Search, but didn't find anything I thought could help.

I know I am probably opening a can of worms by asking about this, but is there any way I can technically do this, whether it is right or wrong to do so? As far as my use statements, I only have Text::Autoformat and MIME::Lite.

D a d d i o

Replies are listed 'Best First'.
Re: Copying complete module code into a script
by tachyon (Chancellor) on Aug 01, 2001 at 19:44 UTC

    Yes you can technically do this. At this node I did just that to make it easy to run the example.

    When you say doesn't seem to work what exactly do you mean. Compile time errors, runtime errors, wrong output, no output? How did you do the cut and paste. Did you include the package names? You need to give your script a package name too or paste the modules at the end of the script so that your script defaults to main and the modules have their proper package names.

    Part of the module rational is to allow you to simply update the one module and (hopefully) all the scripts that use it then benefit from the bug fixes/patches...Doesn't always go that smoothly when the author changes the interface but that is another story.

    Update

    I have just tested MIME::Lite by pasting one of the examples given at the top of the module and it produces the expected output - ie a MIME encoded string.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Copying complete module code into a script
by tadman (Prior) on Aug 01, 2001 at 19:40 UTC
    Your code would have to look something like:
    # Module Pasted Here package Something; # Code # End Paste package main; #use Something; # Your code
    If you forget to switch back to package main, or you still try and use the module, you're not going to go very far.

    I'm thinking that a quick bit of code would allow you to "attach" modules to your program, perhaps in the __DATA__ section, such that they could be extracted and eval'd. This isn't going to do wonders for the speed of your program, though.
Re: Copying complete module code into a script
by bikeNomad (Priest) on Aug 01, 2001 at 20:00 UTC
    You might want to look at the utility I just posted in Code called Self-extracting Perl archives.

    Of course you can technically do it! This is Perl, after all...

Re: Copying complete module code into a script
by Zaxo (Archbishop) on Aug 02, 2001 at 02:09 UTC

    As for the rights and wrongs, I believe this is ok as long as the licence permits it. You need to check that. At a minimum, you must:

    1. Preserve all copyright notices and license references
    2. Make absolutely sure your company doesn't get the idea it owns the copied code.

    After Compline,
    Zaxo

Re: Copying complete module code into a script
by merlyn (Sage) on Aug 02, 2001 at 02:56 UTC
Re: Copying complete module code into a script
by Daddio (Chaplain) on Aug 02, 2001 at 17:55 UTC

    Thanks for the help and suggestions.

    I tried the similar formats of tadman and tachyon, and that worked fine for MIME::Lite, but wouldn't quite get Text::Autoformat to load. I took a look at bikeNomad's Self-extracting Perl archives, but that didn't quite fit this situation. Zaxo, thanks, and I will make sure this applies. I tried merlyn's No excuses about not using CGI.pm, but even that wouldn't handle Text::Autoformat.

    I talked to the PM for the project again, and I think I have convinced him that the "better" way to do this, since he will not ask to have modules installed properly, is to elevate the modules locally rather than internal to the script.

    Even if I do get the modules elevated, I still wonder why I can't seem to get Text::Autoformat to load internally. Using merlyn's technique, I was getting these errors when run on the command line:

    Missing right bracket at mimemail.cgi line 6227, at end of line syntax error at mimemail.cgi line 6227, at EOF

    This confuses me, because as I check, there are no missing brackets, and line 6227 is the last line of the script. I copied modules into the script, and only removed the __END__ lines.

    When trying the other method (just including the modules without the BEGIN { } blocks), the script returns nothing at all when run on the command line.

    I probably shouldn't be taking too muh time with this since I will probably not need to use this method, but I am still curious. Any ideas? Thanks...

    D a d d i o

      Of course, the problem with a missing right bracket is that perl keeps looking for it till it reaches the end of the file. So that damned error _always_ gives the last line! The best bet is a syntax sensitive editor. Though come to think of it it could be a good use for Text::DelimMatch...
      (It would be nice if perl reported the line of the left bracket, but I guess that would be yet more complications in the parser.)

        p

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found