Skip to content

Instantly share code, notes, and snippets.

@SunKing2
Created June 10, 2023 04:45
Show Gist options
  • Save SunKing2/cd32371c56b223ad86d6c36176649764 to your computer and use it in GitHub Desktop.
Save SunKing2/cd32371c56b223ad86d6c36176649764 to your computer and use it in GitHub Desktop.
object oriented with Moose and perl (it uses an array too!)
# Sources:
# Mastering Object-Oriented Perl Programming (OOP): Unleashing the Power of OOP in Perl (YouTube, last 2 minutes!!!!)
# Docs for Moose::Meta::Attribute::Native::Trait::Array
# probably at: https://metacpan.org/pod/Moose::Meta::Attribute::Native::Trait::Array
# GlobalStuff.pm in current directory
package GlobalStuff;
use Moose;
has 'name' => (is=>'ro' , isa=>'Str' , default=>'');
has 'surname' => (is=>'ro' , isa=>'Str' , default=>'');
has 'roll' => (is=>'rw' , isa=>'Int' , default=>0);
has 'options' => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Str]',
default => sub { [] },
handles => {
all_options => 'elements',
add_option => 'push',
map_options => 'map',
filter_options => 'grep',
find_option => 'first',
get_option => 'get',
join_options => 'join',
count_options => 'count',
has_options => 'count',
has_no_options => 'is_empty',
sorted_options => 'sort',
# these are mine:
boogy_options => 'join',
gsHasErrors => 'count',
gsAddError => 'push',
gsPopFirstError => 'shift',
},
);
sub print_info {
my $self = shift;
my $prefix = shift // "This file is at ";
print $prefix, ", ", $self->name, " ", $self->surname, "\n";
}
sub gsGetFirstError {
my $self = shift;
return $self->get_option(0);
}
1;
##############################################
# moose-example.pl in current directory
# #!/usr/bin/perl
use strict;
use warnings;
use GlobalStuff;
my $xgs = GlobalStuff->new(name=>'Naga', surname=>'Yamini' , roll=>112);
print "\n..Name = ". $xgs->name . "\n";
print "\n..Surname = ".$xgs->surname . "\n";
print "\n..Roll = ".$xgs->roll . "\n";
$xgs->print_info("The file is located at ");
my $gs = GlobalStuff->new();
print "\n";
print "has errors?:" , $gs->gsHasErrors()?"True":"False" . "\n";
$gs->gsAddError(3);
my $s1 = $gs->get_option(0);
print "added 3 and the element at 0 is $s1\n";
print "has errors?:" , $gs->gsHasErrors()?"True\n":"False\n";
print "gsGetFirstError:", $gs->gsGetFirstError() . "\n";
$gs->gsAddError(2);
$s1 = $gs->get_option(1);
print "added 2 and the element at 1 is $s1\n";
print "here is the count:";
print $gs->count_options; # prints 4
print "popping\n";
my $m = $gs->gsPopFirstError();
print "popped was: " , $m, "\n";
print "all elements:" , $gs->boogy_options(':'), "\n";
print "\n\n";
####################################################
perl -I. moose-example.pl
..Name = Naga
..Surname = Yamini
..Roll = 112
The file is located at , Naga Yamini
has errors?:False
added 3 and the element at 0 is 3
has errors?:True
gsGetFirstError:3
added 2 and the element at 1 is 2
here is the count:2popping
popped was: 3
all elements:2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment