Problem: you have a laptop, which has to work with different wifi networks. For each one, you need different startup options (ie. you have to start openvpn at work, you have to set encryption at home, you just have to run networking in all other cases). You would like to autodetect the network you're in and run configs accordingly.
Solution: instead of the usual networking startup script, run this one. Then create some scripts inside /etc/nw directory, one for each ESSID network you've specified inside the Perl script and a default one. For instance:
#!/bin/sh iwconfig eth1 essid wlanHOME iwconfig eth1 key 917AD823B4EA3395E214BC258B /etc/init.d/networking start
#!/bin/sh /etc/init.d/networking start /etc/init.d/openvpn start
#!/bin/sh /etc/init.d/networking start
... of course it's not perfect, but it's quite easy to understand and I hope that with the code under your hands you'll be able to create something useful.