Tie::Cycle(3)

πŸ’« Tie::Cycle(3) πŸ’«

okay so bestie, you know how the man page header says Tie::Cycle(3) from the User Contributed Perl Documentation? yeah. that's this. it's the Tie::Cycle module and it's literally about to make your whole scalar situation iconic ✨


🌸 NAME 🌸

Tie::Cycle β€” cycle through a list of values via a scalar. that's it. that's the whole vibe. no cap πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

like, watch this:

use v5.10;
use Tie::Cycle;

tie my $cycle, 'Tie::Cycle', [ qw( FFFFFF 000000 FFFF00 ) ];

say $cycle; # FFFFFF
say $cycle; # 000000
say $cycle; # FFFF00
say $cycle; # FFFFFF  back to the beginning

(tied $cycle)->reset;  # back to the beginning

see how it just goes and goes and goes? FFFFFF, 000000, FFFF00, and then boom β€” back to FFFFFF. it's giving carousel. it's giving main character energy. periodt πŸ’…


πŸ’– DESCRIPTION πŸ’–

so here's the tea, bestie: you use Tie::Cycle to go through a list over and over again. once you hit the end of the list, you go right back to the beginning. and the way you don't even have to worry about any of it? that's the magic of tie doing it all for you. literally iconic 🧚

the tie takes an array reference as its third argument. the tie should succeed unless the argument is not an array reference. and here's a little lore drop: previous versions required you to use an array that had more than one element (like... what's the point of looping otherwise? 😭), but that restriction has been removed, because the number of elements you want to use may change depending on the situation. slay for flexibility πŸ’œ

⚠️ okay but this part is important, don't skip it: during the tie, this module makes a shallow copy of the array reference. so if the array reference contains references, and those references are changed after the tie, the elements of the cycle will change as well. πŸ’€ see the included test.pl script for an example of this effect. fr fr, go look at it.

πŸ‘‘ OBJECT METHODS πŸ‘‘

you can call methods on the underlying object β€” which you access with tied(), iykyk 🫢

🌈 reset

roll the iterator all the way back to the starting position. the next access will give you the first element in the list. fresh start, clean slate, new era ✨

πŸ“ previous

give the previous element. and no, this does not affect the current position. it's just a little peek behind you, bestie πŸ™ˆ

⭐ next

give the next element. this also does not affect the current position. so yes, you can absolutely peek at the next element if you like. no consequences, no drama πŸ’…


πŸ”₯ SOURCE AVAILABILITY πŸ”₯

this module is on Github, go be nosy:

https://github.com/briandfoy/tie-cycle

πŸŽ€ AUTHOR πŸŽ€

brian d foy, "<briandfoy@pobox.com>" β€” the legend themselves πŸ’–


Copyright © 2000-2025, brian d foy "<briandfoy@pobox.com>". All rights reserved. this software is available under the Artistic License 2.0. read it, respect it, love it 🫢


πŸ¦‹ and that's the whole page bestie!! this one's from perl v5.32.1, dated 2026-01-17, and it's the Tie::Cycle(3) man page β€” now go loop some values, you absolute icon πŸŒΈπŸ’…βœ¨
πŸ“¦ where this page comes from