Changeset 495 in ProjectBuilder for devel/pb-modules/lib/ProjectBuilder/Base.pm


Ignore:
Timestamp:
Jul 10, 2008, 11:36:18 AM (16 years ago)
Author:
Bruno Cornec
Message:
  • all global variables are prefixed with pb
  • First attempt at using locale and gettext
  • use of pb_display and pb_display_init added
  • Update presentation following RMLL 2008
File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/pb-modules/lib/ProjectBuilder/Base.pm

    r482 r495  
    2020use Pod::Usage;
    2121use English;
     22use locale;
     23use Locale::gettext;
     24use POSIX qw(setlocale);
    2225
    2326# Inherit from the "Exporter" module which handles exporting functions.
     
    2831# any code which uses this module.
    2932 
    30 our $debug = 0;         # Global debug level
    31 our $LOG = \*STDOUT;    # File descriptor of the log file
    32 our $synmsg = "Error";  # Global error message
     33our $pbdebug = 0;       # Global debug level
     34our $pbLOG = \*STDOUT;  # File descriptor of the log file
     35our $pbsynmsg = "Error";    # Global error message
     36our $pbdisplaytype = "text";
     37                        # default display mode for messages
     38our $pblocale = "C";
    3339
    3440our @ISA = qw(Exporter);
    35 our @EXPORT = qw(pb_mkdir_p pb_system pb_rm_rf pb_get_date pb_log pb_log_init pb_get_uri pb_get_content pb_display_file pb_syntax_init pb_syntax pb_temp_init $debug $LOG);
     41our @EXPORT = qw(pb_mkdir_p pb_system pb_rm_rf pb_get_date pb_log pb_log_init pb_display pb_display_init pb_get_uri pb_get_content pb_display_file pb_syntax_init pb_syntax pb_temp_init $pbdebug $pbLOG $pbdisplaytype $pblocale);
    3642
    3743=pod
     
    7985  pb_log_init(2,\*STDOUT);
    8086  pb_log(1,"Message to print\n");
     87
     88  #
     89  # Manages prints of the program
     90  #
     91  pb_display_init("text","fr_FR:UTF-8");
     92  pb_display("Message to print\n");
    8193
    8294  #
     
    220232sub pb_log_init {
    221233
    222 $debug = shift || 0;
    223 $LOG = shift || \*STDOUT;
    224 pb_log(1,"Debug value: $debug\n");
     234$pbdebug = shift || 0;
     235$pbLOG = shift || \*STDOUT;
     236pb_log(1,"Debug value: $pbdebug\n");
    225237
    226238}
     
    249261my $msg = shift;
    250262
    251 print $LOG "$msg" if ($dlevel <= $debug);
     263print $pbLOG "$msg" if ($dlevel <= $pbdebug);
     264}
     265
     266
     267=item B<pb_display_init>
     268
     269This function initializes the environment used by the pb_display function.
     270
     271The first parameter is the type of display which will be used. Could be "text", "web", "newt",...
     272The second parameter is the loacle to be used.
     273
     274The call to B<pb_display_init> is typically done after getting a parameter on the CLI indicating the locale used or the type of interface to report messages to.
     275
     276=cut
     277
     278sub pb_display_init {
     279
     280$pbdisplaytype = shift || "text";
     281$pblocale = shift || "C";
     282
     283setlocale(LC_ALL, $pblocale);
     284pb_log(1,"Using $pbdisplaytype interface with $pblocale locale\n");
     285
     286if ($pbdisplaytype =~ /text/) {
     287} elsif ($pbdisplaytype = /newt/) {
     288} else {
     289    die "display system $pbdisplaytype unsupported";
     290}
     291}
     292
     293=item B<pb_display>
     294
     295This function prints the messages passed as parameter using the configuration set up with the B<pb_display_init> function.
     296
     297Here is a usage example:
     298
     299  pb_display_init("text","fr_FR.UTF-8");
     300  pb_display("Hello World\n");
     301
     302  will print:
     303 
     304  Bonjour Monde
     305
     306=cut
     307
     308sub pb_display {
     309
     310my $msg = shift;
     311
     312if ($pbdisplaytype =~ /text/) {
     313    print STDOUT gettext($msg);
     314    }
    252315}
    253316
     
    298361sub pb_syntax_init {
    299362
    300 $synmsg = shift || "Error";
     363$pbsynmsg = shift || "Error";
    301364}
    302365
     
    320383$filehandle = \*STDOUT if ($exit_status == 0);
    321384
    322 pod2usage( { -message => $synmsg,
     385pod2usage( { -message => $pbsynmsg,
    323386             -exitval => $exit_status  ,
    324387             -verbose => $verbose_level,
Note: See TracChangeset for help on using the changeset viewer.