1 / 32

Object Oriented Programming

Learn best practices for storing data effectively using object-oriented programming in Perl, as well as tips for creating clear and maintainable code.

nibaw
Download Presentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming Programming Perl Chapter 12: "Objects" Cartwright, De Smet, LeRoy

  2. Tuesday's Homework(Processing addresses) Two style suggestions Cartwright, De Smet, LeRoy

  3. Store mixed data in a hash my $record = [$addr,$state,$zip]; $record[1] # Less clear my $record = { 'address' => $addr, 'state' => $state, 'zip' => $zip, $record{'zip'} # More clear Cartwright, De Smet, LeRoy

  4. Memory is cheap • Make multiple copies of data if it's easier to work with my %name_index; my %state_city_index; • Counterpoint: multiple copies makes it easier to diverge Cartwright, De Smet, LeRoy

  5. Store parsed data, not raw strings $name_index{$name} = $line; • Harder to use later $name_index{$name} = { 'state' => $state, 'city' => $city, } • Easier to use later Cartwright, De Smet, LeRoy

  6. Wednesday's Homework Cartwright, De Smet, LeRoy

  7. Interesting problems Cartwright, De Smet, LeRoy

  8. Object Oriented Programming Cartwright, De Smet, LeRoy

  9. A simple module: use FindBin; print "I was run from $FindBin::Bin\n"; • There is a module called "FindBin", and it contains a variable $Bin. Cartwright, De Smet, LeRoy

  10. A quick trick • My module is next to my script • (ex path/main.pl) % ./main.pl Hello, world! % cd .. % path/main.pl Can't locate Alan/Hello.pm in @INC Cartwright, De Smet, LeRoy

  11. Why? • My directory isn't in the search path • Can set the environment variable PERL5LIB • Easier: use FindBin; use lib $FindBin::Bin; • "use lib 'path'" lets you add to Perl's search path • FindBin returns the the directory your script is in Cartwright, De Smet, LeRoy

  12. Another module: use Digest::MD5; my $encoded = Digest::MD5::md5_hex($password); • Digest::MD5 provides the function md5_hex Cartwright, De Smet, LeRoy

  13. Packages • Obviously Perl provides some sort of namespaces (C++) or packages (Java). Cartwright, De Smet, LeRoy

  14. package package Alan::MyPackageName; • Everything from that point forward is in the Alan::MyPackagename package. • Stop at end of file, or the next package line. • You start in a package cleverly called main. Cartwright, De Smet, LeRoy

  15. package • A single file can hold multiple packages • (ex. two-in-one.pl) A::hi(); B::hi(); package A; sub hi { print "Hello from A\n"; } package B; sub hi { print "Hello from B\n"; } Cartwright, De Smet, LeRoy

  16. package per file/module • Put a package in a file with of the same name: • (ex hello_world.pl, Alan/Hello.pm) • In the file "Alan/Hello.pm": package Alan::Hello; sub hi { print "Hello, world!"; } 1; • In "hello_world.pl": use Alan::Hello; Alan::Hello::hi(); Cartwright, De Smet, LeRoy

  17. That's all nice, but where at the objects? Cartwright, De Smet, LeRoy

  18. A module implementing an object use FileHandle; my $fh = new FileHandle; $fh->open("</etc/services"); my $line = $fh->getline; $fh->close; Cartwright, De Smet, LeRoy

  19. Perl's Do-It-Yourself Object System • package • module • blessing Cartwright, De Smet, LeRoy

  20. bless my reference • An object is a reference that is "blessed" into a package • Usually the package is in a file/module of the same name Cartwright, De Smet, LeRoy

  21. An example (ex. oo/before and oo/after) Cartwright, De Smet, LeRoy

  22. Common (simplified) usage sub new { my($class, $name) = @_; my($self) = { 'name' => $name, 'count' => 0, }, $class; bless $self, $class; return $self; } Cartwright, De Smet, LeRoy

  23. Common (simplified) usage sub new { my($class, $name) = @_; return bless { 'name' => $name, 'count' => 0, }, $class; } Cartwright, De Smet, LeRoy

  24. Create your own idiom? • "new" isn't special • You could call it "create" or "fred". • new is a good name. Use that. Cartwright, De Smet, LeRoy

  25. Alternate calls • alan->new(); • new alan; • $existing_instance->new(); Cartwright, De Smet, LeRoy

  26. $existing_instance->new(); • Requires a tweak to support... sub new { my($class, $name) = @_; return bless { 'name' => $name, 'count' => 0, }, (ref($class) || $class); } Cartwright, De Smet, LeRoy

  27. Python class hello(object): def __init__(self, name): self._count = 0 self._name = name def hi(self): self._count += 1 print "Hello, %s (I've said hello to %s %d times)" % \ (self._name, self._name, self._count) hello_alan = hello("Alan") hello_alan.hi() Cartwright, De Smet, LeRoy

  28. Ruby class Hello def initialize(name) @name = name @count = 0 end def hi @count = @count + 1 puts "Hello, #{@name} (I've said hello to #{@name} #{@count} times)" end end hello_alan = Hello.new("Alan") hello_alan.hi Cartwright, De Smet, LeRoy

  29. Javascript • Function as class function hello(name) { this.name = name; this.count = 0; this.hi = function() { this.count++; document.write("Hello, " + this.name + " (I've said hello to " + this.name + " " + this.count + " times)\n"); } } hello_alan = new hello("Alan"); hello_alan.hi(); Cartwright, De Smet, LeRoy

  30. Advanced Topics Cartwright, De Smet, LeRoy

  31. Inheritance package HondaCivic; @ISA = ("Car"); • A HondaCivic "is a" Car. • If HondaCivic fails to implement a method, try car • (ex. inherit/*) Cartwright, De Smet, LeRoy

  32. Freeing object memory • General rule: don't worry about it • More specific rule: ensure no variables point to an unwanted object, it will go away eventually • my $obj = new MyObject; • $obj = undef; • Most specific rule: Break circular references • Many scripting languages only reference count and don't fully garbage collect Cartwright, De Smet, LeRoy

More Related