Debian Codegraph @

lib/Dancer2/Manual/Keywords.pod

raw
1 # ABSTRACT: Dancer2 DSL Keyword Reference
2 package Dancer2::Manual::Keywords;
3
4 __END__
5
6 =pod
7
8 =encoding UTF-8
9
10 =head1 NAME
11
12 Dancer2::Manual::Keywords - Dancer2 DSL Keyword Reference
13
14 =head1 VERSION
15
16 version 2.2.2
17
18 =head1 Dancer2 Keyword Guide
19
20 This guide categorizes all Dancer2 keywords with descriptions, usage
21 guidance, and relationships with other keywords. Each entry links to the
22 relevant section in the L<Dancer2 Manual|Dancer2::Manual>.
23
24 =head1 Routing Keywords
25
26 =head2 L<any|Dancer2::Manual/"HTTP Methods: Visitor Actions">
27
28 Defines a route that responds to any HTTP method.
29
30 B<When to use>: Handle routes that accept multiple methods (e.g., C<GET>
31 and C<POST>).
32
33 B<Related Keywords>: All HTTP methods.
34
35 =head2 L<del|Dancer2::Manual/"HTTP Methods: Visitor Actions">
36
37 Defines a route that responds to HTTP C<DELETE> requests.
38
39 B<When to use>: Delete resources.
40
41 B<Related Keywords>: L</get>, L</post>, L</put>, L</patch>, L</options>, L</any>
42
43 =head2 L<get|Dancer2::Manual/get>
44
45 Defines a route that responds to HTTP C<GET> requests. Automatically
46 creates a C<HEAD> route.
47
48 B<When to use>: Fetch static resources or perform read-only actions.
49
50 B<Related Keywords>: L</post>, L</put>, L</del>, L</patch>, L</options>, L</any>
51
52 =head2 L<options|Dancer2::Manual/"HTTP Methods: Visitor Actions">
53
54 Defines a route that responds to HTTP C<OPTIONS> requests.
55
56 B<When to use>: Check available HTTP methods for a resource.
57
58 B<Related Keywords>: All HTTP methods.
59
60 =head2 L<patch|Dancer2::Manual/"HTTP Methods: Visitor Actions">
61
62 Defines a route that responds to HTTP C<PATCH> requests, often for partial
63 updates.
64
65 B<When to use>: Partially update existing resources.
66
67 B<Related Keywords>: L</get>, L</post>, L</put>, L</del>, L</options>, L</any>
68
69 =head2 L<post|Dancer2::Manual/post>
70
71 Defines a route that responds to HTTP C<POST> requests, typically for form
72 submissions or resource creation.
73
74 B<When to use>: Submit forms, upload files, or create resources.
75
76 B<Related Keywords>: L</get>, L</put>, L</del>, L</patch>, L</options>, L</any>
77
78 =head2 L<prefix|Dancer2::Manual/"Organizing routes and growing your app using prefix">
79
80 Defines a common path prefix for grouped routes.
81
82 B<When to use>: Organize routes logically under a shared prefix.
83
84 B<Related Keywords>: All HTTP methods.
85
86 =head2 L<put|Dancer2::Manual/"HTTP Methods: Visitor Actions">
87
88 Defines a route that responds to HTTP C<PUT> requests, often for updating
89 resources.
90
91 B<When to use>: Update existing resources in an idempotent way.
92
93 B<Related Keywords>: L</get>, L</post>, L</del>, L</patch>, L</options>, L</any>
94
95 =head2 L<splat|Dancer2::Manual/"Wildcard Route Parameters">
96
97 Retrieves a list value captured by a wildcard parameter in routes.
98
99 B<When to use>: Use with wildcard routes to capture arbitrary path segments.
100
101 B<Related Keywords>: L</captures>
102
103 =head1 URL and Route Helper Keywords
104
105 =head2 L<uri_for|Dancer2::Manual/uri_for>
106
107 Constructs a URI for a given path and query parameters.
108
109 B<When to use>: Generate URLs for links and redirects.
110
111 B<Related Keywords>: L</uri_for_route>
112
113 =head2 L<uri_for_route|Dancer2::Manual/uri_for_route>
114
115 Constructs a URI for a named route.
116
117 B<When to use>: Generate URLs based on route names and parameters.
118
119 B<Best Practice>: Use L</uri_for_route> with named routes to ensure
120 consistency even if route paths change.
121
122 B<Related Keywords>: L</uri_for>
123
124 =head1 Request Handling Keywords
125
126 =head2 L<body_parameters|Dancer2::Manual/body_parameters>
127
128 Retrieves parameters specifically from the request body.
129
130 B<When to use>: Handle C<POST> or C<PUT> data.
131
132 B<Related Keywords>: L</query_parameters>, L</route_parameters>, L</params>.
133
134 =head2 L<captures|Dancer2::Manual/"Regex Route Matching">
135
136 Accesses all regex capture groups from a route pattern.
137
138 B<When to use>: Extract parts of a URL matched by a regex route.
139
140 B<Related Keywords>: L</splat>
141
142 =head2 context
143
144 Deprecated. Use L</app> instead.
145
146 B<See also>: L</app>
147
148 =head2 L<param|Dancer2::Manual/"param and params (Not Recommended)">
149
150 Retrieves a parameter from the request (query, body, or route parameters).
151
152 B<When to use>: Not recommended. Use L</query_parameters>, L</body_parameters>,
153 or L</route_parameters> for clarity.
154
155 B<Best Practice>: Avoid using L</param> in new code to prevent confusion
156 between parameter sources. Instead, use more specific keywords.
157
158 B<Related Keywords>: L</params>, L</query_parameters>, L</body_parameters>, L</route_parameters>
159
160 =head2 L<params|Dancer2::Manual/"param and params (Not Recommended)">
161
162 Returns all parameters from the request as a hash reference.
163
164 B<When to use>: Retrieve all request parameters at once.
165
166 B<Best Practice>: Prefer specific keywords like L</query_parameters> for
167 clarity in code.
168
169 B<Related Keywords>: L</param>, L</query_parameters>, L</body_parameters>, L</route_parameters>
170
171 =head2 L<query_parameters|Dancer2::Manual/query_parameters>
172
173 Retrieves parameters specifically from the query string.
174
175 B<When to use>: Handle parameters in the query string (e.g., for searches).
176
177 B<Related Keywords>: L</body_parameters>, L</route_parameters>, L</params>
178
179 =head2 L<request|Dancer2::Core::Request>
180
181 Provides access to the current HTTP request object.
182
183 B<When to use>: Get details about the incoming request (headers, method,
184 path).
185
186 B<Related Keywords>: L</response>, L</params>, L</header>
187
188 =head2 request_data
189
190 Retrieves the raw data from the request body. If a serializer is set,
191 data will be automatically deserialized.
192
193 B<When to use>: Access the unprocessed body content of the request, especially
194 when dealing with custom or non-standard payloads.
195
196 B<Related Keywords>: L</request>, L</body_parameters>, L</params>
197
198 =head2 L<request_header|Dancer2::Manual/request_header>
199
200 Fetches the value of a specific header from the incoming request.
201
202 B<When to use>: Obtain information from request headers, such as 'User-Agent'
203 or 'Authorization'.
204
205 B<Related Keywords>: L</headers>, L</response_header>
206
207 =head2 L<route_parameters|Dancer2::Manual/route_parameters>
208
209 Retrieves parameters from the route pattern.
210
211 B<When to use>: Handle named or wildcard route parameters.
212
213 B<Related Keywords>: L</query_parameters>, L</body_parameters>, L</params>
214
215 =head2 L<upload|Dancer2::Manual/upload>
216
217 Uploads a file and makes it accessible as a L<Dancer2::Core::Request::Upload>
218 object.
219
220 If you named multiple files with the same name, C<upload> keyword returns
221 an array of L<Dancer2::Core::Request::Upload> objects:
222
223 post '/some/route' => sub {
224 my ($file1, $file2) = upload('files_input');
225 # $file1 and $file2 are Dancer2::Core::Request::Upload objects
226 };
227
228 You can also access the raw hashref of parsed uploads via the current request object:
229
230 post '/some/route' => sub {
231 my $all_uploads = request->uploads;
232 # $all_uploads->{'file_input_foo'} is a Dancer2::Core::Request::Upload object
233 # $all_uploads->{'files_input'} is an arrayref of Dancer2::Core::Request::Upload objects
234 };
235
236 B<When to use>: To handle file uploads from client requests.
237
238 B<Related Keywords>: L</uploads>, L</request>
239
240 =head1 Response Handling Keywords
241
242 =head2 L<content|Dancer2::Manual/"content">
243
244 Sets the content of the response. This B<only> works within a delayed
245 response.
246
247 This will crash:
248
249 get '/' => sub {
250 content 'Hello, world!';
251 };
252
253 But this will work just fine:
254
255 get '/' => sub {
256 delayed {
257 content 'Hello, world!';
258 };
259 };
260
261 B<When to use>: Directly set content of an asynchronous response.
262
263 B<Related Keywords>: L</response>
264
265 =head2 L<content_type|Dancer2::Manual/"Manually Adjusting MIME Settings">
266
267 Sets the C<Content-Type> header of the response.
268
269 B<When to use>: Specify the MIME type of the response.
270
271 B<Related Keywords>: L</header>
272
273 =head2 L<decode_json|Dancer2::Manual/decode_json>
274
275 Deserializes a JSON string to a Perl data structure. Does not trigger
276 serialization hooks.
277
278 B<When to use>: When you need to translate incoming JSON to a Perl hashref.
279
280 B<Related Keywords>: L</encode_json>, L</from_json>
281
282 =head2 L<delayed|Dancer2::Manual/delayed>
283
284 Initiates an asynchronous response. Requires a Plack/PSGI server capable
285 of serving requests asynchronously.
286
287 B<When to use>: When starting an asynchronous reponse.
288
289 B<Related Keywords>: L</done>, L</flush>
290
291 =head2 L<done|Dancer2::Manual/done>
292
293 Finalizes an asynchronous repsonse and closes the connection. Can only
294 be run within the streaming response callback.
295
296 B<When to use>: When finishing delivery of data asynchronously.
297
298 B<See also>: L</delayed>, L</flush>
299
300 =head2 L<encode_json|Dancer2::Manual/encode_json>
301
302 Serializes a data structure to a UTF-8 encoded binary JSON string. Calling
303 this keyword will not trigger the application's serializer hooks.
304
305 B<When to use>: When needing to translate a Perl data structure to JSON.
306
307 B<See also>: L</decode_json>, L</to_json>
308
309 =head2 L<flush|Dancer2::Manual/flush>
310
311 Flushes content headers when streaming a response. This is necessary when
312 C<content> is called multiple times.
313
314 B<When to use>: When streaming large amounts of data (such as video)
315 asynchronously.
316
317 B<See also>: L</delayed>, L</done>
318
319 =head2 L<forward|Dancer2::Manual/forward>
320
321 Forwards the request to another route handler.
322
323 B<When to use>: Internally redirect to another route within the app.
324
325 B<Related Keywords>: L</pass>
326
327 =head2 L<from_dumper|Dancer2::Manual/from_dumper>
328
329 Deserializes a L<Data::Dumper> structure.
330
331 B<When to use>: When you need to turn a L<Data::Dumper> structure into a
332 Perl hashref.
333
334 B<See also>: L</to_dumper>, L</from_json>
335
336 =head2 L<from_json|Dancer2::Manual/from_json>
337
338 Deserializes a JSON string into a Perl data structure. The input is expected
339 to be UTF-8 encoded bytes.
340
341 B<When to use>: Decode JSON data from the client. L</decode_json> is likely
342 what you want.
343
344 B<Related Keywords>: L</to_json>
345
346 =head2 L<from_yaml|Dancer2::Manual/from_yaml>
347
348 Deserializes a YAML structure.
349
350 B<When to use>: When you need to turn YAML into a Perl hashref.
351
352 B<See also>: L</to_yaml>, L</from_json>
353
354 =head2 L<halt|Dancer2::Manual/halt>
355
356 Halts request processing and sends a response immediately.
357
358 B<When to use>: Stop processing and return a response early.
359
360 B<Best Practice>: Combine L</halt> with L</status> to ensure appropriate
361 HTTP response codes are sent.
362
363 B<Related Keywords>: L</status>, L</send_error>
364
365 =head2 C<header>
366
367 Deprecated. Use L</response_header> instead.
368
369 B<Related Keywords>: L</response_header>.
370
371 =head2 C<headers>
372
373 Deprecated. Use L</response_headers> instead.
374
375 B<Related Keywords>: L</response_headers>.
376
377 =head2 L<pass|Dancer2::Manual/pass>
378
379 Passes control to the next matching route.
380
381 B<When to use>: Skip the current route and try the next one.
382
383 B<Related Keywords>: L</forward>
384
385 =head2 C<push_header>
386
387 Deprecated. Use L</push_response_header> instead.
388
389 B<When to use>: Don't. Use L</push_response_header>.
390
391 B<Related Keywords>: L</push_response_header>.
392
393 =head2 L<push_response_header|Dancer2::Manual/"Adding Additional Response Headers">
394
395 Adds a new value to an existing response header without replacing its current value.
396
397 B<When to use>: Append additional values to headers like 'Set-Cookie' or 'X-Forwarded-For'.
398
399 B<See also>: L</response_header>, L</response_headers>
400
401 =head2 L<redirect|Dancer2::Manual/redirect>
402
403 Redirects the client to a different location.
404
405 B<When to use>: Redirect users to another route or external URL.
406
407 B<Related Keywords>: L</status>
408
409 =head2 L<response|Dancer2::Core::Response>
410
411 Provides access to the current response object.
412
413 B<When to use>: Manipulate the outgoing response (headers, status, content).
414
415 B<Related Keywords>: L</request>, L</header>, L</content>
416
417 =head2 L<response_header|Dancer2::Manual/"Adding Additional Response Headers">
418
419 Sets or retrieves a specific header in the outgoing response.
420
421 B<When to use>: Modify or access response headers before sending them to the client.
422
423 B<Related Keywords>: L</header>, L</response_headers>
424
425 =head2 L<response_headers|Dancer2::Manual/"Adding Additional Response Headers">
426
427 Returns a hash reference of all headers set in the response.
428
429 B<When to use>: Inspect or manipulate multiple response headers simultaneously.
430
431 B<Related Keywords>: L</response_header>, L</header>
432
433 =head2 L<send_as|Dancer2::Manual/send_as>
434
435 Manually serializes and sends the response in a specific format.
436
437 B<When to use>: Control response serialization explicitly.
438
439 B<Related Keywords>: C<encode_json>, C<decode_json>.
440
441 =head2 L<send_error|Dancer2::Manual/send_error>
442
443 Immediately halts processing and sends an error response with a specific
444 status code.
445
446 B<When to use>: To send a custom error response.
447
448 B<Related Keywords>: L</halt>, L</status>
449
450 =head2 L<send_file|Dancer2::Manual/"Sending Static Files from a Route Handler">
451
452 Sends a file to the client.
453
454 B<When to use>: Send files from disk or in-memory data as a file.
455
456 B<Related Keywords>: L</send_as>
457
458 =head2 L<status|Dancer2::Manual/status>
459
460 Sets the HTTP status code for the response.
461
462 B<When to use>: Specify response codes (e.g., 200, 404).
463
464 B<Best Practice>: Combine L</status> with L</halt> for clearer error handling
465 when sending error responses.
466
467 B<Related Keywords>: L</halt>, L</send_error>
468
469 =head2 L<to_dumper|Dancer2::Manual/to_dumper>
470
471 Serializes a Perl data structure into a string using Data::Dumper.
472
473 B<When to use>: Convert complex data structures into a readable string format for debugging.
474
475 B<Related Keywords>: L</from_dumper>, L</to_yaml>, L</to_json>
476
477 =head2 L<to_json|Dancer2::Manual/to_json>
478
479 Serializes a Perl data structure to JSON. The input should be character
480 strings. The output is UTF-8 encoded bytes.
481
482 B<When to use>: Encode data to JSON format.
483
484 B<Related Keywords>: L</from_json>, L</send_as>
485
486 =head2 L<to_yaml|Dancer2::Manua/#to_yaml>
487
488 Serializes a Perl data structure into a YAML string.
489
490 B<When to use>: Convert data structures into YAML format.
491
492 B<Related Keywords>: L</from_yaml>, L</to_json>, L</to_dumper>
493
494 =head1 Session and Cookie Keywords
495
496 =head2 L<cookie|Dancer2::Manual/"Setting and Updating Cookies">
497
498 Retrieves the value of a specific cookie from the request.
499
500 B<When to use>: Access individual cookies.
501
502 B<Related Keywords>: L</cookies>, L</session>
503
504 =head2 L<cookies|Dancer2::Manual/"Retrieving Cookies">
505
506 Retrieves all cookies from the request as a hash reference.
507
508 B<When to use>: Inspect or manage multiple cookies.
509
510 B<Related Keywords>: L</cookie>
511
512 =head2 L<session|Dancer2::Manual/session>
513
514 Stores, retrieves, or removes data from the session.
515
516 B<When to use>: Persist user-specific data between requests.
517
518 B<Related Keywords>: L</cookie>, L</cookies>
519
520 =head1 Application Configuration Keywords
521
522 =head2 L<app|Dancer2::Manual/"Accessing the Application Object">
523
524 Returns an instance of the app. App is a L<Dancer2::Core::App>.
525
526 B<When to use>: When calling methods only available via the application object.
527
528 B<See also>: L</dancer_app>
529
530 =head2 L<config|Dancer2::Manual/config>
531
532 Retrieves the entire application configuration as a hash reference.
533
534 B<When to use>: Access or inspect all configuration settings.
535
536 B<Related Keywords>: L</set>, L</setting>
537
538 =head2 C<dancer_app>
539
540 Alias for L</app>.
541
542 B<Best Practice>: Use L</app> instead.
543
544 B<Related Keywords>: L</app>
545
546 =head2 L<dancer_version|Dancer2::Manual/dancer_version>
547
548 Retrieve full version number of installed Dancer2.
549
550 B<When to use>: To help manage compatability and feature usage in your
551 applications.
552
553 B<See also>: L</dancer_major_version>
554
555 =head2 L<dancer_major_version|Dancer2::Manual/dancer_major_version>
556
557 Retrieves major version number of Dancer2.
558
559 B<When to use>: To help manage compatability and feature usage in your
560 applications.
561
562 B<See also>: L</dancer_version>
563
564 =head2 L<prepare_app|Dancer2::Manual/prepare_app>
565
566 Function to help initialize and configure your Dancer2 app.
567
568 B<When to use>: When you need to run initialization or setup code once at
569 the start of your application.
570
571 B<See also>: L</to_app>
572
573 =head2 C<runner>
574
575 Provides access to the Dancer2 runner object, which manages the application's execution.
576
577 B<When to use>: Control or inspect the application's runtime environment.
578
579 B<Related Keywords>: L</app>, L</config>
580
581 =head2 L<set|Dancer2::Manual/set>
582
583 Sets a configuration value for the application.
584
585 B<When to use>: Change configuration dynamically at runtime.
586
587 B<Related Keywords>: L</setting>, L</config>
588
589 =head2 L<setting|Dancer2::Manual/setting>
590
591 Retrieves a configuration value.
592
593 B<When to use>: Access app configuration values.
594
595 B<Related Keywords>: L</set>, L</config>
596
597 =head2 C<start>
598
599 Initiates the Dancer2 application; typically called internally.
600
601 B<When to use>: Rarely used directly; see L</to_app> instead.
602
603 B<Related Keywords>: L</dance>, L</psgi_app>
604
605 =head2 L<var|Dancer2::Manual/var>
606
607 Sets a variable in the application context.
608
609 B<When to use>: Store temporary data accessible in routes and templates.
610
611 B<Related Keywords>: L</vars>
612
613 =head2 L<vars|Dancer2::Manual/vars>
614
615 Retrieves the application context variables.
616
617 B<When to use>: Access data stored with L</var>.
618
619 B<Related Keywords>: L</var>
620
621 =head1 Hooks
622
623 =head2 L<hook|Dancer2::Manual/Hooks>
624
625 Declares a hook to run custom code at specific stages in the request/response lifecycle.
626
627 B<When to use>: Modify requests or responses, or perform actions at predefined lifecycle points.
628
629 B<Related Keywords>: L</var>, L</vars>
630
631 =head1 Template Keywords
632
633 =head2 L<template|Dancer2::Manual/template>
634
635 Renders a view with the provided data.
636
637 B<When to use>: Generate HTML or other formats dynamically.
638
639 B<Related Keywords>: L</vars>
640
641 =head1 Logging Keywords
642
643 =head2 L<debug|Dancer2::Manual/debug>
644
645 Logs a message at the debug level.
646
647 B<When to use>: Detailed debugging and diagnostic messages.
648
649 B<Best Practice>: Use appropriate logging levels to maintain clear and actionable logs.
650
651 B<Related Keywords>: L</info>, L</warning>, L</error>, L</log>
652
653 =head2 L<error|Dancer2::Manual/error>
654
655 Logs a message at the error level.
656
657 B<When to use>: Log errors that need immediate attention.
658
659 B<Best Practice>: Use appropriate logging levels to maintain clear and actionable logs.
660
661 B<Related Keywords>: L</debug>, L</info>, L</warning>, L</log>
662
663 =head2 L<info|Dancer2::Manual/info>
664
665 Logs a message at the info level.
666
667 B<When to use>: General informational messages.
668
669 B<Best Practice>: Use appropriate logging levels to maintain clear and actionable logs.
670
671 B<Related Keywords>: L</debug>, L</warning>, L</error>, L</log>
672
673 =head2 L<log|Dancer2::Manual/log>
674
675 Logs a message at a specified level.
676
677 B<When to use>: Log messages with custom or dynamic levels.
678
679 B<Related Keywords>: L</debug>, L</info>, L</warning>, L</error>
680
681 =head2 L<warning|Dancer2::Manual/warning>
682
683 Logs a message at the warning level.
684
685 B<When to use>: Logging non-critical issues that may need attention.
686
687 B<Best Practice>: Use appropriate logging levels to maintain clear and actionable logs.
688
689 B<Related Keywords>: L</debug>, L</info>, L</error>, L</log>
690
691 =head1 Miscellaneous Keywords
692
693 =head2 dance
694
695 Alias to the L<start> keyword.
696
697 B<Note>: This keyword has been superseded by L</to_app>.
698
699 B<Best Practice>: Use L</to_app> instead.
700
701 B<Related Keywords>: L</to_app>
702
703 =head2 dirname
704
705 Given a path, return the directory name.
706
707 B<When to use>: When you need to extract the directory name given a path.
708
709 B<See also>: L</path>
710
711 =head2 dsl
712
713 Provides access to the DSL (Domain Specific Language) object.
714
715 B<When to use>: When writing plugins or extending Dancer2's capabilities.
716
717 B<Related Keywords>: None.
718
719 =head2 engine
720
721 Given a namespace, return the current engine object. For example:
722
723 my $template_engine = engine 'template';
724
725 B<When to use>: When you need to manually interact with one of the
726 underlying engines in Dancer2.
727
728 B<See also>: L</template>, L</session>, L</logger>
729
730 =head2 C<false>
731
732 Returns a false value.
733
734 B<When to use>: Explicitly return a false value in your application.
735
736 B<Related Keywords>: L</true>
737
738 =head2 mime
739
740 Provide access to your application's instance of L<Dancer2::Core::MIME>.
741
742 B<When to use>: When querying or changing your application's MIME settings.
743
744 B<See also>: L</send_file>
745
746 =head2 path
747
748 Concatenates multiple paths together without worrying about the underlying
749 operating system. It cleans the path aesthetically, but does not verify its
750 existence.
751
752 B<When to use>: When you need to generate a filesystem path within your
753 application.
754
755 B<See also>: L</dirname>
756
757 =head2 psgi_app
758
759 Provides the same functionality as L</to_app>, but uses the deprecated
760 Dispatcher engine.
761
762 B<Note>: This keyword has been superseded by L</to_app>.
763
764 B<Best Practice>: Use L</to_app> instead.
765
766 B<See also>: L<to_app|#to_app>.
767
768 =head2 L<to_app|Dancer2::Manual/to_app>
769
770 Returns the PSGI application code reference for the current Dancer2 app.
771
772 B<When to use>: Integrate the Dancer2 application with other PSGI-compatible frameworks or servers.
773
774 B<Related Keywords>: L</psgi_app>, L</dance>
775
776 =head2 C<true>
777
778 Returns a true value.
779
780 B<When to use>: Explicitly return a true value in your application.
781
782 B<Related Keywords>: L</false>
783
784 =head1 AUTHOR
785
786 Dancer Core Developers
787
788 =head1 COPYRIGHT AND LICENSE
789
790 This software is copyright (c) 2026 by Alexis Sukrieh.
791
792 This is free software; you can redistribute it and/or modify it under
793 the same terms as the Perl 5 programming language system itself.
794
795 =cut