0

I have a perl pre_installer.conf program for storing config.

use strict;

$VAR::phpmodules = ("php5-curl", "php5-mcrypt", "php-abc");
1;

I have included this file and accessed $VAR::phpmodules

require 'pre_installer.conf';
print $VAR::phpmodules;

But it prints only 'php-abc'. That is the last item only? Why didn't it print the whole array?

2
  • Out of interest - if you perl -c that module, does it work? Commented Feb 20, 2015 at 14:50
  • Actually, never mind. You need warnings turned on too, for perl to warn you about the bad syntax on that line. Commented Feb 20, 2015 at 14:53

1 Answer 1

4

Because

$VAR::phpmodules = ("php5-curl", "php5-mcrypt", "php-abc");

isn't doing what you think it is. It's assigning the last element in the list.

my $thing = ( "one", "two", "three" );
print $thing;

#prints "three"; 

However, this is a really good example why use strict; and use warnings; is a REALLY good idea - because warnings tells you:

Useless use of a constant ("one") in void context at line ...
Useless use of a constant ("two") in void context at line ...

Try:

$VAR::phpmodules = ["php5-curl", "php5-mcrypt", "php-abc"];

Which will turn it into an array-ref. (You'll have to dereference it to print it though, with print @$VAR::phpmodules )

my $thing = [ "one", "two", "three" ];
print @$thing;
#prints onetwothree because no delimiter between array elements. 

Or

@VAR::phpmodules = ("php5-curl", "php5-mcrypt", "php-abc");

e.g.

my @thing = ( "one", "two", "three" );
print @thing;
#prints "onetwothree"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.