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


In reply to [SOLVED] Get element count of CONSTANT list (aref)) by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.