Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Foo;
- #In Perl, the constructor is just a subroutine called new.
- sub new {
- #I don't get what this line does at all, but I always see it. Do I need it?
- my $type = shift;
- #I'm turning the array of inputs into a hash, called parameters.
- my %params = @_;
- #I'm making a new hash called $self to store my instance variables?
- my $self = {};
- #I'm adding two values to the instance variables called "High" and "Low".
- #But I'm not sure how $params{'High'} has any meaning, since it was an
- #array, and I turned it into a hash.
- $self->{'High'} = $params{'High'};
- $self->{'Low'} = $params{'Low'};
- #Even though I read the page on [bless][2], I still don't get what it does.
- bless $self, $type;
- }
- package Bar;
- sub new {
- my $type = shift;
- #I still don't see how I can just turn an array into a hash and expect things
- #to work out for me.
- my %params = @_;
- my $self = [];
- #Exactly where did params{'Left'} and params{'Right'} come from?
- $self->[0] = $params{'Left'};
- $self->[1] = $params{'Right'};
- #and again with the bless.
- bless $self, $type;
- }
- package main;
- $a = Foo->new( 'High' => 42, 'Low' => 11 );
- print "High=$a->{'High'}n";
- print "Low=$a->{'Low'}n";
- $b = Bar->new( 'Left' => 78, 'Right' => 40 );
- print "Left=$b->[0]n";
- print "Right=$b->[1]n";
- sub foo {
- my %stuff = @_;
- ...
- }
- foo( beer => 'good', vodka => 'great' );
- package Foo;
- use strict;
- use warnings;
- use Carp qw(croak);
- sub new {
- my $class = shift;
- croak "Illegal parameter list has odd number of values"
- if @_ % 2;
- my %params = @_;
- my $self = {};
- bless $self, $class;
- # This could be abstracted out into a method call if you
- # expect to need to override this check.
- for my $required (qw{ name rank serial_number });
- croak "Required parameter '$required' not passed to '$class' constructor"
- unless exists $params{$required};
- }
- # initialize all attributes by passing arguments to accessor methods.
- for my $attrib ( keys %params ) {
- croak "Invalid parameter '$attrib' passed to '$class' constructor"
- unless $self->can( $attrib );
- $self->$attrib( $params{$attrib} );
- }
- return $self;
- }
- my @array = (key1, val1, key2, val2, key3, val3, ...);
- my %hash = @array;
- # %hash = ( key1 => val1, key2 => val2, key3 => val3, ...);
- my @x = ('High' => 42, 'Low' => 11);
- my %h = @x;
- use Data::Dumper;
- print Dumper %h;
- bless
- #!/usr/bin/perl
- package My::Mod;
- use strict;
- use warnings;
- use Data::Dumper;
- $Data::Dumper::Indent = 0;
- sub new { bless [] => shift }
- sub frobnicate { Dumper(@_) }
- package main;
- use strict;
- use warnings;
- my $x = My::Mod->new;
- # invoke instance method
- print $x->frobnicate('High' => 42, 'Low' => 11);
- # invoke class method
- print My::Mod->frobnicate('High' => 42, 'Low' => 11);
- # call sub frobnicate in package My::Mod
- print My::Mod::frobnicate('High' => 42, 'Low' => 11);
- new
- Employee->new(name => 'Fred Flintstone', occupation => 'quarry worker');
Add Comment
Please, Sign In to add comment