Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Introducing Class::InsideOut

by xdg (Monsignor)
on Jan 09, 2006 at 23:20 UTC ( [id://522065]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Introducing Class::InsideOut
in thread Introducing Class::InsideOut

From my AnnoCPAN comment:

These package blocks are only necessary if multiple classes are being defined in the same file, or there are mixed classes and code. (Which is what the synopsis shows.) They keep the lexical variables for each in separate scopes. If a single file holds only a single class, the enclosing file scope is sufficient and the braces aren't necessary.

Perl 6 uses a class declaration with braces. From Apocalypse 12, here's an example of a Perl 6 class:

# A Perl 6 class class Point { has $.x; has $.y is rw; method clear () { $.x = 0; $.y = 0; } }

Since a Perl 5 package statement isn't exactly the same as a Perl 6 class statement, it's potentially confusing to write this:

# A Perl 5 class package Point; { use Object::InsideOut; my @x :Field; my @y :Field('Accessor' => 'y'); sub clear { my $self = shift; $x[$$self] = 0; $y[$$self] = 0; } }

In Perl 5, the braces are just defining a lexical scope, which inside-out objects happen to use to create encapsulation. But unless there are other lexical scopes in the file that defines the Perl 5 class, the braces aren't necessary and writing them on the same line may make people think it's related to the package statement. As an example, consider this version with the braces moved:

# A Perl 5 class -- take 2 package Point; { # same guts as before } # still part of package Point outside the braces # but @x and @y can't be seen from out here

Now consider what happens if the package statement is inside the braces instead:

# A Perl 5 class -- take 3 package Foo; # just for later clarity { package Point; # same guts as before } # Back to package Foo out here

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-23 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found