2

I am trying to piece together other scripts i have seen to be able to loop through a list of users on the channel.

Here is what i have come up with

my $channel = @_;
foreach my $nick ($channel->nicks()) {
    $server->command("msg $chatchannel $nick->{nick}");
}

But all i get from that is

Can't call method "nicks" without a package or object reference at /root/.irssi/scripts/test.pl line 64.

which is referring to

$channel->nicks()

Am i going about this the wrong way? or should this be working? I have seen many other scripts using $channel->nicks() so i know it must work?

Edit

I should also mention that this is already define further up in the code

my ($server, $msg, $target, $channel, $chatnet) = @_;

But when i try it with that $channel variable i get

Can't locate object method "nicks" via package "mattigins@mattigins.tmi.twitch.tv" (perhaps you forgot to load "mattigins@mattigins.tmi.twitch.tv"?) at /root/.irssi/scripts/test.pl line 64.

Mattigins
  • 1,014
  • 9
  • 25

2 Answers2

2

Since the left hand side (LHS) of my $channel = @_; is a scalar it imposes scalar context on the @_ array. This means that the length of the array gets assigned to $channel. You want to assign with my ($channel) = @_; so that the LHS is in list context and that the first element in the @_ array gets assigned to your scalar.

Ref:

What is the difference between the scalar and list contexts in Perl?

Scalar and List context in Perl

Community
  • 1
  • 1
Oesor
  • 6,632
  • 2
  • 29
  • 56
  • Thanks for the advise. I tried putting the () around the $channel but now i get this error `Can't locate object method "nicks" via package "Irssi::Irc::Server" at /root/.irssi/scripts/test.pl line 64.` – Mattigins Feb 17 '15 at 03:27
1

I figured out how to do it.

$chan = $server->channel_find('#channel');
foreach my $nick ($chan->nicks()) {
    $nickname = $nick->{nick};
}
Mattigins
  • 1,014
  • 9
  • 25