title: Object-Oriented Perl constructor syntax and named parameters
body: <p>I'm a little confused about what is going on in Perl constructors. I found these two examples <a href="http://perldoc.perl.org/perlbot.html#INSTANCE-VARIABLES" rel="noreferrer">perldoc perlbot</a>.</p>
  
  <pre><code>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-&gt;{'High'} = $params{'High'};
    $self-&gt;{'Low'} = $params{'Low'};
  
    #Even though I read the page on [bless][2], I still don't get what it does.
    bless $self, $type;
  }
  </code></pre>
  
  <p>And another example is:</p>
  
  <pre><code>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-&gt;[0] = $params{'Left'};
    $self-&gt;[1] = $params{'Right'};
  
    #and again with the bless.
    bless $self, $type;
  }
  </code></pre>
  
  <p>And here is the script that uses these objects:</p>
  
  <pre><code>package main;
  
  $a = Foo-&gt;new( 'High' =&gt; 42, 'Low' =&gt; 11 );
  print "High=$a-&gt;{'High'}\n";
  print "Low=$a-&gt;{'Low'}\n";
  
  $b = Bar-&gt;new( 'Left' =&gt; 78, 'Right' =&gt; 40 );
  print "Left=$b-&gt;[0]\n";
  print "Right=$b-&gt;[1]\n";
  </code></pre>
  
  <p>I've injected the questions/confusion that I've been having into the code as comments.</p>