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

and can there be a perl module which contains no functions? will the code in it be excecuted if its in no function.. its just under the package declaration? thanx ketaki

Replies are listed 'Best First'.
Re: when is a perl module executed?
by GrandFather (Saint) on Jul 17, 2008 at 03:04 UTC

    A module is compiled when it is used. Anything not inside a sub in the module is executed during the load process.

    The actual sequence is complicated slightly by BEGIN blocks which are executed before anything else in the current source file.

    Simplistically the execution sequence is: BEGIN blocks, use statements, source code, END blocks. Each source file processed as the result of a use follows the same execution sequence so use statements result in nesting.


    Perl is environmentally friendly - it saves trees
      it doesnt seem so. i added a statement which is written below into an already existing module which i had written. the line which i added is
      print "aaaaaaaaaaaaaaaaaa";
      and it doesnt print that text (aaaaaaaaaaaa) when the perl file is executed. Is it becoz im executing the perl file on the webserver and seeing the output on the browser on the internet. the perl file is a cgi file. the rest of the program runs. only this text (aaaaaaaaaaaaaaaaaaaaa) is not being printed anywhere.
        and it doesnt print that text (aaaaaaaaaaaa) when the perl file is executed.

        If it doesn't, then print is broken in Perl and no one has noticed all these years. print is probably not broken in Perl.

        It's more likely that that (unbuffered) print occurs before you've finished printing HTTP headers to the browser, so it's getting lost somewhere that you don't see it. Run your program from the command line instead to see that, indeed, print works in Perl as it has worked for 20 years.

        Whatever you are trying to do, it sounds wrong. You are abusing the normal use of modules. Write a sub in the module and have the main script call it.


        Perl is environmentally friendly - it saves trees
        You didn't pay off the Executioner :p
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: when is a perl module executed?
by hangon (Deacon) on Jul 17, 2008 at 03:04 UTC

    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;
      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

        "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???
Re: when is a perl module executed?
by apl (Monsignor) on Jul 17, 2008 at 10:54 UTC
    May I ask why you didn't write a Perl script to test this, rather than asking?