Advertisement
TermSpar

Perl Object Oriented

Jun 12th, 2016
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.02 KB | None | 0 0
  1. #Tutorial.pl:
  2. use lib 'lib';
  3. use Users;
  4.  
  5. my $new_user = new Users("Ben", "1234", 1);
  6.  
  7. print("Username: ");
  8. print($new_user->getName());
  9. print("\nPassword: ");
  10. print($new_user->getPass());
  11. print("\nID: ");
  12. print($new_user->getID());
  13.  
  14. #Users.pm:
  15. package Users;
  16.  
  17. sub new{
  18.   $class = shift;
  19.   $self = {
  20.     #Member Variables:
  21.     username => shift,
  22.     password => shift,
  23.     id => shift
  24.   };
  25.   bless $self, $class;
  26.   return $self;
  27. }
  28.  
  29. sub setName{
  30.   my ($self, $new_name) = @_;
  31.   $self->{username} = $new_name if defined($new_name);
  32.   return $self->{username};
  33. }
  34.  
  35. sub getName{
  36.   my ($self) = @_;
  37.   return $self->{username};
  38. }
  39.  
  40. sub setPass{
  41.   my ($self, $new_pass) = @_;
  42.   $self->{password} = $new_pass if defined($new_pass);
  43.   return $self->{password};
  44. }
  45.  
  46. sub getPass{
  47.   my ($self) = @_;
  48.   return $self->{password};
  49. }
  50.  
  51. sub setID{
  52.   my ($self, $new_ID) = @_;
  53.   $self->{id} = $new_ID if defined($new_ID);
  54.   return $self->{id};
  55. }
  56.  
  57. sub getID{
  58.   my ($self) = @_;
  59.   return $self->{id};
  60. }
  61.  
  62. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement