@@ -5,9 +5,9 @@ use warnings;
5
5
use Exporter qw/ import/ ;
6
6
use Carp qw/ carp croak/ ;
7
7
8
- our $VERSION = q{ 1.05 } ;
9
- our @EXPORT = qw( dispatch on cases xdefault) ;
10
- our @EXPORT_OK = qw( dispatch on cases xdefault) ;
8
+ our $VERSION = q{ 1.06 } ;
9
+ our @EXPORT = qw( dispatch on cases xdefault xshift_and_deref ) ;
10
+ our @EXPORT_OK = qw( dispatch on cases xdefault xshift_and_deref ) ;
11
11
12
12
my $DISPATCH_TABLE = {};
13
13
@@ -74,7 +74,14 @@ sub xdefault($;$) {
74
74
if ($case and grep { / $case / } (cases)){
75
75
return $case ;
76
76
}
77
- return $default // q{ default} ;
77
+ return (defined $default ) ? $default : q{ default} ;
78
+ }
79
+
80
+ # for multi-assignment syntax, given the first reference in the parameter list; e.g., "my ($x, $y, $z) = ..."
81
+ sub xshift_and_deref (@) {
82
+ return %{ +shift } if ref $_ [0] eq q{ HASH} ;
83
+ return @{ +shift } if ref $_ [0] eq q{ ARRAY} ;
84
+ return shift @_ if ref $_ [0] eq q{ SCALAR} ;
78
85
}
79
86
80
87
# utility sub to force a BLOCK into a sub reference
@@ -86,6 +93,8 @@ sub _to_sub (&) {
86
93
87
94
__END__
88
95
96
+ =pod
97
+
89
98
=head1 NAME
90
99
91
100
Dispatch::Fu - Converts any complicated conditional dispatch situation into familiar static hash-key based dispatch
@@ -343,7 +352,7 @@ the way to pass arbitrary data into C<dispatch>. E.g.,
343
352
...
344
353
return $key;
345
354
346
- } $INPUT, # # <><~ the single scalar reference to be passed to the C<dispatch > BLOCK
355
+ } $INPUT, # <><~ the single scalar reference to be passed to the C<dispatch > BLOCK
347
356
...
348
357
349
358
=item C<on >
@@ -368,8 +377,8 @@ BLOCK must return strictly only the keys that are defined via C<on>.
368
377
on case4 => sub { my $INPUT = shift; ... },
369
378
on case5 => sub { my $INPUT = shift; ... };
370
379
371
- Note: when the subroutine associated with each I<case > is dispatched, the C< $INPUT > scalar is provide
372
- as input.
380
+ Note: when the subroutine associated with each I<case > is dispatched, the
381
+ C< $INPUT > scalar is provide as input.
373
382
374
383
my $INPUT = [qw/foo bar baz 1 3 4 5/];
375
384
@@ -380,7 +389,7 @@ as input.
380
389
...
381
390
return $key;
382
391
383
- } $INPUT, # <~ the single scalar reference to be passed to the C<dispatch > BLOCK
392
+ } $INPUT, # <~ the single scalar reference to be passed to the C<dispatch > BLOCK
384
393
on default => sub {
385
394
my $INPUT = shift;
386
395
do_default($INPUT);
@@ -394,6 +403,37 @@ as input.
394
403
do_key2(qw/some other inputs entirely/);
395
404
};
396
405
406
+ =item C<xshift_and_deref > ARRAY
407
+
408
+ Used within C<dispatch > and static key handlers defined by C<on > to provide a
409
+ single statement for C<shift @_ > , then an immediate I<dereferencing > of the
410
+ C<SCALAR > reference based on it's reference I<type > based on the results of
411
+ C<CORE::ref > (or just, C<ref > . E.g.,
412
+
413
+ my ($thing1, $thing2, $thing3) = xshift_and_deref @_;
414
+
415
+ And as part of a mostly complete C<dispatch > block,
416
+
417
+ dispatch {
418
+ my ($thing1, $thing2, $thing3) = xshift_and_deref @_; # <~ HERE
419
+ ...
420
+ return q{do_dis} if ...;
421
+ return q{do_dat};
422
+ } [ qw/thing1 thing2 thing3/ ],
423
+ on do_dis => sub {
424
+ my ($thing1, $thing2, $thing3) = xshift_and_deref @_; # <~ HERE
425
+ ...
426
+ },
427
+ on do_dat => sub {
428
+ my ($thing1, $thing2, $thing3) = xshift_and_deref @_; # <~ HERE
429
+ ...
430
+ };
431
+
432
+ This makes dealing with C<REF > s passed into C<dispatch > (and additinally into
433
+ the static key handler) very convenient. It eliminates potentally many lines
434
+ of boilerplate code that is meant simply for getting the contents of C<$_[0] >
435
+ into a set of explicit variables inside of C<dispatch > .
436
+
397
437
=back
398
438
399
439
=head3 Diagnostics and Warnings
@@ -434,8 +474,8 @@ the misplaced semicolon below:
434
474
This module will also throw an exeption (via C<croak > ) if C<dispatch > is
435
475
defined, but there are no C<on > statements. This covers the situation where
436
476
a semicolon has also snuck in prematurely; E.g., the following examples will
437
- die because due to lack of C<on > cases before C<on > warns that it's being used
438
- in a useless context:
477
+ die because due to lack of C<on > cases before C<on > warns that it's being
478
+ used in a useless context:
439
479
440
480
my $result = dispatch {
441
481
@@ -455,3 +495,5 @@ O. ODLER 558 L<< <oodler@cpan.org> >>.
455
495
=head1 LICENSE AND COPYRIGHT
456
496
457
497
Same as Perl.
498
+
499
+ =cut
0 commit comments