You're viewing old version number 1. - Current version

1 min

Creating Perl Modules and OO Programming

http://www.perlmonks.org/?node_id=586646
Defining classes and inheritance using packages within a single .pl file, without creating modules

http://stackoverflow.com/questions/2621225/how-can-i-call-a-perl-package-i-define-in-the-same-file

http://oreilly.com/catalog/prkunix/excerpt/PWPMch01.html#MARKER-9-23

http://stackoverflow.com/questions/1748896/in-perl-how-do-i-put-multiple-packages-in-a-single-pm-file

http://stackoverflow.com/questions/1143874/what-is-the-difference-between-package-module-and-class-in-object-oriented-perl

http://broadcast.oreilly.com/2008/11/beginners-introduction-to-obje.html

http://perldoc.perl.org/perlobj.html

http://www.perl.com/doc/FMTEYEWTK/perltoot.html

http://turtle.ee.ncku.edu.tw/docs/perl/manual/pod/perltoot.html

http://www.perl.com/pub/2002/05/14/mod_perl.html

Excerpts below from an old article, located at:
http://mathforum.org/ken/perl_modules.html

Related:

Object Oriented Perl syntax


package NewModule;

use strict;
use vars qw($VERSION);
$VERSION = '0.01';

sub new {
  my $package = shift;
  return bless({}, $package);
}

sub verbose {
  my $self = shift;
  if (@_) {
    $self->{'verbose'} = shift;
  }
  return $self->{'verbose'};
}

sub hoot {
  my $self = shift;
  return "Don't pollute!" if $self->{'verbose'};
  return;
}

1;
__END__


Test code


# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..1\n"; }
END {print "not ok 1\n" unless $loaded;}
use NewModule;
$loaded = 1;
print "ok 1\n";

######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):

# Test 2:
my $obj = new NewModule;
$obj->verbose(1);
my $result = $obj->hoot;
print ($result eq "Don't pollute!" ? "ok 2\n" : "not ok 2\n");

# Test 3:
$obj->verbose(0);
my $result = $obj->hoot;
print ($result eq "" ? "ok 3\n" : "not ok 3\n");

Inside-out objects

inside out

[[Perl example of inside-out object]]

[[Perl example of inside-out object number 2]]

http://perltraining.com.au/tips/2006-03-31.html

http://perldoc.perl.org/perlobj.html#Inside-Out-objects

http://perldoc.perl.org/Hash/Util/FieldHash.html

http://search.cpan.org/dist/Object-InsideOut/lib/Object/InsideOut.pod

getters and setters

!!!! http://www.perlmonks.org/?node_id=317885

http://stackoverflow.com/questions/1390082/in-perl-are-there-disadvantages-to-generating-getters-and-setters-rather-than-ha

http://etutorials.org/Programming/Perl+objects,+references+and+modules/Chapter+11.+Some+Advanced+Object+Topics/11.5+Creating+Getters+and+Setters+More+Easily/

http://search.cpan.org/~dconway/Class-Std-0.011/lib/Class/Std.pm

http://perldoc.perl.org/perlootut.html

#perl
#programming
#objectoriented

From JR's : articles
283 words - 3158 chars - 1 min read
created on
updated on - #
source - versions

Related articles
Creating Perl Modules and OO Programming - Oct 17, 2013
Perl code for string processing - Jul 06, 2013
Perl and Curl - Mar 23, 2015
Perl conditional loading modules - Oct 02, 2014
Quotes in Perl - Oct 12, 2013
more >>



A     A     A     A     A

© 2013-2017 JotHut - Online notebook

current date: May 17, 2024 - 3:04 a.m. EDT