Skip to content

Commit b5dd677

Browse files
committed
Add Perl tutorial files.
1 parent eefd0bf commit b5dd677

13 files changed

+576
-0
lines changed

perl/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Perl code for RabbitMQ tutorials
2+
3+
Here you can find Perl code examples from [RabbitMQ
4+
tutorials](http://www.rabbitmq.com/getstarted.html).
5+
6+
To successfully use the examples you will need a running RabbitMQ server.
7+
8+
## Requirements
9+
10+
To run this code you need to intall Net::RabbitFoot
11+
12+
For tutorial six UUID::Tiny needs to be installed.
13+
14+
## Code
15+
16+
[Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html):
17+
18+
perl send.pl
19+
perl receive.pl
20+
21+
22+
[Tutorial two: Work Queues](http://www.rabbitmq.com/tutorial-two-python.html):
23+
24+
perl new_task.pl "A very hard task which takes two seconds.."
25+
perl worker.pl
26+
27+
28+
[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html):
29+
30+
perl receive_logs.pl
31+
perl emit_log.pl "info: This is the log message"
32+
33+
34+
[Tutorial four: Routing](http://www.rabbitmq.com/tutorial-four-python.html):
35+
36+
perl receive_logs_direct.pl info
37+
perl emit_log_direct.pl info "The message"
38+
39+
40+
[Tutorial five: Topics](http://www.rabbitmq.com/tutorial-five-python.html):
41+
42+
perl receive_logs_topic.pl "*.rabbit"
43+
perl emit_log_topic.pl red.rabbit Hello
44+
45+
46+
[Tutorial six: RPC](http://www.rabbitmq.com/tutorial-six-python.html):
47+
48+
perl rpc_server.pl
49+
perl rpc_client.pl

perl/emit_log.pl

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Net::RabbitFoot;
7+
8+
use Data::Dumper;
9+
10+
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
11+
host => 'localhost',
12+
port => 5672,
13+
user => 'guest',
14+
pass => 'guest',
15+
vhost => '/',
16+
);
17+
18+
my $channel = $conn->open_channel();
19+
20+
$channel->declare_exchange(
21+
exchange => 'logs',
22+
type => 'fanout',
23+
);
24+
25+
my $msg = join(' ', @ARGV) || "info: Hello World!";
26+
27+
$channel->publish(
28+
exchange => 'logs',
29+
routing_key => '',
30+
body => $msg,
31+
);
32+
33+
print " [x] Sent $msg\n";
34+
35+
$conn->close();

perl/emit_log_direct.pl

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Net::RabbitFoot;
7+
8+
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
9+
host => 'localhost',
10+
port => 5672,
11+
user => 'guest',
12+
pass => 'guest',
13+
vhost => '/',
14+
);
15+
16+
my $channel = $conn->open_channel();
17+
18+
$channel->declare_exchange(
19+
exchange => 'direct_logs',
20+
type => 'direct',
21+
);
22+
23+
my $severity = delete $ARGV[0] || 'info';
24+
my $msg = join(' ', @ARGV[1..$#ARGV]) || 'Hello World!';
25+
26+
$channel->publish(
27+
exchange => 'direct_logs',
28+
routing_key => $severity,
29+
body => $msg,
30+
);
31+
32+
print " [x] Send $severity: $msg\n";
33+
34+
$conn->close();

perl/emit_log_topic.pl

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Net::RabbitFoot;
7+
use AnyEvent;
8+
9+
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
10+
host => 'localhost',
11+
port => 5672,
12+
user => 'guest',
13+
pass => 'guest',
14+
vhost => '/',
15+
);
16+
17+
my $channel = $conn->open_channel();
18+
19+
$channel->declare_exchange(
20+
exchange => 'topic_logs',
21+
type => 'topic',
22+
);
23+
24+
my $routing_key = $ARGV[0] || 'anonymous.info';
25+
my $msg = join(' ', @ARGV[1..$#ARGV]) || 'Hello World!';
26+
27+
$channel->publish(
28+
exchange => 'topic_logs',
29+
routing_key => $routing_key,
30+
body => $msg,
31+
);
32+
33+
print " [x] Sent $routing_key:$msg\n";
34+
35+
$conn->close();
36+

perl/new_task.pl

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Net::RabbitFoot;
7+
8+
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
9+
host => 'localhost',
10+
port => 5672,
11+
user => 'guest',
12+
pass => 'guest',
13+
vhost => '/',
14+
timeout => 1,
15+
);
16+
17+
18+
my $chan = $conn->open_channel();
19+
20+
$chan->declare_queue(
21+
queue => 'task_queue',
22+
durable => 1,
23+
);
24+
25+
my $msg = join(' ', @ARGV) || "Hello World!";
26+
27+
$chan->publish(
28+
exchange => '',
29+
routing_key => 'task_queue',
30+
body => $msg,
31+
);
32+
33+
print " [x] Sent '$msg'\n";
34+
35+
$conn->close();
36+

perl/receive.pl

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Net::RabbitFoot;
7+
use AnyEvent;
8+
9+
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
10+
host => 'localhost',
11+
port => 5672,
12+
user => 'guest',
13+
pass => 'guest',
14+
vhost => '/',
15+
);
16+
17+
my $ch = $conn->open_channel();
18+
19+
$ch->declare_queue(queue => 'hello');
20+
21+
print " [*] Waiting for messages. To exit press CTRL-C\n";
22+
23+
sub callback {
24+
my $var = shift;
25+
my $body = $var->{body}->{payload};
26+
print " [X] Recevied $body\n";
27+
}
28+
29+
$ch->consume(
30+
on_consume => \&callback,
31+
no_ack => 1,
32+
);
33+
34+
# Wait forever
35+
AnyEvent->condvar->recv;
36+

perl/receive_logs.pl

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use AnyEvent;
7+
use Net::RabbitFoot;
8+
9+
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
10+
host => 'localhost',
11+
port => 5672,
12+
user => 'guest',
13+
pass => 'guest',
14+
vhost => '/',
15+
);
16+
17+
my $channel = $conn->open_channel();
18+
19+
$channel->declare_exchange(
20+
exchange => 'logs',
21+
type => 'fanout',
22+
);
23+
24+
my $result = $channel->declare_queue( exclusive => 1, );
25+
26+
my $queue_name = $result->{method_frame}->{queue};
27+
28+
$channel->bind_queue(
29+
exchange => 'logs',
30+
queue => $queue_name,
31+
);
32+
33+
print " [*] Waiting for logs. To exit press CTRL-C\n";
34+
35+
sub callback {
36+
my $var = shift;
37+
my $body = $var->{body}->{payload};
38+
39+
print " [x] $body\n";
40+
}
41+
42+
$channel->consume(
43+
on_consume => \&callback,
44+
queue => $queue_name,
45+
no_ack => 1,
46+
);
47+
48+
AnyEvent->condvar->recv;

perl/receive_logs_direct.pl

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use AnyEvent;
7+
use Net::RabbitFoot;
8+
9+
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
10+
host => 'localhost',
11+
port => 5672,
12+
user => 'guest',
13+
pass => 'guest',
14+
vhost => '/',
15+
);
16+
17+
my $channel = $conn->open_channel();
18+
19+
$channel->declare_exchange(
20+
exchange => 'direct_logs',
21+
type => 'direct',
22+
);
23+
24+
my $result = $channel->declare_queue(
25+
exclusive => 1,
26+
);
27+
28+
my $queue_name = $result->{method_frame}->{queue};
29+
30+
my @severities = @ARGV or die "Usage: $0 [info] [warning] [error]\n";
31+
foreach my $severity (@severities) {
32+
$channel->bind_queue(
33+
exchange => 'direct_logs',
34+
queue => $queue_name,
35+
routing_key => $severity,
36+
);
37+
}
38+
39+
print " [*] Waiting for logs. To exit press CTRL-C\n";
40+
41+
sub callback {
42+
my $var = shift;
43+
my $body = $var->{body}->{payload};
44+
my $routing_key = $var->{deliver}->{method_frame}->{routing_key};
45+
print " [x] $routing_key: $body\n";
46+
}
47+
48+
$channel->consume(
49+
on_consume => \&callback,
50+
no_ack => 1,
51+
);
52+
53+
# Wait forever
54+
AnyEvent->condvar->recv;

perl/receive_logs_topic.pl

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Net::RabbitFoot;
7+
use AnyEvent;
8+
9+
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
10+
host => 'localhost',
11+
port => 5672,
12+
user => 'guest',
13+
pass => 'guest',
14+
vhost => '/',
15+
);
16+
17+
my $channel = $conn->open_channel();
18+
19+
$channel->declare_exchange(
20+
exchange => 'topic_logs',
21+
type => 'topic',
22+
);
23+
24+
my $result = $channel->declare_queue(exclusive => 1);
25+
26+
my $queue_name = $result->{method_frame}->{queue};
27+
28+
my @binding_keys = @ARGV or die "Usage: $0 [binding_key]...\n";
29+
30+
for my $key (@binding_keys) {
31+
$channel->bind_queue(
32+
exchange => 'topic_logs',
33+
queue => $queue_name,
34+
routing_key => $key,
35+
);
36+
}
37+
38+
print " [*] Waiting for logs. To exit press CTRL-C\n";
39+
40+
sub callback {
41+
my $var = shift;
42+
my $body = $var->{body}->{payload};
43+
my $routing_key = $var->{deliver}->{method_frame}->{routing_key};
44+
print " [x] $routing_key:$body\n";
45+
}
46+
47+
$channel->consume(
48+
on_consume => \&callback,
49+
no_ack => 1,
50+
);
51+
52+
# Wait forever
53+
AnyEvent->condvar->recv;
54+

0 commit comments

Comments
 (0)