in reply to when is a perl module executed?

Sure, try this. Just curious, why do you want to?

# module package foo; print ' foo'; 1;
# calling script use foo; print ' bar';

update: also

# calling script print ' bar'; use foo;

Replies are listed 'Best First'.
Re^2: when is a perl module executed?
by ketaki (Acolyte) on Jul 17, 2008 at 03:16 UTC
    i tried this code. it doesnt give me any output. it doesnt print 'foo'.

      Ensure that the capitalization for the package name and the file name match and are the same as used in the use statement.

      Note that all lower case module names are reserved for pragmas and should not be used for user modules.


      Perl is environmentally friendly - it saves trees
        no its not printing 'foo'. is that because this perl file output im seeing on the browser (the perl file is a cgi file), on the internet?this module made by me resides in a diffrent directory, and i have written
        use lib '/home/www/xxxxxxxxxxxxxxxxxx.org/modules';
        before i write the use statement, in the perl/cgi file.

      "Dear" ketaki,

      you're basically saying this over and over: please don't! There's no reason to increase the noise/signal ratio of this (and any) thread. Especially annoying is your dude talk, which can make communication difficult for an international audience like the one we have here and has no reason to be, anyway. Even more annoying is the typo in the title: in the body of a post it is much more tolerable - and happens all the time, to everybody, but there it will get replicated in replies (unless people correct it manually, but then you're forcing them to do so...) May I ask you to be so gentle and pay more attention for the future?

      --
      If you can't understand the incipit, then please check the IPB Campaign.

      You need a little more to get this to work from a web server. Here is a more complete demo incorporating ideas from other replies. Try playing around with the code until you understand what's happening. Also, it is ok to execute code outside of a module's subs depending on what you're trying to achieve.

      package foo; use strict; use warnings; require Exporter; our @ISA = 'Exporter'; our @EXPORT = 'bar'; print "module foo loaded here \n"; sub bar{ print "module foo, sub bar executed here\n"; } 1;
      #!/usr/bin/perl use strict; use warnings; print "main I \n"; # sub called from module bar(); # begin blocks are loaded at compile time # the first BEGIN block will be the first thing to execute BEGIN { # turn on autoflush so you will see print results immediately $| = 1; # plain text header to simplify the print statements for this demo print "content-type: text/plain\n\n"; print "begin block 1 \n"; } print "main II \n"; # modules are also loaded at compile time # consider them to behave like BEGIN blocks use foo; print "main III \n"; BEGIN { print "begin block 2 \n"; }
      you mean generally it is done like this that the code in sub is executed when the sub is called for from the perl file right?.. i mean it is not d right way to have statements (like those which i need to b executed) outside sub's???