Channel(3pm)

πŸ¦‹ Channel(3pm) πŸ¦‹

okay so bestie, this is the Channel(3pm) page, and it comes to you from the User Contributed Perl Documentation ✨ every single detail is staying, we're just giving it the glow-up it deserves πŸ’…


✨ NAME ✨

Coro::Channel β€” message queues πŸ’Œ

like... a queue. for messages. that's it, that's the vibe. iconic behavior honestly.

🌈 SYNOPSIS 🌈

use Coro;

$q1 = new Coro::Channel <maxsize>;

$q1->put ("xxx");
print $q1->get;

die unless $q1->size;

πŸ’– DESCRIPTION πŸ’–

so a Coro::Channel is literally the equivalent of a unix pipe β€” and also kinda similar to amiga message ports, iykyk 😌 you put stuff in at one end and read stuff out at the other end. if the capacity of the Channel is maxed out, writers will block β€” no cap, they just sit there waiting. and both ends of a Channel can be read from and written to by as many coroutines as you want, all concurrently, no fighting, no drama, we love a communal queen 🫢

and the best part? you don't have to load Coro::Channel manually, bestie. it gets loaded automatically when you use Coro and call the new constructor. it's giving auto-pilot ✨


  • $q = new Coro:Channel $maxsize β€” makes you a brand new channel with that maximum size you asked for (and it's practically unlimited if maxsize is omitted or is zero 🌈). giving a size of one hands you a traditional channel, like a queue that can only ever hold a single element β€” which means there's no buffering at all, and put will just wait around until there's a matching get call to hand it to 😭. to buffer one element you have to say 2, and so on, and so on. the math is mathing.
  • $q->put ($scalar) β€” puts the given scalar into the queue. that's the whole thing, bestie. yeet it in there πŸ“₯
  • $q->get β€” returns the next element from the queue, and if there isn't one yet, it waits. patient queen behavior πŸ’…
  • $q->shutdown β€” shuts down the Channel by pushing a virtual end marker onto it πŸŽ€ what does that do? it changes the behaviour of the Channel, so that when it becomes empty (or when it is empty) it returns undef β€” almost as if infinitely many undef elements had been put into the queue. yes, infinitely many. we said what we said.

specifically: this function wakes up any pending get calls and lets them return undef, and the same thing happens on all future get calls too. size will return the real number of stored elements, though. so like, size is not buying into the drama.

another way to describe the behaviour is that get calls will not block when the queue becomes empty β€” they just immediately return undef. this means that calls to put will work normally, and the data will be returned on subsequent get calls. so it's not closed-closed, it's more like... emotionally unavailable πŸ₯²

this method is useful to signal the end of data to any consumers, quite similar to an end of stream on, say, a tcp socket. so picture it: you have one or more producers that put data into the Channel, and one or more consumers who get them. when all the producers have finished producing data, a call to shutdown signals that fact to any consumers. communication! teamwork! iconic!

a common implementation uses one or more threads that get from a channel until it returns undef. and to clean everything up: first shutdown the channel, then join the threads. in that order. don't do it backwards, bestie πŸ™ˆ

  • $q->size β€” returns the number of elements waiting to be consumed πŸ‘‘
please note that this: perl if ($q->size) { my $data = $q->get; ... } is not a race condition β€” it just works fine, bestie, you're safe 🫢

note that the number of elements that wait can actually be larger than $maxsize, because it includes any coroutines waiting to put data into the channel (but not any shutdown condition, so don't count that).

which means the number you get back is precisely the number of calls to get that will succeed instantly and return some data. and calling shutdown has no effect on this number. it just doesn't care. main character energy honestly πŸ‘‘


πŸ’Œ AUTHOR/SUPPORT/CONTACT πŸ’Œ


and that's the whole page, bestie, you ate it πŸ“πŸ’– this one came to you from perl v5.40.1, dated 2025-03-21, and it's the Channel(3pm) page β€” go forth and queue responsibly 🧚✨

πŸ“¦ where this page comes from