you missed to declare "blurple" as of type RGBColor
use strict;
use warnings;
warn "running";
package RGBColor {
use fields qw(red green blue);
sub new {
my RGBColor $self = shift;
my %params = @_;
unless (ref $self) {
$self = fields::new($self);
}
%{$self} = %params;
return $self;
}
}
my RGBColor $blurple = RGBColor->new(red => 75, blue => 200, green =>
+0);
print "blurple's blue is $blurple->{blue}\n";
$blurple->new(red => 175, blue => 100, green => 100);
print $blurple->{orange}; ### BANGS AT COMPILETIME
print "blurple's blue is $blurple->{blue}\n";
OUTPUT
No such class field "orange" in variable $blurple of type RGBColor at
+d:/tmp/pm/t_fields_pm.pl line 35.
Compilation exited abnormally with code 255 at Sun Feb 14 23:04:10
runs fine after uncommenting the orange line
running at d:/tmp/pm/t_fields_pm.pl line 5.
blurple's blue is 200
blurple's blue is 100