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

Fellow Monks,
In the book Learning Perl Objects, References and Modules, there is a code example that look like this:
{ package Animal; sub speak { our $class = shift; print "a $class goes ", $class->sound, "!\n"; } } { package Cow; our @ISA = qw(Animal); sub sound { "moooo" } } Animal::speak("Cow");
Are the curly braces around each package necessary? the code appears to work without them. What purpose do they serve?

Thanks
Jonathan

Replies are listed 'Best First'.
Re: package question
by merlyn (Sage) on Jan 09, 2006 at 17:12 UTC
    They limit the scope of the package directive, since package names are lexically scoped.

    The examples would work without it, as long as you remember to add package main; before the main part of your code.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: package question
by Roy Johnson (Monsignor) on Jan 09, 2006 at 17:14 UTC
    They limit the scope of the package statements. The net effect is that the last line is in package main without your having to specify it.

    Caution: Contents may have been coded under pressure.