http://www.perlmonks.org?node_id=1208713

Gilu has asked for the wisdom of the Perl Monks concerning the following question:

So, I got this module:
package Slic3r::GUI::ColorScheme; use strict; use warnings; use Exporter; our @ISA = 'Exporter'; our @EXPORT = qw(@SELECTED_COLOR @b); our (@SELECTED_COLOR); our @COLOR_MAGENTA = [0.82745,0.21176,0.50980]; # This is an array, ye +s?! our $COLORMAGENTA = [0.82745,0.21176,0.50980]; # only for testing sub getMODColorScheme { print "-----> getColorScheme from MODULE\n"; print "from dollar: @{ $COLORMAGENTA } \n"; # 0.82745 0.21176 0.5 +098 print "from at : @COLOR_MAGENTA \n"; # ARRAY(0x59d88f8) -> + this needs to be 0.82745 0.21176 0.5098 if ($Slic3r::GUI::Settings->{_}{colorschema_solarized}) { print "Using S O L A R I Z E from Slic3r::GUI::ColorScheme\n" +; @SELECTED_COLOR = @COLOR_MAGENTA; } else { @SELECTED_COLOR = [0,1,0]; } } 1;
And in another file, I need to get STRING of @SELECTED_COLOR. So I've done this so far:
# 3dscene.pm use Slic3r::GUI::ColorScheme; sub new { my ($class, $parent) = @_; Slic3r::GUI::ColorScheme::getMODColorScheme(); glColor4f( @SELECTED_COLOR , $volume->color->[3]); # this should b +e like: glColor4f(0.82745,0.21176,0.50980, 1); # glColor4f(@{ @SELECTED_COLOR }, $volume->color->[3]); # this doe +s not work at all! # ERROR from line above: Can't use string ("1") as an ARRAY ref wh +ile "strict refs" in use }
Somehow I get ARRAY(0x59d88f8) back from @SELECTED_COLOR. If I use $COLORMAGENTA in 3dscene.pm (as this: glColor4f(@{ $COLORMAGENTA }, $volume->color->[3]);), then I get the expected output (0.82745 0.21176 0.5098). How can I get 0.82745 0.21176 0.5098 from @SELECTED_COLOR? Any help is appreciated. Thanks. This is my first PERL question/experience ever; please be gentle.

Replies are listed 'Best First'.
Re: How to get string from array from module?
by hippo (Bishop) on Feb 08, 2018 at 14:45 UTC
    our @COLOR_MAGENTA = [0.82745,0.21176,0.50980]; # This is an array, yes?!

    No. The term on the right of the equals sign is an array reference, not an array. You assign that to the term on left which is an array so what you end up with is the array @COLOR_MAGENTA the first and only item of which is an array reference which itself has 3 items.

    Have you read either or both of perlreftut and perlref? References are a very powerful tool but you do need to know how to use them otherwise you will end up with a lot of similar issues.

Re: How to get string from array from module?
by Laurent_R (Canon) on Feb 08, 2018 at 22:15 UTC
    hippo is right. Just wanting to add that, if you want an array, you need to use round parentheses, not square brackets, like this:
    our @COLOR_MAGENTA = (0.82745,0.21176,0.50980); # Now this is an array
Re: How to get string from array from module?
by Gilu (Initiate) on Feb 09, 2018 at 09:14 UTC
    Ok! Thanks for your help, guys! I think I got it. I changed the line in ColorScheme.pm to:
    our @COLORMAGENTA = (0.82745,0.21176,0.50980); @SELECTED_COLOR = @COLORMAGENTA;

    and "fetched" this data in 3DScene.pm like this:
    my $SELECTED_COLOR; $SELECTED_COLOR =\@SELECTED_COLOR;
    And then I was able to retrieve my colors like that:
    glColor4f( $SELECTED_COLOR->[0],$SELECTED_COLOR->[1],$SELECTED_COLOR->[2] , $volume->color->[3]);
    It works but I'm not sure if this is the correct way?!

      I don't see the point of turning @SELECTED_COLOR into a reference and then back again. Why not simply do this:

      ### NOT NEEDED my $SELECTED_COLOR; ### NOT NEEDED $SELECTED_COLOR =\@SELECTED_COLOR; glColor4f (@SELECTED_COLOR, $volume->color->[3]);
        Well ... if that was your second day with Perl; everything seems so ... you know! :-) You're absolutely correct - of course! Thanks for the hint!