App-CELL
view release on metacpan or search on metacpan
lib/App/CELL/Message.pm view on Meta::CPAN
=head1 FUNCTIONS AND METHODS
=head2 supported_languages
Get reference to list of supported languages.
=cut
sub supported_languages {
my $sl = $supp_lang || [ 'en' ];
return $sl;
}
=head2 language_supported
Determine if a given language is supported.
=cut
sub language_supported {
my ( $lang ) = @_;
return 1 if grep( /$lang/, @{ supported_languages() } );
return 0;
}
=head2 default_language
Return the default language.
=cut
sub default_language {
my $dl = $def_lang || 'en';
return $dl;
}
=head2 new
Construct a message object. Takes a PARAMHASH containing, at least,
a 'code' attribute as well as, optionally, other attributes such as
'args' (a reference to an array of arguments). Returns a status object. If
the status is ok, then the message object will be in the payload. See
L</SYNOPSIS>.
=cut
sub new {
my ( $class, %ARGS ) = @_;
my $stringified_args = stringify_args( \%ARGS );
my $my_caller;
my $msgobj = {};
#$log->debug( "Entering Message->new called from " . (caller)[1] . " line " . (caller)[2]);
if ( $ARGS{called_from_status} ) {
$my_caller = $ARGS{caller};
} else {
$my_caller = [ CORE::caller() ];
}
if ( not exists( $ARGS{'code'} ) ) {
return App::CELL::Status->new( level => 'ERR',
code => 'CELL_MESSAGE_NO_CODE',
caller => $my_caller,
);
}
if ( not $ARGS{'code'} ) {
return App::CELL::Status->new( level => 'ERR',
code => 'CELL_MESSAGE_CODE_UNDEFINED',
caller => $my_caller,
);
}
$msgobj->{'code'} = $ARGS{code};
if ( $ARGS{lang} ) {
$log->debug( $ARGS{code} . ": " . $mesg->{ $ARGS{code} }->{ $ARGS{lang} }->{ 'Text' },
cell => 1 );
}
$msgobj->{'lang'} = $ARGS{lang} || $def_lang || 'en';
$msgobj->{'file'} = $mesg->
{ $msgobj->{code} }->
{ $msgobj->{lang} }->
{ 'File' } || '<NONE>';
$msgobj->{'line'} = $mesg->
{ $msgobj->{code} }->
{ $msgobj->{lang} }->
{ 'Line' } || '<NONE>';
# This next line is important: it may happen that the developer wants
# to quickly code some messages/statuses without formally assigning
# codes in the site configuration. In these cases, the $mesg lookup
# will fail. Instead of throwing an error, we just generate a message
# text from the value of 'code'.
my $text = $mesg->
{ $msgobj->{code} }->
{ $msgobj->{lang} }->
{ 'Text' }
|| $msgobj->{code};
# strip out anything that resembles a newline
$text =~ s/\n//g;
$text =~ s/\012/ -- /g;
my $stringy = stringify_args( $ARGS{args} ) || '';
if ( defined $ARGS{args} and @{ $ARGS{args} } and not $text =~ m/%s/ ) {
$ARGS{text} = $text . " ARGS: $stringy";
} else {
# insert the arguments into the message text -- needs to be in an eval
# block because we have no control over what crap the application
# programmer might send us
try {
local $SIG{__WARN__} = sub {
die @_;
( run in 0.878 second using v1.01-cache-2.11-cpan-ceb78f64989 )