If you don't mind that it can be changed, you can simply declare your constants as follows:
# Constants used throughout the program:
my $number_of_pages = 3;
If you are really paranoid and don't want your constants to be changeable, use the constant pragma:
use constant NUMBER_OF_PAGES => 3;
| [reply] [d/l] [select] |
Thank you. I used this pragma already but had the problems mentioned in the bugs section (brewordsm, sub calls). Does there exist any other alternative (no my or our declaration)?
| [reply] |
You can decalare a sub routine...
use strict;
use warnings;
sub PI () {3.14}
print PI;
still not sure what you've got against 'my' though
---
my name's not Keith, and I'm not reasonable.
| [reply] [d/l] |
| [reply] |
Beside the already mentioned constant pragma, there's also the Readonly module. The advantage of Readonly is that you can mark interpolatable variable readonly (that is, constant), instead of using subroutines.
use Readonly;
Readonly my $foo => "bar";
$foo .= "baz";
__END__
Modification of a read-only value attempted at ...
| [reply] [d/l] |
Or just do it the old-fashioned way if all you need is constant scalars and you don't care if they are globals. Since, I find I often want my constants to be global and I rarely need a constant array or hash, this works very well for me. And on those rare occassions when I do have a need for a constant array (or hash), I never need to interpolate them... so use constant generally works just fine in those cases.
*PI = \3.14159;
*SLOGAN = \q(Don't tread on me!);
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] [select] |
Based on the question, I would assume your looking for the C/C++ #define method for Perl. You can find an explaination of converting C/C++ #define methods to Perl here.
Simply put, the #define used in C/C++ does not exist in Perl, atleast not in the same way. You will need to use use constant to define a fixed value. You would need it, idealy, in your Perl script head, or in a use command (in the script head), like use myheadderfile; where myheadderfile is your #define's in Perl methods with .pm for the file extension (verses .h or .hpp).
In regards to using my, in most situations my should be use, there are, however, situations where my causes the script to fail or causes incorrect output (few, but they do exist). Based on what you asked for, they should be fine, and should be used. | [reply] [d/l] [select] |
Hi,
You can use something like this.This is a sample.
use constant USER => qw(email
first_name
last_name
login
mobile_phone
phone
);
-Prasanna.K | [reply] [d/l] |