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

hi guys,

I have a stuped problem I'm using Net::IRC and i'm trying to write a IRC bot. I want to store some data in a file from web form and then read this data from bot script in IRC. the problem is that when I have the variable $nick in the text file it has no value but have just $nick ( as a string ) here are my files ( part of them )

its my text file ( test.irc )

##########

!beer|give to $nick one beer|cheers $nick

#############

its part of my bot file

##########

open(INF,"test.irc"); @ary=<INF>; close(INF); foreach $line (@ary) { chomp($line); ($txt_cmd,$txt_me,$txt_chan)=split(/\|/,$line); if ($text eq $txt_cmd){ $conn->me($conn->{channel},"$txt_me"); $conn->privmsg($conn->{channel}, "$txt_chan"); } }

###############

here is my output in IRC

[00:13] <boilera> !beer [00:13] * Curolina give to $nick one beer [00:13] <Curolina> cheers $nick

but I want this to be like this :

[00:13] <boilera> !beer [00:13] * Curolina give to boilera one beer [00:13] <Curolina> cheers boilera

thanx to all that will reply

boilera

Replies are listed 'Best First'.
Re: Problems with Net::IRC
by jasonk (Parson) on Feb 16, 2003 at 22:30 UTC

    The text you are reading in from the file is just that, text. The variables aren't going to get interpolated unless you either eval them, or substitute the variables yourself.

    $conn->me($conn->{channel},eval $txt_me); $conn->privmsg($conn->{channel}, eval $txt_chan);

    This doesn't take into account any of the security issues you need to consider when using eval though.

    The other option is to do the substitution of any variables in your data file yourself.

    $txt_me =~ s/\$nick/$nick/g; $txt_chan =~ s/\$nick/$nick/g;
Re: Problems with Net::IRC
by Wonko the sane (Curate) on Feb 17, 2003 at 01:31 UTC
    A small utility routine that I like to use to do this, sinice the need arises so often. Good way to avoid some of the security risks inherent to using eval. Gives you the control of exactly 'what' is allowed to be interpreted.
    #--------------------------------------------------------------------- +--------- =pod =head2 interpolate( scalar, href ) Purpose : To interpolate an href of vars into a string. Usage : my $template = q{$name was also know as $alias.}; my $variables = { '$name' => 'Bob', '$alias' => 'Big Bad Bob', }; interpolate( $template, $variables ); =cut #--------------------------------------------------------------------- +--------- sub interpolate { my ( $text, $variables ) = @_; $text =~ s/$_/$variables->{$_}/g for ( keys %$variables ); return $text; }

    Wonko

      10x guys

      You are GREAT !!!