drudgesentinel has asked for the wisdom of the Perl Monks concerning the following question:
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 +;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Using perlclass 'methods'
by eyepopslikeamosquito (Archbishop) on Jul 18, 2024 at 08:43 UTC | |
Re: Using perlclass 'methods'
by haj (Vicar) on Jul 18, 2024 at 07:32 UTC | |
by kikuchiyo (Hermit) on Jul 18, 2024 at 08:07 UTC | |
by haj (Vicar) on Jul 18, 2024 at 10:20 UTC | |
by bliako (Abbot) on Jul 18, 2024 at 09:07 UTC | |
by hippo (Archbishop) on Jul 18, 2024 at 09:26 UTC | |
by bliako (Abbot) on Jul 18, 2024 at 12:50 UTC | |
by Arunbear (Prior) on Jul 18, 2024 at 10:31 UTC | |
by parv (Parson) on Jul 18, 2024 at 11:00 UTC | |
by haj (Vicar) on Jul 18, 2024 at 10:47 UTC | |
by bliako (Abbot) on Jul 18, 2024 at 12:54 UTC | |
Re: Using perlclass 'methods'
by Anonymous Monk on Jul 18, 2024 at 06:31 UTC |