The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

WWW::Kickstarter::Data::Categories - Kickstarter categories

SYNOPSIS

   use WWW::Kickstarter;

   my $email    = '...';  # Your Kickstarter login credentials
   my $password = '...';

   my $ks = WWW::Kickstarter->new();
   $ks->login($email, $password);

   my $categories = $ks->categories();

   $categories->visit(sub{
      my ($category, $depth, $visit_next) = @_;
      say "   " x $depth, $category->name;
      1 while $visit_next->();
   });

ACCESSORS

my @categories = $categories->categories;

Returns a WWW::Kickstarter::Data::Category object for each Kickstarter category.

my @categories = $categories->top_level_categories;

Returns a WWW::Kickstarter::Data::Category object for each top-level Kickstarter category.

API CALLS

refetch

   $categories = $categories->refetch();

Refetches the categories from Kickstarter.

METHODS

visit

   sub visitor {
      my ($category, $depth, $visit_next, $num_subcategories, @args) = @_;
      ...
   }

   $categories->visit(\&visitor, @args);

   $categories->visit({ visitor => \&visitor, %opts }, @args);

Traverses the category hiearchy in a depth-first, alphabetical manner.

The visitor is called with the following arguments:

  • $category

    A category as an WWW::Kickstarter::Data::Category object.

  • $depth

    The depth of the category in the hierarchy, zero for top-level categories.

  • $visit_next

    A code reference that visits one subcategory each time it's called. Unless you want to avoid visiting a category's subcategories, it should be called until it returns false.

  • $num_subcategories

    The number of subcategories this category has. The following are basically equivalent:

    • 1 while $visit_next->(); =item * $visit_next->() for 1..$num_subcategories;

  • @args

    The values passed to visit or &$visit_next.

Options:

  • root => 1

    The visitor will be called one extra time for the root of the tree. $category will be undefined in the visitor for this call. The root will have a depth of zero, so the top-level categories will have a depth of one.

Examples

  • Simple Example

       $categories->visit(sub{
          my ($category, $depth, $visit_next) = @_;
          say "   " x $depth, $category->name;
          1 while $visit_next->();
       });

    Output:

       Art
          Crafts
          Digital Art
          ...
          Sculpture
       Comics
       Dance
       Design
          Graphic Design
          Product Design
       ...
  • Passing data down to subcategories.

       $categories->visit(sub{
          my ($category, $depth, $visit_next, undef, $parent) = @_;
          say $parent . $category->name;
          1 while $visit_next->($parent . $category->name . '/');
       }, '');

    Output:

       Art
       Art/Crafts
       Art/Digital Art
       ...
       Art/Sculpture
       Comics
       Dance
       Design
       Design/Graphic Design
       Design/Product Design
       ...
  • Complex example

       $categories->visit({
          root    => 1,
          visitor => sub{
             my ($category, $depth, $visit_next, $num_subcategories, $subcategory_idx) = @_;
    
             if ($category) {
                my $class = $subcategory_idx % 2 ? 'odd' : 'even';
                print qq{<li class="$class">} . $category->name;
             }
    
             if ($num_subcategories) {
                say "<ul>";
                for my $subcategory_idx (1..$num_subcategories) {
                   $visit_next->($subcategory_idx);
                }
                say "</ul>";
             }
    
             if ($category) {
                say "</li>"
             }
          },
       });

    Output:

       <ul>
       <li class="odd">Art<ul>
       <li class="odd">Crafts</li>
       <li class="even">Digital Art</li>
       ...
       <li class="even">Sculpture</li>
       </ul>
       </li>
       <li class="even">Comics</li>
       <li class="odd">Dance</li>
       <li class="even">Design<ul>
       <li class="odd">Graphic Design</li>
       <li class="even">Product Design</li>
       </ul>
       </li>
       ...
       </ul>

VERSION, BUGS, KNOWN ISSUES, SUPPORT, AUTHORS, COPYRIGHT & LICENSE

See WWW::Kickstarter