sriram83.life has asked for the wisdom of the Perl Monks concerning the following question:

I have a script as follows:
#!/usr/bin/perl use Mailer; my $circle = Chennai; my $file = "TO_" . "$circle"; print "MailDetails : $Mailer::$file\n";
And Module "Mailer" as follows:
Package Mailer; our $TO_Chennai = q(sriram.perumalla@ness.com); 1;
Now, when i print $Mailer::$file , I should get "sriram.perumalla@ness.com".But, it prints nothing. Can any one suggest me how to get the value of the variable in the perl script which is dynamically named and it's variable value is in other module.

Regards,

Sriram

Replies are listed 'Best First'.
Re: Dynamically name the variable and get it's value from Module
by Corion (Patriarch) on Apr 28, 2015 at 13:35 UTC

    Please don't do that. Use a hash that stores all the adresses:

    package Mailer; our( %TO ); $TO{ Chennai }= 'sriram.perumalla@ness.com'; package main; print $TO{ Chennai };

    If you want to learn about why it's a bad practice, read http://perl.plover.com/varvarname.html and the two follow-up parts.

Re: Dynamically name the variable and get it's value from Module
by Laurent_R (Canon) on Apr 28, 2015 at 17:43 UTC
    If you had the use strict; pragma at the beginning of your script (and you should), you would get a first error at compile time:
    Bareword "Chennai" not allowed while "strict subs" in use at ...
    And, once corrected by adding quote marks, you would get a second error at runtime:
    $ perl -e ' > use strict; > use warnings; > > our $TO_Chennai = "foobar"; > > my $circle = "Chennai"; > my $file = "TO_" . "$circle"; > print $$file;' Can't use string ("TO_Chennai") as a SCALAR ref while "strict refs" in + use at -e line 9.
    Symbolic references were once (before Perl 5, i.e. more than 20 years ago) the only way to do certain types of things similar to what is sometimes done in shell scripting, but they are really frown upon in today's Perl programming. They are really a source of errors often difficult to track. To the point that they are forbidden with strictures (there are still there with no strictures only to preserve compatibility of very old programs). Use a hash instead.

    Do yourself a favor: use strict; and use warnings;, and the compiler will tell you immediately about many of your mistakes, typos, etc., and you'll gain a lot of time.

    Je suis Charlie.
Re: Dynamically name the variable and get it's value from Module
by hdb (Monsignor) on Apr 28, 2015 at 13:34 UTC

    It should be

    $Mailer::file

    Update: Forget my reply. I misread the code and the question.