Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: oop, variable that counts the number of objects created

by tobyink (Canon)
on Jul 30, 2020 at 09:40 UTC ( [id://11120056]=note: print w/replies, xml ) Need Help??


in reply to oop, variable that counts the number of objects created

Just a random example using Zydeco...

use Zydeco prefix => 'App'; class Car { has name ( type => Str ); has price ( type => Num ); has speed ( type => Num, default => 0, handles_via => 'Number', handles => { speed_up => 'add', slow_down => 'sub', }, ); my $count; method BUILD { ++$count } method DEMOLISH { --$count } method count { $count } } my $red_car = App->new_car( name => 'Red' ); my $blue_car = App->new_car( name => 'Blue' ); $red_car->speed_up( 25 ); $red_car->speed_up( 10 ); say $red_car->speed; say App::Car->count;

Replies are listed 'Best First'.
Re^2: oop, variable that counts the number of objects created
by perlfan (Vicar) on Jul 30, 2020 at 16:22 UTC
    not germane to OP but generally relevant

    Nice to see this example. I should probably ask this in another thread, but what's the obsession with has? Can't we (everyone not just your modules) just have blocks all the way down or superficially support something that's familiar to JAVA programmers? At this point we're just cargo culting a failed declarative approach (IMO). What are we getting beyond classic Perl OO? (sincerely want to break this down for the benefit of the overall discussion):

    • protected methods
    • strong types/checking
    • strong data encapsulation
    What else? If this has been hashed out before (a true breakdown), I'd be satisfied with a link. TY!

      Why would declaring object member variables with blocks be familiar to Java programmers?

      # Java class Car { public String name; public int price; public int speed; } # Zydeco class Car { has name ( type => Str ); has price ( type => Int ); has speed ( type => Int ); }

      Where would you add blocks to the Zydeco example to make it more like Java?

        Just a general thought, why is has so pervasive? Idk I probably should think more about what I am really trying to figure out. This is not about your modules specifically.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found