Hello! I'm quite new to Perl 5, and often struggle with the concept of references (as other languages I've dabbled in do not use these)
I'm going through a Ruby OOP book that I like and am trying to use the experimental 'class' features to familiarize myself with the language more (I'm learning it to work on a project with a friend)
In the following snippet, the code for the ratio method was working fine. However, with the addition of the gear_inches method, I get a cryptic (to me) error: Can't use string ("26") as a symbol ref while "strict refs" in use at gear1.pm line 15.
This seems to imply that when I create the new class instance at the end, the rim parameter value of 26 is interpreted as a symbolic reference when the 'gear_inches' function is called. Upon reading up on symbolic references: https://perldoc.perl.org/perlref#Symbolic-references
It seems that Perl is saying that '26' (the value of the scalar) is taken to be the name of a variable. I have no idea how this could possibly be given the simplicity of the code. Every problem that produced an error message I couldn't make sense of previously involved de referencing, a concept with which I've not previously dealt. I guess this is probably also that somehow (lol?) Thank you for your time!use v5.40.0; use experimental 'class'; class Gear { field $chainring :param :reader; field $cog :param :reader; field $rim :param :reader; field $tire :param :reader; method ratio { return $chainring/$cog; } method gear_inches { return ratio * $rim + ($tire * 2); } } say Gear->new(chainring=>52, cog=>11, rim=>26, tire=>1.5)->gear_inches +;
In reply to Using perlclass 'methods' by drudgesentinel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |