Base.pm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # ------------------------------------------------------------------------------
  2. # NAME
  3. # Fcm::Base
  4. #
  5. # DESCRIPTION
  6. # This is base class for all FCM OO packages.
  7. #
  8. # COPYRIGHT
  9. # (C) Crown copyright Met Office. All rights reserved.
  10. # For further details please refer to the file COPYRIGHT.txt
  11. # which you should have received as part of this distribution.
  12. # ------------------------------------------------------------------------------
  13. package Fcm::Base;
  14. # Standard pragma
  15. use strict;
  16. use warnings;
  17. use Fcm::Config;
  18. my @scalar_properties = (
  19. 'config', # instance of Fcm::Config, configuration setting
  20. );
  21. # ------------------------------------------------------------------------------
  22. # SYNOPSIS
  23. # $obj = Fcm::Base->new;
  24. #
  25. # DESCRIPTION
  26. # This method constructs a new instance of the Fcm::Base class.
  27. # ------------------------------------------------------------------------------
  28. sub new {
  29. my $this = shift;
  30. my %args = @_;
  31. my $class = ref $this || $this;
  32. my $self = {};
  33. for (@scalar_properties) {
  34. $self->{$_} = exists $args{uc ($_)} ? $args{uc ($_)} : undef;
  35. }
  36. bless $self, $class;
  37. return $self;
  38. }
  39. # ------------------------------------------------------------------------------
  40. # SYNOPSIS
  41. # $value = $obj->X;
  42. # $obj->X ($value);
  43. #
  44. # DESCRIPTION
  45. # Details of these properties are explained in @scalar_properties.
  46. # ------------------------------------------------------------------------------
  47. for my $name (@scalar_properties) {
  48. no strict 'refs';
  49. *$name = sub {
  50. my $self = shift;
  51. # Argument specified, set property to specified argument
  52. if (@_) {
  53. $self->{$name} = $_[0];
  54. }
  55. # Default value for property
  56. if (not defined $self->{$name}) {
  57. if ($name eq 'config') {
  58. # Configuration setting of the main program
  59. $self->{$name} = Fcm::Config->instance();
  60. }
  61. }
  62. return $self->{$name};
  63. }
  64. }
  65. # ------------------------------------------------------------------------------
  66. # SYNOPSIS
  67. # $value = $self->setting (@args); # $self->config->setting
  68. # $value = $self->verbose (@args); # $self->config->verbose
  69. # ------------------------------------------------------------------------------
  70. for my $name (qw/setting verbose/) {
  71. no strict 'refs';
  72. *$name = sub {
  73. my $self = shift;
  74. return $self->config->$name (@_);
  75. }
  76. }
  77. # ------------------------------------------------------------------------------
  78. # SYNOPSIS
  79. # $value = $self->cfglabel (@args);
  80. #
  81. # DESCRIPTION
  82. # This is an alias to $self->config->setting ('CFG_LABEL', @args);
  83. # ------------------------------------------------------------------------------
  84. sub cfglabel {
  85. my $self = shift;
  86. return $self->setting ('CFG_LABEL', @_);
  87. }
  88. # ------------------------------------------------------------------------------
  89. 1;
  90. __END__