Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Write to file, after manipulation

by ibanix (Hermit)
on Jan 28, 2003 at 04:02 UTC ( [id://230480]=note: print w/replies, xml ) Need Help??


in reply to Write to file, after manipulation

Hi,

First off, have you looked at Net::IRC, Bot::BasicBot or Bot::Pluggable? They may save you some time, unless you're determined to reinvent the wheel. If you are, see if you can build a whole bike for us. :-)

I'm not sure how your code looks outside of your posting, but may I suggest you use a set of functions that is given to easily readable and reuseable code? I happen to like the following type of method myself:
# Untested code for demonstation purposes only while($command = <IRC>) { last if /please leave, $botname/; bot_command($command); } sub bot_command { my $command = shift(@_); join_channel($command) if ($command =~ /join/); part_channel($command) if ($command =~ /part/); msg_user($command) if ($command =~ /msg/); .... default($command); } sub join_channel { ... } sub part_channel { ... }
I'm not saying this is the best way to do it, but it's worked for me when I need to multiplex between a number of different choices. It keeps my code bits isolated from each other, and it's easy for other people to read.

This being said, I'm still trying to figure out how a dispatch table works -- if you can figure that one out, that might be a good bet for you.

I should probally attempt to answer your question, eh? :-)

You're opening the file as open(REMOVE, ">project.pjt") Which is output only, but you're trying to read from it via
$remove = <REMOVE>;
If you have use warnings turned on, when you try this you should get an error like:
Filehandle FILE opened only for output at C:\file.pl line 7.
To open a file for reading AND write, use:
open(REMOVE, "+>project.pjt")
but this will clober your file, removing any contents before you can read from it. Use
open(REMOVE, "+>>project.pjt")
if you want to read and write (append) to the file without zapping the contents everytime. See also the perl Open() tutorial.

Hope this helps (and that I got it right),
ibanix

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: Write to file, after manipulation
by FireBird34 (Pilgrim) on Jan 28, 2003 at 05:08 UTC
    I actually didn't want anything to complex... All I wanted was a bot that would preform simple tasks (such as add/remove/list data basically, given the data from a file). Also, I wanted to read from the file, then replace whatever was in the file with the new manipulation. Will take a look at the above comments though, thanks :)

    I plan on making a few *fun* bots, so I'll take a look at the above modules aswell =) Thanks!

      ibanix did answer your question - it's just somewhat overshadowed by his misc. comments in the top half of his node. The problem is you're opening the file for writing only, and then attempting to read from it.

      You may want to have a look at perlopentut sometime for some of the subtleties of working with files.

      Makeshifts last the longest.

        Will do, thanks. I got it working -- it may be a bit *simplistic*, but I just opened the file, read from it, closed it, then re-opened it for writing... I'm just stuck on one other little portion, but that's on the IRC side. Again, thanks -- I'll head over and take a look at that tutorial aswell.
Re: Re: Write to file, after manipulation
by jdporter (Paladin) on Jan 28, 2003 at 19:54 UTC
    ibanix wrote:
    sub bot_command { my $command = shift(@_); join_channel($command) if ($command =~ /join/); part_channel($command) if ($command =~ /part/); msg_user($command) if ($command =~ /msg/); .... default($command); }
    Ugh!
    sub bot_command { local $_ = shift; /join/ ? &join_channel : /part/ ? &part_channel : /msg/ ? &msg_user : &default }
    I'm still trying to figure out how a dispatch table works
    Like this:
    sub join_channel { ... } sub part_channel { ... } sub msg_user { ... } my %dispatch = ( join => \&join_channel, part => \&part_channel, msg => \&msg_user, ); sub bot_command { my $cmd = shift; $dispatch{$cmd} ? &{$dispatch{$cmd}} : &default }

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

      But then, another way is to let perl manage the dispatch table for you.
      Like so:
      sub BotCmd::join_channel { ... } sub BotCmd::part_channel { ... } sub BotCmd::msg_user { ... } sub BotCmd::default { ... } my %method_map = ( # cmd: method: join => 'join_channel', part => 'part_channel', msg => 'msg_user', ); sub bot_command { my $cmd = shift; my $method = $method_map{ $cmd || 'default' }; BotCmd->$method(@_); }

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.

      Ugh!

      I said I liked it -- not that you had to. TIMTOWDI.

      Thanks for the dispatch table introduction! I think I may be using that method in my code soon.

      ibanix

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://230480]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-23 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found