stevieb has asked for the wisdom of the Perl Monks concerning the following question:
Hello friends and esteemed Monks,
This isn't a problem per-se, just something I've observed and am wondering if there's a way around it.
I am currently building several wildlife cameras that will be attached to my house and some of my cabins, each camera will be attached to a Raspberry Pi, and will stream back results to a central preview server that I will display on a television screen (up to eight inside of a single browser window).
To enhance what I've already got working, I decided that I want pan/tilt capabilities to my cameras, so for the tilt, I'm using a servo (with the servo() capability of RPi::WiringPi), and for the pan, I'm going to use a stepper motor with a ULN2003 integrated circuit, for which I'm now writing a new distribution, RPi::StepperMotor for. Everything is working great thus far, but I have a question specifically related to constant.
Normally, with a list (ie. array in this case), we can get the count of elements and assign it elsewhere:
my $array = [qw(1 2 3)]; my $array_len = @$array; # $array_len == 3
However, in the module I'm writing, I'd prefer to use a constant:
use constant STEPPER_SEQUENCE => [ [qw(1 0 0 1)], [qw(1 0 0 0)], [qw(1 1 0 0)], [qw(0 1 0 0)], [qw(0 1 1 0)], [qw(0 0 1 0)], [qw(0 0 1 1)], [qw(0 0 0 1)], ];
All is still well and great. However, what I can't seem to figure out, is how to extract the count of elements in the constant list. Here is an example. It prints only aref ok. I've tried a few things I've found in the documentation and elsewhere, but I've either overlooked something, or it's not possible to extract the info I want here:
use warnings; use strict; use feature 'say'; use constant STEPPER_SEQUENCE => [ [qw(1 0 0 1)], [qw(1 0 0 0)], [qw(1 1 0 0)], [qw(0 1 0 0)], [qw(0 1 1 0)], [qw(0 0 1 0)], [qw(0 0 1 1)], [qw(0 0 0 1)], ]; # the below line houses the normal "ARRAY(0x...)" use constant STEP_COUNT => STEPPER_SEQUENCE; # ... and because STEPPER_SEQUENCE returns the "ARRAY(0x...)", # this fails: say "const ok" if STEPPER_SEQUENCE == 3; my $aref = [qw(1 2 3)]; say "aref ok" if @$aref == 3;
I have tried:
@{ STEPPER_SEQUENCE } @( STEPPER_SEQUENCE ) (STEPPER_SEQUENCE) @STEPPER_SEQUENCE etc, etc, etdc
Is it that I'm oblivious to something, or is what I'm attempting not possible?
Thanks!
-stevieb
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Get element count of CONSTANT list (aref))
by haukex (Archbishop) on Mar 12, 2018 at 21:37 UTC | |
by stevieb (Canon) on Mar 12, 2018 at 21:52 UTC | |
|
Re: Get element count of CONSTANT list (aref))
by tybalt89 (Monsignor) on Mar 12, 2018 at 22:17 UTC | |
by stevieb (Canon) on Mar 12, 2018 at 22:22 UTC | |
by haukex (Archbishop) on Mar 13, 2018 at 11:59 UTC | |
|
Re: Get element count of CONSTANT list (aref))
by stevieb (Canon) on Mar 12, 2018 at 22:07 UTC | |
|
Re: [SOLVED] Get element count of CONSTANT list (aref))
by stevieb (Canon) on Mar 13, 2018 at 21:36 UTC |