This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Puts not working o_O

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
henasraf
Voice
Posts: 4
Joined: Mon Nov 02, 2009 4:34 am
Location: Bat Yam, Israel
Contact:

Puts not working o_O

Post by henasraf »

I've used puts a fair amount of times, not once has it cause this.
I get no error at all; the file exists, all the variables it needs exist and contain the right contents; but the file just doesn't get written to.

Code: Select all

proc spidey:settings:greet { nick host hand chan text } {
    
    global nogreetpath
    
    if { [lindex $text 0] == "off" } {
        set nicklist [split [read -nonewline [open "$nogreetpath\\nogreet.txt" r]] "\n"]
        if { [lsearch $nicklist $nick] == -1 } {
            puts [open "$nogreetpath\\nogreet.txt" a] $nick
        }
        putnotc $nick "You will no longer be greeted when you enter a channel with me in it."
        putnotc $nick "Type \002!setting greet on\002 to be greeted again."
    } elseif { [lindex $text 0] == "on" } {
        set foundnick 0
        set nicklist [split [read -nonewline [open "$nogreetpath\\nogreet.txt" r]] "\n"]
        while { [lsearch $nicklist $nick] != -1 } {
            set index [lsearch $nicklist $nick]
            set nicklist [lreplace $nicklist $index $index]
        }
        puts [open "$nogreetpath\\nogreet.txt" w] [join $nicklist "\n"]
        putnotc $nick "You will be greeted every time you enter a channel with me in it."
        putnotc $nick "Type \002!setting greet off\002 to stop being greeted."
    } else {
        putnotc $nick "Invalid setting. Must be \002on|off\002."
    }
}
I put around some putlogs (removed from here) to see that the vars are all correct. They are, and the putlogs execute meaning it's not a conditions problem. The messages also get echoed correctly. Wth is this happening? The "on" setting also works; puts work fine there.
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

combining different things seem fine in order to shorten code and make it a bit faster, but definately hard to read - even if you are looking for an error.

my guess:

the channel which is being opened up with open isnt being closed, maybe you also would like to put in putlogs after each line to see where it stops working, explode the following part into different lines:

Code: Select all

puts [open "$nogreetpath\\nogreet.txt" a] $nick
in example:

Code: Select all

set filename "$nogreetpath/nogreet.txt"
set file [open $filename a]
puts $file $nick
close $file
Im kind of wondering why you are using \\ since I always used / to separate different directories from each other, works even on windows based systems. and $file (in my case) already exists I guess since you are reading from it as well?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Just to confirm raider2k's suspicions; since you don't close the file, the output will never be flushed to the file, as tcl buffers file operations by default.

Further, invoking this proc over and over will "leak" file handlers, up until the point (usually around 1023 file handlers) where you have exhausted the available resources, and your eggdrop will be unable to open any other files or network connections.

Tcl does not come with a garbage collector, so you'll have to take the effort of keeping track/cleaning up your resources such as file handles and sockets yourself. The only cleanup done is the anonymous namespace of the proc itself (local variables will be removed as the proc ends).
NML_375
User avatar
henasraf
Voice
Posts: 4
Joined: Mon Nov 02, 2009 4:34 am
Location: Bat Yam, Israel
Contact:

Post by henasraf »

Yeah... still not working. Same goes for another script of mine, too. Should be simple, but noooooooooo....

Code: Select all

proc henasraf:satiel:add { nick host hand chan text } {
    if { $text != "" } {
        set fopen [open $::satielfile a]
        puts $fopen $text
        close $fopen
        putmsg $chan "Added <$text> to Satiels database"
    }
}
Sends the message to the channel but doesn't actually do anything to the file. $::satielfile refers to the right file path/name. Not a problem with the variable, it works fine for reading from the file. Iunno what to do |:
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Should work like that, assuming you havn't already opened $::satielfile for writing without closing it earlier.
NML_375
Post Reply