in reply to Re: What is method () ?
in thread What is method () ?

From Perl Best Practices, pg. 328:
That block is vital, because it creates a limited scope,
to which any lexical variables that are declared as part
of the class will automatically be restricted.
The benefits are then explained later on pg. 330-332.

Remember: There's always one more bug.

Replies are listed 'Best First'.
Re^3: What is method () ?
by revdiablo (Prior) on Oct 05, 2005 at 22:30 UTC

    It's true that the block will limit the scope of lexical variables, but my point is that the code you have won't limit the scope of the package declaration. We can see how the package is still changed, even after the block ends:

    package foo; { print __PACKAGE__; # prints "foo" } print __PACKAGE__; # also prints "foo"

    But compare this, where the package is only changed within the block:

    { package foo; print __PACKAGE__; # prints "foo" } print __PACKAGE__; # prints "main"

    Unless there is some strong further justification in the book (which I would be interested to see, but not interested enough to pay for it), I remain unpersuaded.

Re^3: What is method () ?
by demerphq (Chancellor) on Oct 06, 2005 at 06:48 UTC

    I don't know the context that TheDamian wrote that in, but I would consider it to be misleading as posted here.

    When you have a single package declared in a file there is no need for the braces. The file is a single enclosing lexical scope so the braces are unnecessary, and even possibly confusing.

    OTOH, when you have a mutliple packages declared in a file then you should use the braces as it enforces lexical seperation between the package implementations. But the thing is you probably should put the package declaration inside of the braces and not outside.

    But that leads to the question of why you would put mutliple packages in a single file. I know of a few reasons to do so, but IME its rare that they come up.

    ---
    $world=~s/war/peace/g

      Most of the time when I put multiple package statements in the same file, I do that to show a specific behaviour. To explain something, or to show a bug. Like a posting to Perlmonks or usenet. Or on a mailinglist. ;-)
      Perl --((8:>*

        Well, my response was more motivated by the comment "yeah but Perl Best Practices says you should do it".

        So why does Perl Best Practices say it? I don't know. As I said I don't have the context to see exactly what TheDamian was saying. Basically I dont think it is "best practice" to do this. It is a good idea when multiple packages are in a single file, but its of minimal value when you only have one, and in that case IMO is actually bad practice as it increases indentation with no benefit.

        My guess based on the quoted text is that TheDamian meant the case of multiple packages in one file and not a general advisory. Which just says that taking PBP quotes out of context isn't so wise.

        ---
        $world=~s/war/peace/g

Re^3: What is method () ?
by Roger_B (Scribe) on Oct 06, 2005 at 12:44 UTC
    TheDamian's benefits relate to hashes scoped to the block that are used hold the attributes of instances to the class, making them properly declared and truly private.

    The block is redundant in your code as no such variables exist, and your classes appear to be using the usual idiom of blessing an anonymous hash to store attributes, whereas TheDamian's code blesses an anonymous scalar so that this idiom can't be used.

    Roger_B