Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: What is method () ?

by revdiablo (Prior)
on Oct 05, 2005 at 17:53 UTC ( [id://497683]=note: print w/replies, xml ) Need Help??


in reply to What is method () ?

Completely unrelated to the topic at hand, I noticed you have your packages declared with:

package foo; { ... }

Which I don't really understand. It makes it look almost like the package is defined within the block. But it's really a bare block with a package declaration just before it. That means the package declaration won't be limited to the block, as it would with something like:

{ package foo; ... }

So while your style looks nice, I think it might be misleading, and I'm wondering if there's any reason you use it that I'm not thinking of.

Replies are listed 'Best First'.
Re^2: What is method () ?
by jdhedden (Deacon) on Oct 05, 2005 at 20:08 UTC
    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.

      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.

      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:>*
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://497683]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found