in reply to when is a perl module executed?

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

Replies are listed 'Best First'.
Re^2: when is a perl module executed?
by ketaki (Acolyte) on Jul 17, 2008 at 03:24 UTC
    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 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?
      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.