perleager has asked for the wisdom of the Perl Monks concerning the following question:

hello. im making a instant messenger program for my teen community online.

lets say "ultimatedbz" sends a message to lets say "vegeta". It'll create a file with ultimatedbz's message named "imultimatedbz" in directory /members/vegeta.

There is a frame where refreshes the page every 10 seconds to check if he new messages. When there is a new message it opens the directory and prints out the message from ultimate dbz.

if (-e glob("$free_path/$Account::cata_acc/_data/messages/im*")) { opendir(MESSAGE, "$free_path/$Account::cata_acc/_data/messages/"); @messages = grep !/\.\.?/, readdir MESSAGE; closedir (LOGDIR); foreach $message (@messages) { ($im,$from) = split(/im/, $message); print <<EOF; trim=window.open("http://www.teen-reality.com/cgi-bin/members/trim.cgi +?account=$Account::cata_acc&session=$Account::session&action=main&fro +m=$from&what=newim","","width=180,height=350") trim.creator=self EOF }#foreach $messages }#if -e glob im check if ne im messages #done checking


Now the $from returns a value of "ult" instead of ultimatedbz. Now this is the first time it happend and before, when I try it out by vegeta sending a message to vegeta, it returns a value of "vegeta"

I think it's something dealing with the length of characters in the file name?

Thank you
Dave

Replies are listed 'Best First'.
Re: getting a filename
by chromatic (Archbishop) on Mar 23, 2001 at 00:54 UTC
    While the idea of storing these files in a separate directory (or even a simple DBM-based database, see tie) is the way to go, there's a Learning Opportunity here.

    Constrain your split, especially if you're splitting on something that just may be part of the data you want to save. This would probably be fixed by changing the line to read:

    ($im, $from) = split(/im/, $message, 2);

    Cultivate good habits.

Re: getting a filename
by arturo (Vicar) on Mar 23, 2001 at 00:44 UTC

    Hint: ultimate ...

    Longer answer (left the short one up for a moment, just for forehead-smacking effect ... =). Consider a different scheme for storing the messages (e.g. store all ims in a directory called ... /im) or something like that.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: getting a filename
by myocom (Deacon) on Mar 23, 2001 at 00:44 UTC

    The problem lies in this line: ($im,$from) = split(/im/, $message);

    Since you're splitting on 'im', and 'ultimatedbz' has 'im' in it, it's correctly returning 'ult' (and discarding 'atedbz')

      Oooh. I c.

      Is there any way to fix this?

        instead of using split, use a regex:

        if ( my ($from) = /^im(.*)/ ) { # do stuff }

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        I'd recommend changing your naming scheme for your files, for starters, to something that people won't be naming themselves.

Re: getting a filename
by buckaduck (Chaplain) on Mar 23, 2001 at 04:32 UTC
    For that matter, why not trim down the first few lines? If I count correctly, you check for the existence of these files three times: glob, -e, and readdir

    I also think that a substr would be a much simpler idea here than a split command. (But I will concede that split(/im/,$message,2) will work fine)

    foreach $from (glob("$free_path/$Account::cata_acc/_data/messages/im*" +)) { substr($from, 0, 2) = ''; # Delete the leading "im" print <<EOF; # Trimmed for brevity trim=window.open("...") trim.creator=self EOF }
    buckaduck