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

#!/usr/local/bin/perl print "In the main package\n"; #require "program"; &program::simple(); die; package program; print "how to print this"; sub simple { print "Hi from program::simple\n\n"; } As expected the output is In the main package Hi from program::simple Died at ./x.pl line 5.
Question: I would like to execute the block of code in program package which is not contained in any subroutine

ie.. I would like it to print 'how to print this' which is not in any subroutine... Thus I want to uncomment line require 'program'. Both the package have to be in the same file.
Looking the help of fellow monks.
Thanks..

Replies are listed 'Best First'.
Re: require 'package'
by arturo (Vicar) on Feb 01, 2001 at 02:51 UTC

    Short answer: remove the die in line 5. The code will continue to execute.

    Longer answer: a main use of require is for loading up modules which are in different files -- in fact, what follows require (unless it's a Perl version number, IIRC) is interpreted as a filename -- the compiler will look for a file called program.pm in the directories where your system's perl modules live.

    It doesn't really make sense to require a namespace which lives in the same file.

    package is just for declaring a new namespace. You'll usually want a module to declare its own package, but that's the only connection between package and require

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: require 'package'
by btrott (Parson) on Feb 01, 2001 at 02:51 UTC
    If the two package declarations are in the same file, require isn't what you want; the purpose of require is to pull in an outside file. It sounds more like what you want is to put that print into a BEGIN block:
    BEGIN { print "how to print this"; }
    Does that work for you?

    At the same time I'd wonder whether your code setup is really as nice as possible; why do you need this code to be executed at compile-time, and is it possible that your logic could be reworked so that you don't have to put the print out in the open like that? And so on.

Re: require 'package'
by Fastolfe (Vicar) on Feb 01, 2001 at 03:38 UTC
    Any code in your script that is not in a subroutine will be executed serially. All the package directive does is say, "OK, now assume a default namespace of X for everything I declare." The code is still executed straight through the file.

    Things like use and require can be reduced to do with additional logic. All you're doing is loading an external file and executing it in-line (though use does this in the equivalent of a BEGIN { } block, so in a way this all happens before anything else is executed in your script). Usually you see a package declaration in these files, which cause the new subroutines to be declared in a new namespace, but otherwise code that isn't in a subroutine is executed like any other.

    I'm over-simplifying here, since a file is a 'block' with respects to variable scoping, but that's about the only thing multiple files do that's different.

    The bottom line is that since your additional code towards the bottom isn't declared in a subroutine, it will never be reached because you are dieing. A package directive doesn't do all that much, and certainly doesn't do what you think it does.