Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Ways to store messages...

by kiat (Vicar)
on Dec 12, 2003 at 09:25 UTC ( #314259=perlquestion: print w/replies, xml ) Need Help??
kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hey all,

In a web environment, how do you normally deal with messages whether it's to do with a failed or successful operation? Examples of such messages are:

a) Wrong input.
b) Thanks for signing up with us!
c) Go away, hacker.

I found this solution used by YaBB. The solution is to store an array of messages as follows:

my @msg; $msg[0] = 'Wrong input.'; # Doesn't say much but it's just an example $msg[1] = 'Thanks for signing up with us!'; $msg[2] = 'Go away, hacker.'; # Example of use... sub check_input { #blah blah... msg($[2]); } sub msg { my $msg = shift; start_html("Message"), print p("$msg"); end_html(); }
An advantage of storing messages in the above manner is that the same message can be shared by different parts of the code. Another is that when the message content is changed, the change is effected in all the places it's used.

One disadvantage is that you don't know what a message is unless you look it up in the array. For example, if $msg5 is used somewhere in the code, after a while you tend to forget what it is and have to look at the array code to figure it out. This problem becomes obvious when you have lots of such messages or when the array is stored in another file.

How would you normally do it?

Looking forward to hearing your replies :)

Replies are listed 'Best First'.
Re: Ways to store messages...
by CombatSquirrel (Hermit) on Dec 12, 2003 at 10:08 UTC
    If you want to store messages globally, as in your code, think about using a hash instead of an array. $msg{'cracker'} is more human-readable than $msg[2], especially when your code is long and you don't want to look up the exact ID of a message.
    Entropy is the tendency of everything going to hell.
Re: Ways to store messages...
by EvdB (Deacon) on Dec 12, 2003 at 11:38 UTC
    Alternatively you could write a function that returns the message:
    use MyMsg; # exports get_message. my $msg = get_message( 'cracker' );
    This way you could build goodies into the function that allows clever stuff to happen - such as checking which language to return the message in - not to mention croaking if you ask for a message that does not exist.

    It would also make sense to store your messages centrally in an XML or YAML file as suggested above.

    --tidiness is the memory loss of environmental mnemonics

Re: Ways to store messages...
by Roger (Parson) on Dec 12, 2003 at 11:01 UTC
    You could store the message definitions in an (external) xml configuration file.
    use strict; use XML::Simple; my $msg = LoadMsgConfig(); print "$msg->{0}\n"; # look up by id print "$msg->{thanks}\n"; # look up by mnemonics sub LoadMsgConfig { my $xs = new XML::Simple(); my $xmlmsg = do { local $/; <DATA> }; my $tags = $xs->XMLin($xmlmsg); return undef if ! defined $tags->{msg}; my %msg; foreach (keys %{$tags->{msg}}) { $msg{$_} = $tags->{msg}{$_}{text}; $msg{$tags->{msg}{$_}{id}} = $msg{$_}; } # undef $xmlmsg, $tags, $xs; return \%msg; } __DATA__ <?xml version="1.0"?> <config> <msg id="0" name="wrong_input" text="Wrong input"/> <msg id="1" name="thanks" text="Thanks for signing up with us" +/> <msg id="2" name="go_away" text="Go away hacker"/> </config>
    And the output -
    Wrong input Thanks for signing up with us
Re: Messages - housekeeping...
by Taulmarill (Deacon) on Dec 12, 2003 at 09:36 UTC
    you listed the advanteges and disadvanteges youself.
    when unsing the same messages over and over again in a large script it makes sense to do the array thing you showed.
    however in a smal script where you just have to print a small number of messages and using them mostly once it whould make sense to print them directly where you whant them whithout unsing scalars.
    i think it depends on the circumstances.
Re: Ways to store messages...
by chromatic (Archbishop) on Dec 12, 2003 at 18:14 UTC

    One other advantage is easier localization. If you already have all external messages outside of your code, it's somewhat easier to provide translations into other languages.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://314259]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2017-02-25 17:21 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (366 votes). Check out past polls.