Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^2: Introducing Class::InsideOut

by johnnywang (Priest)
on Jan 09, 2006 at 21:13 UTC ( #522043=note: print w/replies, xml ) Need Help??

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

Can someone explain what this is about? the difference in syntax? and its relation to Perl6? Thanks.

Replies are listed 'Best First'.
Re^3: Introducing Class::InsideOut
by xdg (Monsignor) on Jan 09, 2006 at 23:20 UTC

    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


    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?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2021-09-19 09:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found