in reply to Re^2: Slices from array reference
in thread Slices from array reference
If you hate "magic numbers" appearing scattered throughout a script, you can use enum to set up constants with similar syntax to C:
# At the top of the script... use enum qw( YR=0 MO DM DY=7 WK ); # ...later... my( $yr, $mo, $dm, $dy, $wk ) = @{$values}[ YR, MO, DM, DY, WK ];
The same effect can be achieved with the constant pragma, though the setup is a little less convenient.
Update: I haven't used enum before, but I like how it seems so familiar to someone who has spent time with C or C++. I discovered it when I went looking for a module that would provide the inlinable functions that constant does, but with a declaration syntax closer to the convenience of C's enums. Imagine that; a Perl programmer going looking for C-like convenience!!! That will never happen again. Anyway, why I'm really happy for having found it is because the module's documentation led me to this: Neil Bowers' blog on "CPAN modules for defining constants".
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Slices from array reference
by AnomalousMonk (Archbishop) on Mar 25, 2014 at 16:30 UTC |