1

I have an array of arrays that I am trying to print out (used to grab SQL database info):

my $variables_array = [[u1, answer1, Uvalue], ["v1", u2, v2, answer2, Vvalue]];

When I print out $variables_array all of the variables except for v2 are printed out with the formatting I have above. I searched google/SO and could not find any special significance for v# in Perl. I noticed that notepad++ changes the color of v# in the array of arrays indicating it is some kind of special variable, but I cannot tell what it is? Can someone please shed some light on this for me, I'm confused.

thomas.cloud
  • 881
  • 13
  • 30
  • 3
    http://stackoverflow.com/questions/8023959/why-use-strict-and-warnings – FMc Feb 08 '13 at 14:34
  • 1
    As FMc cryptically points out, adding `use strict;` and `use warnings;` to the top of your script will help point you in the right direction. Including those in every Perl script you ever write is good programming practice. – Jonah Bishop Feb 08 '13 at 14:42
  • 1
    thanks, yep I had use strict and use warnings at the top of my calling file and also the module that I am calling. I did have no strict qw(subs) at the top of my calling file, I figured that the use strict in the module would take care of any major problems. I was just confused why u2 was okay as a bareword, but not v2, or v3, etc. – thomas.cloud Feb 08 '13 at 15:10

2 Answers2

8

Perl has a lot of different literals

  • Numbers like 123, 123.0, 1.23e2, 0x7b
  • String literals "abc", 'abc', q/abc/, …
  • Barewords
    • Barewords look like function calls without a param list
    • In special places, this is OK even under strict: Foo::Bar->new()
    • without strict 'refs', barewords that don't signify subs are treated as strings.
    • the LHS of the fat comma => is always autoquoted
    • Barewords with leading minus are always strings, unless they are file-test operators. -abc eq "-abc"
  • V-strings (V as in vector, or version). v1.2.3

V-strings consist of a sequence of numbers that are seperated by a period. Each of the numbers is translated to a corresponding character. As they are strings, they can be compared with lt, gt, etc.

They are good for e.g. IP addresses, or version numbers. They are not good for being printed out, as low numbers signify unprintable characters.

$ perl -E'say v49.50.51'
123

The moral of the story? Always use strict; use warnings;, and maybe look into the qw// quoting operator:

my $variables_array = [[qw/u1 answer1 Uvalue/], [qw/v1 u2 v2 answer2 Vvalue/]];
# or verbose:
my $variables_array = [['u1', 'answer1', 'Uvalue'], ['v1', 'u2', 'v2', 'answer2', 'Vvalue']];

(qw does not interpolate, splits the string at any whitespace, and is equal to the list of strings)

amon
  • 57,091
  • 2
  • 89
  • 149
  • In ASCII, the `0x02` stands for `STX`, the “start of text” This is an unprintable character. `"\x02" eq v2` is true. – amon Feb 08 '13 at 14:56
  • brillant, thank you, I like the qw of the inner anonymous array, I will use that. Very through explanation. – thomas.cloud Feb 08 '13 at 15:13
  • 1
    See also [Perl Scalar Value Constructors](http://perldoc.perl.org/perldata.html#Scalar-value-constructors) and the sub-section on 'Version Strings'. – Jonathan Leffler Feb 08 '13 at 15:32
3

With no strict or warnings, I get:

$variables_array: [
                    [
                      'u1',
                      'answer1',
                      'Uvalue'
                    ],
                    [
                      'v1',
                      'u2',
                      v2,
                      'answer2',
                      'Vvalue'
                    ]
                  ]

amon's answer explains that they are "barewords". Barewords are deprecated in almost all contexts (perhaps not command-line scripts, though).

Notice that it quotes 'v1' but not 'v2', that because a version string--a number beginning with a v--are legal literals in Perl.

Axeman
  • 29,660
  • 2
  • 47
  • 102