Hi, i wrote a little perl script that gets called from my IM client (centericq) and keeps a file with my contacts and if they are online and the current nickname they using.
First a bit of information about the ENV variables:
centericq sets up a few environment variables in the shell where the perl script gets run:
$CONTACT_NICK: unlike the name suggests, this variable actually holds the UIN of the user that generated the event. $EVENT_NETWORK: indication of the network the event occured (msn, aim, yahoo, irc, jab, ...) $EVENT_TYPE: the type of event that occured, since i link the script to two events (online & offline) it can be either of those two. $CONTACT_INFODIR: this contains the path to the directory where more information about the user generating the event can be found. Specifically i use this to get the nickname, the file 'info' in that directory contains the nickname of the user on the first line.
So, i want the contacts that come online/offline to be saved in a textfile, one on each line with the following format:
network:contact:status:nickname
Where network is the $EVENT_NETWORK value, contact is the $CONTACT_NICK, status is 1 or 0 indicating current online/offline and nickname is a encoded version of the nickname (since editing the txt file manually became a little hard with all those odd unicode chars in).
Contacts that are allready in the txt file should be updated (status & nickname), while contacts that aren't yet should be added below.
I got everything working correctly, i'm just not too sure if this code can be improved speedwise. I am not much of a perl coder, so i don't know the ins and outs when it comes to optimizing perl code.
Ok, here comes the code, the part before %exec% is centericq dependant, everything below it is standard perl:
No, it's not a comparison. The inner part sets $email to the ENV var $contact_nick,while the if() around it checks if the variable is empty.
This centericq program seems to initiate the call sometimes without filling out the ENV $CONTACT_NICK, i put the contents of this ENV var in the perl variable $email and check if it contains something. This method works on most other languages i know, so i assumed it did in perl, thusfar it seems to work since i haven't got any lines in my file with an empty uin.
Did some tests of myself, and it seems to work out though...
Code:
if($test = 'a') {<br /> print "True\n";<br />}<br />-> prints TRUE<br />if($test = '') {<br /> print "True\n";<br />}<br />-> prints nothing... implying it returns FALSE<br />
I used this kind of method in PHP, i think it worked in VB too but i'm not sure (it's been years since i last coded a VB program). Used it a lot in MOO code too.
So it seems to work on perl too, but obviously i have some controversal methods of testing.
What is undef btw? Is it some predefined variable in perl indicating an empty string?
Will return true as 5*5 = 25... Not very useful code in this context, but i tend to use this kind of method a lot in PHP, setting variables and using them directly...
As i did some more experiments with PERL, it seems to handle that how i would've expected it.
Joined: 03 May 2006 Posts: 2292 Location: Colorado
Posted: Wed Mar 09, 2005 4:18 pm Post subject:
QUOTE(ohsmitt @ Mar 9 2005, 07:43 AM)
No thats not how it works. Cer is right. 1 = 1 is always true. $foo = undef is always true. $foo = is always true.
You need to test for undef. [right][snapback]46677[/snapback][/right]
Actualy brother is completely write. Seting a variable returns the value it was set to.
So
Code:
<br />my $var = undef;<br />if ($test = $var) {<br /> # this will only be called when $var is something that is true<br />} else {<br /> # this will be called anytime $var is something false}<br />}<br />
In both cases $test will equal $var afterwords. _________________ Eric256
Proud previous owner and current admin of Bot-depot.com
No, it's not a comparison. The inner part sets $email to the ENV var $contact_nick,while the if() around it checks if the variable is empty.
This centericq program seems to initiate the call sometimes without filling out the ENV $CONTACT_NICK, i put the contents of this ENV var in the perl variable $email and check if it contains something. This method works on most other languages i know, so i assumed it did in perl, thusfar it seems to work since i haven't got any lines in my file with an empty uin.
I hope this made sense... [right][snapback]46672[/snapback][/right]
If you want to (re)set $email to the ENV variable, if the $email is undefined, do this:
I mainly wanted to see if there is a better method to update the file.
I read all lines in one cycle and rewrite all of them in a second one. I wonder if there is a possibility to seek the line in the file directly, and replace it, without touching the other information.
Not that it causes performance problems, and i doubt it ever will as long as the text file doesn't grow beyond a few megabytes, but i'm just intrested in shortening/optimizing that specific area of the code.
Hmm. Nice work.. but theres a few little mistakes in it, you should really make the variables global(my), and change $nickname = @userinfo[0]; to $nickname = $userinfo[0];
Hmm, i really have to do some reading on the whole global/local thing in variables. Also, i'm not too sure when to use @ and $ on an array.
Since it works i assume the global/local thing only gets important when working with subs and classes, which i'm holding off for a while until i grasp the essentials. I really love to work OO, but i'd need some decent docs on doing that in perl.
Any nice place to learn some of the perl basics? What i'm looking for is something like the PHP documentation site, a place to look up every available builtin function and language structures, with some comments and example code.
<br /># Setup our array :)<br /><br />my @array = ("Hi", "Hello", "Yo");<br /><br /># Lets see all the greetz<br /><br />print "My greeting:\n" . join("\n", @array);<br /><br /># Lets pick the first greeting which is Hi :).<br /><br />print "Well " . $array[0];<br />