LoginSignup
2
2

More than 5 years have passed since last update.

スゲい簡単なPerl入門 -3rdDay

Posted at

Play with Object

date.pm
package main;
my $date = main->new;
# Using Object(= Reference related with Package name) call subroutine in package
$date->set_date(9);
print "\n";
sub new {
        my $pkg = shift;
        bless {
                year    => undef,
                month   => undef,
                day     => undef,
                second  => undef
        },$pkg;
}
sub set_date {
        # this $self is for receiving 1st argument(in this case, $date)
        my $self = shift;
        my $time_dereference = shift;
        ($self->{year},$self->{month},$self->{day},$self->{hour},$self->{minute},$self->{second}) = ( gmtime time + $time_dereference * 3600 )[5,4,3,2,1];
        $self->{year} += 1900;
        $self->{month}++;
}
sub get_year {
        my $self = shift;
        return $self->{year};
}
sub get_month {
        my $self = shift;
        return $self->{month};
}
sub get_day {
        my $self = shift;
        return $self->{day};
}
sub get_hour {
        return $date->{hour};
}
sub get_minute {
        return $date->{minute};
}
sub get_second {
        return $date->{second};
}
$date->set_date(9);

Using Object(= Reference related with Package name) call subroutine in package
1st argument is self($date), 2nd argument is 9. (main::set_date($date,9);)
e.g) $obj = Hoge->new;
$obj = Hoge::new('Hoge');

In this source, $self is same to $date.
So you can delete "my $self = shift;" and use "$date->{year};" inseted of $self.

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2