Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

How to get string from array from module?

by Gilu (Initiate)
on Feb 08, 2018 at 14:23 UTC ( #1208713=perlquestion: print w/replies, xml ) Need Help??
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:
# 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 (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 (Canon) 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 to:
    our @COLORMAGENTA = (0.82745,0.21176,0.50980); @SELECTED_COLOR = @COLORMAGENTA;

    and "fetched" this data in like this:
    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:

        Well ... if that was your second day with Perl; everything seems so ... you know! :-) You're absolutely correct - of course! Thanks for the hint!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1208713]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2018-06-23 08:28 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.