Guest User

Untitled

a guest
Apr 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. package Foo;
  2.  
  3. #In Perl, the constructor is just a subroutine called new.
  4. sub new {
  5. #I don't get what this line does at all, but I always see it. Do I need it?
  6. my $type = shift;
  7.  
  8. #I'm turning the array of inputs into a hash, called parameters.
  9. my %params = @_;
  10.  
  11. #I'm making a new hash called $self to store my instance variables?
  12. my $self = {};
  13.  
  14. #I'm adding two values to the instance variables called "High" and "Low".
  15. #But I'm not sure how $params{'High'} has any meaning, since it was an
  16. #array, and I turned it into a hash.
  17. $self->{'High'} = $params{'High'};
  18. $self->{'Low'} = $params{'Low'};
  19.  
  20. #Even though I read the page on [bless][2], I still don't get what it does.
  21. bless $self, $type;
  22. }
  23.  
  24. package Bar;
  25.  
  26. sub new {
  27. my $type = shift;
  28.  
  29. #I still don't see how I can just turn an array into a hash and expect things
  30. #to work out for me.
  31. my %params = @_;
  32. my $self = [];
  33.  
  34. #Exactly where did params{'Left'} and params{'Right'} come from?
  35. $self->[0] = $params{'Left'};
  36. $self->[1] = $params{'Right'};
  37.  
  38. #and again with the bless.
  39. bless $self, $type;
  40. }
  41.  
  42. package main;
  43.  
  44. $a = Foo->new( 'High' => 42, 'Low' => 11 );
  45. print "High=$a->{'High'}n";
  46. print "Low=$a->{'Low'}n";
  47.  
  48. $b = Bar->new( 'Left' => 78, 'Right' => 40 );
  49. print "Left=$b->[0]n";
  50. print "Right=$b->[1]n";
  51.  
  52. sub foo {
  53. my %stuff = @_;
  54. ...
  55. }
  56.  
  57. foo( beer => 'good', vodka => 'great' );
  58.  
  59. package Foo;
  60.  
  61. use strict;
  62. use warnings;
  63. use Carp qw(croak);
  64.  
  65. sub new {
  66. my $class = shift;
  67.  
  68. croak "Illegal parameter list has odd number of values"
  69. if @_ % 2;
  70.  
  71. my %params = @_;
  72.  
  73. my $self = {};
  74. bless $self, $class;
  75.  
  76. # This could be abstracted out into a method call if you
  77. # expect to need to override this check.
  78. for my $required (qw{ name rank serial_number });
  79. croak "Required parameter '$required' not passed to '$class' constructor"
  80. unless exists $params{$required};
  81. }
  82.  
  83. # initialize all attributes by passing arguments to accessor methods.
  84. for my $attrib ( keys %params ) {
  85.  
  86. croak "Invalid parameter '$attrib' passed to '$class' constructor"
  87. unless $self->can( $attrib );
  88.  
  89. $self->$attrib( $params{$attrib} );
  90. }
  91.  
  92. return $self;
  93. }
  94.  
  95. my @array = (key1, val1, key2, val2, key3, val3, ...);
  96.  
  97. my %hash = @array;
  98. # %hash = ( key1 => val1, key2 => val2, key3 => val3, ...);
  99.  
  100. my @x = ('High' => 42, 'Low' => 11);
  101. my %h = @x;
  102.  
  103. use Data::Dumper;
  104. print Dumper %h;
  105.  
  106. bless
  107.  
  108. #!/usr/bin/perl
  109.  
  110. package My::Mod;
  111.  
  112. use strict;
  113. use warnings;
  114.  
  115. use Data::Dumper;
  116. $Data::Dumper::Indent = 0;
  117.  
  118. sub new { bless [] => shift }
  119.  
  120. sub frobnicate { Dumper(@_) }
  121.  
  122. package main;
  123.  
  124. use strict;
  125. use warnings;
  126.  
  127. my $x = My::Mod->new;
  128.  
  129. # invoke instance method
  130. print $x->frobnicate('High' => 42, 'Low' => 11);
  131.  
  132. # invoke class method
  133. print My::Mod->frobnicate('High' => 42, 'Low' => 11);
  134.  
  135. # call sub frobnicate in package My::Mod
  136. print My::Mod::frobnicate('High' => 42, 'Low' => 11);
  137.  
  138. new
  139.  
  140. Employee->new(name => 'Fred Flintstone', occupation => 'quarry worker');
Add Comment
Please, Sign In to add comment