thomas.cloud
thomas.cloud

Reputation: 963

What does v1, v2, etc stand for in Perl?

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.

Upvotes: 1

Views: 221

Answers (2)

Axeman
Axeman

Reputation: 29854

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.

Upvotes: 3

amon
amon

Reputation: 57600

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)

Upvotes: 8

Related Questions