raku.gg

[ BACK... ]
By: w1ldc4rd-w1z4rd

Word Quoting in Raku


Word quoting is a powerful feature in Raku that simplifies the creation of lists of strings. It’s a tool that can make your code more readable and easier to maintain. Let’s explore the various forms and their practical applications.

Basic Word Quoting: qw

The qw operator is the foundation of word quoting in Raku. It splits a string into words, creating a list:

my @tools = qw|hammer saw screwdriver wrench|;
say @tools; # OUTPUT: [hammer saw screwdriver wrench]

This is particularly useful when you need to create a list of related items without the clutter of quotes and commas.

Angle Bracket Shorthand: < >

Raku offers an even more concise syntax using angle brackets:

my @planets = <Mercury Venus Earth Mars>;
say @planets; # OUTPUT: [Mercury Venus Earth Mars]

This syntax is clean and easy to read, making it a favorite among Raku developers.

Angle Brackets and Allomorphs

The angle bracket syntax has an interesting feature: it can create allomorphs, which are values that behave as both numbers and strings:

say <42>.WHAT;     # OUTPUT: (IntStr)
say <3.14>.WHAT;   # OUTPUT: (RatStr)
say <1+2i>.WHAT;   # OUTPUT: (ComplexStr)

This can be particularly useful in scenarios where you need a value to have dual number-string behavior.

Quoting with Protection: qww and qqww

Sometimes you need to keep certain words together as a phrase. That’s where qww comes in handy:

my @book_titles = qww{"The Great Gatsby" "To Kill a Mockingbird"};
say @book_titles; # OUTPUT: [The Great Gatsby To Kill a Mockingbird]

And when you need to include variables in your quoted list, qqww is your friend:

my $author = "George Orwell";
my @books = qqww{$author "1984" "Animal Farm"};
say @books; # OUTPUT: [George Orwell 1984 Animal Farm]

The Elegant « » Quotes

For a touch of elegance (and to create allomorphs), Raku offers the « » quotes:

my $year = 2023;
say «$year was a year of innovation»;
# OUTPUT: [2023 was a year of innovation]

The ASCII equivalent << >> is also available if you prefer.

Practical Example: A Simple Inventory System

Let’s put these concepts to work in a more practical scenario. Imagine we’re creating a basic inventory system for a small electronics store:

my $brand = "TechPro";
my @inventory = qqww«
    "$brand Laptop"
    "$brand Smartphone"
    Headphones
    "Wireless Mouse"
    "4K Monitor"
    500GB "Solid State Drive"
»;

say "Current Inventory:";
.say for @inventory;

# Let's find items with a specific word
say "\nItems containing 'Wireless':";
say .grep(*.contains('Wireless'));

This will output:

Current Inventory:
TechPro Laptop
TechPro Smartphone
Headphones
Wireless Mouse
4K Monitor
500GB Solid State Drive

Items containing 'Wireless':
Wireless Mouse

In this example, we’ve used qqww to create a list that includes variables, multi-word items, and even a measurement. The angle quotes « » allow us to create allomorphs for the storage capacity.

Key Takeaways

  1. Use qw or < > for simple lists of words.
  2. qww is great for preserving phrases within your list.
  3. qqw and qqww allow variable interpolation in your quoted lists.
  4. « » (or << >>) create allomorphs and offer the most features.

Word quoting in Raku is more than just a convenience—it’s a versatile tool that can make your code cleaner and more expressive. By choosing the right quoting style for your needs, you can create more readable and maintainable code.

Remember, the goal is to write code that’s not just functional, but also clear and intuitive. Word quoting is one of those features that, once mastered, you’ll find yourself reaching for again and again in your Raku programming journey.

WWW: 🐪 perl.gg - 🧙‍♂️ clc.onl - 💾 GitHub - 🏙️ Neocities

Copyright ©️ 2024 raku.gg