• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Oo Perl
 

Oo Perl

on

  • 873 views

All you ever wanted to know but were afraid to ask

All you ever wanted to know but were afraid to ask

Statistics

Views

Total Views
873
Views on SlideShare
870
Embed Views
3

Actions

Likes
1
Downloads
13
Comments
0

1 Embed 3

http://www.slideshare.net 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • My name is Leonard Miller, I am from the University of Minnesota. The point of this talk will be to help you understand what is happening when you See code like this:
  • First we will setup a scalar, an array and a hash, and then we will use Data::Dumper to print them out.
  • Here we will make three references to the variables above. This is done with the backslash preceding the sigil.
  • Here we will make three references to the variables above. This is done with the backslash preceding the sigil.
  • In perl five the sigil you use tells the compiler what type of variable we are dealing with or accessing. The curly braces give us a different way to access the variable.
  • This is a side comment about a neat side effect of the curly brace syntax.
  • This is a neat way that you can call functions in the middle of strings. Although this Is really neat, you shouldn’t use it because it sucks to maintain.
  • Here we can see the basic de-referencing.
  • Here is package foo, this is an over simplified version of what is under the hood for Moose. We have a local variable, an init function and an accessor function that sets and retrieves the variable ‘bar’.
  • Here is package foo, this is an over simplified version of what is under the hood for Moose. We have a local variable, an init function and an accessor function that sets and retrieves the variable ‘bar’.
  • Bless is what ties an object to a package. The first argument is usually a hashref, but can be any type of reference, and the second is the name of the package. If you have the module loaded, you can create any type of object and perl will not Complain. BEHOLD! THAR BE DRAGONS! You can really muck things up by creating your own objects. Bless only belongs inside a package, never outside.
  • Bless is what ties an object to a package. The first argument is usually a hashref, but can be any type of reference, and the second is the name of the package. If you have the module loaded, you can create any type of object and perl will not Complain. BEHOLD! THAR BE DRAGONS! You can really muck things up by creating your own objects. Bless only belongs inside a package, never outside.

Oo Perl Oo Perl Presentation Transcript

  • Object Oriented Perl All you ever wanted to know but were afraid to ask Leonard Miller YAPC::NA 2009 June 23
    • print $self->{item};
    • print $self->item(‘item’);
    • print $self->item;
    • print $self->item->another_item
    • use strict;
    • use Data::Dumper;
    • my $x = 'ducks';
    • my @y = ('donald', 'daffy');
    • my %z = (donald => 'WB',
    • g => '');
    • $z{i} = 'j';
    • print "dumping x: ” . Dumper($x);
    • print "dumping y: ” . Dumper(@y);
    • print "dumping z: ” . Dumper(%z);
    dumping x: $VAR1 = 'ducks'; dumping y: $VAR1 = 'donald'; $VAR2 = 'daffy'; dumping z: $VAR1 = 'donald'; $VAR2 = 'WB'; $VAR3 = 'g'; $VAR4 = ''; $VAR5 = 'i'; $VAR6 = 'j';
    • my $ref_x = $x;
    • my $ref_y = @y;
    • my $ref_z = z;
    • print Dumper($ref_x) . "-- ";
    • print Dumper($ref_y) . "-- ";
    • print Dumper($ref_z) . "-- ";
    • print $ref_x . " ";
    • print $ref_y . " ";
    • print $ref_z . " -- ";
    • my $ref_x = $x;
    • my $ref_y = @y;
    • my $ref_z = z;
    • print Dumper($ref_x) . "-- ";
    • print Dumper($ref_y) . "-- ";
    • print Dumper($ref_z) . "-- ";
        • $VAR1 = 'ducks';
        • --
        • $VAR1 = [
        • 'donald',
        • 'daffy'
        • ];
        • --
        • $VAR1 = {
        • 'donald' => 'WB',
        • 'g' => '',
        • 'i' => 'j'
        • };
        • --
    • print $ref_x . " ";
    • print $ref_y . " ";
    • print $ref_z . " -- ";
        • SCALAR(0x180b578)
        • ARRAY(0x180b548)
        • HASH(0x180b56c)
        • --
    • Symbol Table hints:
      • $x is the same as ${x}
      • @y is the same as @{y}
      • my $item = ‘cow’ ; my $number=3;
      • print “there are $number $items ”;
            • Global symbol "$items" requires explicit package name
      • print “there are $number ${item}s ”;
            • “ there are three cows”
    • Symbol Table hints:
      • @y is the same as @{y}
      • @array = @{[ 1,2,3]};
      • #does exactly what you would expect
      • @array = @{[ fcn(1), fcn(2), fcn(3) ]};
      • #does also what you would expect
    • Symbol Table hints:
      • @y is the same as @{y}
      • @array = @{[ 1,2,3]};
      • #does exactly what you would expect
      • #same as:
      • #@array = (1,2,3)
      • @array = @{[ fcn(1), fcn(2), fcn(3) ]};
      • #does also what you would expect
      • print “the function returns @{[ fcn(1) ]} ”;
    • print "dereferencing ";
    • print "scalar: ”;
    • print $$ref_x;
    • print . " ";
    • print ”array: ”;
    • print @$ref_y;
    • print . " ";
            • dereferencing
            • scalar: ducks
            • array: donald daffy
    • print "array element: ”;
    • print $$ref_y[0];
    • print . " ”;
    • print "hash: ”;
    • print %$ref_z;
    • print " ";
    • print "hash element: ”;
    • print $$ref_z{donald}
    • print " -- ";
            • array element: donald
            • hash: donaldWBgij
            • hash element: WB
            • --
    • print Dumper($ref_x) . "-- ";
    • print Dumper($ref_y) . "-- ";
    • print Dumper($ref_z) . "-- ";
        • $VAR1 = 'ducks';
        • --
        • $VAR1 = [
        • 'donald',
        • 'daffy'
        • ];
        • --
        • $VAR1 = {
        • 'donald' => 'WB',
        • 'g' => '',
        • 'i' => 'j'
        • };
        • --
    • This is syntax that you are more likely to see in oo programs:
    • print "dereferencing with an arrow ";
    • print $ref_y->[0];
    • print " ";
    • print $ref_z->{donald};
    • print " ";
            • dereferencing with an arrow
            • donald
            • WB
    • package foo;
    • my $bar;
    • sub bar{
    • my $self = shift; #first argument is the
    • #'blessed' variable
    • if(@_) { $bar = $_[0] }
    • return $bar;
    • }
    • sub new{
    • return bless({},$_[0])
    • };
    • 1;
    • package foo;
    • my $bar;
    • sub bar{
    • my $self = shift; #first argument is the
    • #'blessed' variable
    • if(@_) { $bar = $_[0] }
    • return $bar;
    • }
    • sub new{
    • return bless({},$_[0])
    • };
    • 1;
    • Usage of bless:
    • bless(
    • {}, # a variable reference (usually a hashref)
    • $_[0] # a package name.
    • )
    • Usage of bless:
    • bless(
    • {}, # a variable reference (usually a hashref)
    • $_[0] # a package name.
    • )
    • use foo;
    • $ref_z = foo::new();
    • #same as:
    • #$ref_z = {};
    • #bless($ref_z, 'foo');
    • #setting the variable bar:
    • $ref_z->bar("yowza");
    • #getting the variable bar:
    • print $ref_z->bar;
    • print " ";
  •  
    • use fubar;
    • my $oo_var = fubar->new();
    • $oo_var->bar('t');
    • print $oo_var->bar;
            • t
    • $oo_var->bun; #calls sub bun
    • Thank you
  •