Blessing Perl OOP

I am new to Perl and I am going through this tutorial http://qntm.org/files/perl/perl.html

In any case, I am working on creating a package that will take in the matrix and will perform various basic operations (for example, Gaussian exception, rref, back sub, defimants, etc.). I have my own constructor that accepts a list of links, but I have some problems with blessing them, so I can access them later. My code is:

main.pl:

use strict; use warnings; use Matrix; my @list = ([1,1,1],[2,2,2]); my $matrix = Matrix->new(@list); $matrix->test(); 

Matrix.pm:

 package Matrix; sub new(){ my $class = shift; my $self = []; my @params = @_; $self = \@params; print scalar @{$self->[1]}; #just testing some output...(outputs 3 as expected) bless $self,$class; return $self; } sub test(){ print @{$self->[1]}; #does not output anything } 1; 

I guess the problem is that the links referenced by $ self are not blessed, but I'm not sure how to do this. Any help would be appreciated.

thanks

+4
source share
1 answer

You forgot to actually define $self in test ; It is not available to you automatically. This is why you should always put use warnings; use strict; use warnings; use strict; to each Perl source file: so that the compiler tells you about errors like these. (In addition, it makes no sense to write sub new() instead of sub new , as well as for test ; the function prototype is not only erroneous, but will be ignored if new used as a method, i.e. how new supposed to be used.)

+7
source

Source: https://habr.com/ru/post/1496924/


All Articles