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

Fellow monks,

In my current project, I'm using hashes that contain references to all sorts of things and I use arrays to contain the values that these hashes are keyed on:

@keys = ("key1", "key2");
%list_of_refs = (
   key1 = \%ref_to_hash1,
   key2 = \%ref_to_hash2
);

etc. I found that when using extremely short keys (specifically "v2" and "v3"), the program would not find anything from the hashes that were so keyed. However, when I changed the key names to "ver2" and "ver3", the program immediately behaved completely as expected. Can anymonk tell me why I wouldn't be able to use an arbitrary variable name?

DISCLAIMER: I am not using an IDE, but as far as I know (and I did look for a bit) v2 and v3 are not reserved words in PERL. If this is the case, feel free to chastise...errr...enlighten me :)

Replies are listed 'Best First'.
Re: About variable names
by ysth (Canon) on Nov 20, 2003 at 04:24 UTC
    Make sure you say $hash{"v2"} and %hash = ( "v2" => 1 ) to avoid interpretation as a v-string. See perldoc perldata.

      Thanks. I was using the plaintext version v2 instead of "v2" to create the keys. I'll keep that in mind from now on.

Re: About variable names
by asarih (Hermit) on Nov 20, 2003 at 04:28 UTC
    v2, v3, etc., are v-strings (literals that start with v followed by one or more dot-separated integers). If you have a copy of the Camel, see page 67 (3rd ed.). Also see Why will v-strings be deprecated.

      I have a copy of the camel, but it's the first edition. No mention of v-strings :\

Re: About variable names
by Coruscate (Sexton) on Nov 20, 2003 at 05:28 UTC

    Your answer has been provided, but just a side note on implementation. Are you using the @keys array to help populate the %list_of_refs, or just as a way of keeping track what keys are within %list_of_refs? If you have the @keys array just so you can say for (@keys) {}, then take a look at keys(). Then you can do something along the lines of for (keys %list_of_refs) {} or my @keys = keys %list_of_refs; for (@keys) {}. This way, you won't have to keep a separate list; you can just generate it automatically whenever you need it :)

      I created a bunch of global hashes that contain references to all sorts of hashes, arrays, variables, and subs, which may or may not be defined, depending on whether or not they are needed. Typically (99%+ of the time) I will not use all of the references within any given global hash.

      However, many of the places where I created the more localized variables, I have several different hashes which have the same keys, but different values or references. So I use an array to keep track of the keys. It's made that way mostly for readability's sake (I use fairly long and descriptive variable names); I plan to pass this code on to other maintainers after I get done with it.

      I really appreciate the tip, though. I was aware of the keys() function, but never really thought about using it :)

Re: About variable names
by Zaxo (Archbishop) on Nov 20, 2003 at 04:35 UTC

    It's possible that you are running into v-strings, an attempt in perl 5.6 to regularize the treatment of version numbers. The attempt created more problems than it solved and was quickly squashed. The deprecation schedule could be called PDQ. See perldata from perl 5.6 or 5.8 for details.

    After Compline,
    Zaxo