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

Hello monks,

I just joined this portal and this is my first question and it's kind of stupid. I have this situation where I have to use . (the concatenation operator) in a variable identifier. I tried escaping, but that didn't work.

This variable is being accessed by another script and I cannot change the name there.

Could anybody provide a workaround to having the "." in the variable name? I need it to be something like $app1.svr

Replies are listed 'Best First'.
Re: Using . in variable name
by Corion (Patriarch) on Apr 17, 2015 at 07:02 UTC

    That's not how Perl variable names work. Perl variable names (mostly) need to match \w+, and dot is not in \w. You cannot have a variable name with dot in it because the dot is an operator in Perl - see perlop.

Re: Using . in variable name
by GrandFather (Saint) on Apr 17, 2015 at 09:09 UTC

    Is this a COM or Windows automation related issue? If so, the Perl variable name is completely unrelated to a variable name that may be used on the other side of the connection.

    I can't think of any common language that I've used that uses "." in an identifier. There are plenty of languages that use "." to reference a member of a structure or object. You really need to tell us more about what you need to achieve because right now you don't make sense.

    Perl is the programming world's equivalent of English
      The variable is being passed to an ant script which invokes JUnit test cases. The Java code is reading the properties and the property name is defined as app1.url and as such in the Java code and we have been asked not to change that.
        so why do you think your perl variabe has to be named app1.url?

        "properties" does not describe ipc such as system

        app1.url is probably just a string you give as an argument to the java program

        system $java, $jar, @otherargs, 'app1.url=yadayada';
Re: Using . in variable name
by Laurent_R (Canon) on Apr 17, 2015 at 06:58 UTC
    Sorry, I am not sure to understand your problem. Could you provide an example of what you are trying to achieve?

    I am afraid that you might be after symbolic references, and this is very much frown upon in the last 20 years of Perl.

    Je suis Charlie.
Re: Using . in variable name
by bitingduck (Deacon) on Apr 17, 2015 at 07:04 UTC

    Can you show some code and explain how you're going to access the variable from another script? Generally things get passed around between scripts and programs in such a way that it doesn't much matter to one program what it's called internally in another one.

      Thanks everyone for the quick responses but that's the thing, I do not have access to the script. I have just been asked to pass it as $app1.svr which I am not able to do from Perl.

      If this doesn't work out, I will have to rewrite my code in another language, which I am hesitating to do.

      Anyways, as you mentioned, it's not possible so I might as well start exploring other options.

        Maybe I didn't understand the problem as you stated it.

        Can you maybe take a step back and tell us the problem you're trying to solve? If you're calling another script, that script is likely invoked via system or exec. Maybe you don't want to use a variable name but a variable value?

        You can append a value to a string by using dot itself, or by using double quotes:

        print "$app.svr\n"; print $app . ".svr\n";