diff options
author | David Golden <dagolden@cpan.org> | 2010-09-09 17:22:02 -0400 |
---|---|---|
committer | David Golden <dagolden@cpan.org> | 2010-10-31 21:16:21 -0400 |
commit | cba5a3b05660d6a40525beb667a389a690900298 (patch) | |
tree | 4cb5d682634ed416c8b77adb57765035314d1103 /proto.h | |
parent | f64c9ac53bc4a5fa5967c92e98d7b42cca1ce97b (diff) | |
download | perl-cba5a3b05660d6a40525beb667a389a690900298.tar.gz |
Allow push/pop/keys/etc to act on references
All built-in functions that operate directly on array or hash
containers now also accept hard references to arrays or hashes:
|----------------------------+---------------------------|
| Traditional syntax | Terse syntax |
|----------------------------+---------------------------|
| push @$arrayref, @stuff | push $arrayref, @stuff |
| unshift @$arrayref, @stuff | unshift $arrayref, @stuff |
| pop @$arrayref | pop $arrayref |
| shift @$arrayref | shift $arrayref |
| splice @$arrayref, 0, 2 | splice $arrayref, 0, 2 |
| keys %$hashref | keys $hashref |
| keys @$arrayref | keys $arrayref |
| values %$hashref | values $hashref |
| values @$arrayref | values $arrayref |
| ($k,$v) = each %$hashref | ($k,$v) = each $hashref |
| ($k,$v) = each @$arrayref | ($k,$v) = each $arrayref |
|----------------------------+---------------------------|
This allows these built-in functions to act on long dereferencing
chains or on the return value of subroutines without needing to wrap
them in C<@{}> or C<%{}>:
push @{$obj->tags}, $new_tag; # old way
push $obj->tags, $new_tag; # new way
for ( keys %{$hoh->{genres}{artists}} ) {...} # old way
for ( keys $hoh->{genres}{artists} ) {...} # new way
For C<push>, C<unshift> and C<splice>, the reference will auto-vivify
if it is not defined, just as if it were wrapped with C<@{}>.
Calling C<keys> or C<values> directly on a reference gives a
substantial performance improvement over explicit dereferencing.
For C<keys>, C<values>, C<each>, when overloaded dereferencing is
present, the overloaded dereference is used instead of dereferencing
the underlying reftype. Warnings are issued about assumptions made in
the following three ambiguous cases:
(a) If both %{} and @{} overloading exists, %{} is used
(b) If %{} overloading exists on a blessed arrayref, %{} is used
(c) If @{} overloading exists on a blessed hashref, @{} is used
Diffstat (limited to 'proto.h')
-rw-r--r-- | proto.h | 9 |
1 files changed, 9 insertions, 0 deletions
@@ -407,6 +407,12 @@ PERL_CALLCONV OP * Perl_ck_open(pTHX_ OP *o) #define PERL_ARGS_ASSERT_CK_OPEN \ assert(o) +PERL_CALLCONV OP * Perl_ck_push(pTHX_ OP *o) + __attribute__warn_unused_result__ + __attribute__nonnull__(pTHX_1); +#define PERL_ARGS_ASSERT_CK_PUSH \ + assert(o) + PERL_CALLCONV OP * Perl_ck_readline(pTHX_ OP *o) __attribute__warn_unused_result__ __attribute__nonnull__(pTHX_1); @@ -3073,6 +3079,7 @@ PERL_CALLCONV OP * Perl_pp_quotemeta(pTHX); PERL_CALLCONV OP * Perl_pp_rand(pTHX); PERL_CALLCONV OP * Perl_pp_range(pTHX); PERL_CALLCONV OP * Perl_pp_rcatline(pTHX); +PERL_CALLCONV OP * Perl_pp_reach(pTHX); PERL_CALLCONV OP * Perl_pp_read(pTHX); PERL_CALLCONV OP * Perl_pp_readdir(pTHX); PERL_CALLCONV OP * Perl_pp_readline(pTHX); @@ -3093,12 +3100,14 @@ PERL_CALLCONV OP * Perl_pp_reverse(pTHX); PERL_CALLCONV OP * Perl_pp_rewinddir(pTHX); PERL_CALLCONV OP * Perl_pp_right_shift(pTHX); PERL_CALLCONV OP * Perl_pp_rindex(pTHX); +PERL_CALLCONV OP * Perl_pp_rkeys(pTHX); PERL_CALLCONV OP * Perl_pp_rmdir(pTHX); PERL_CALLCONV OP * Perl_pp_rv2av(pTHX); PERL_CALLCONV OP * Perl_pp_rv2cv(pTHX); PERL_CALLCONV OP * Perl_pp_rv2gv(pTHX); PERL_CALLCONV OP * Perl_pp_rv2hv(pTHX); PERL_CALLCONV OP * Perl_pp_rv2sv(pTHX); +PERL_CALLCONV OP * Perl_pp_rvalues(pTHX); PERL_CALLCONV OP * Perl_pp_sassign(pTHX); PERL_CALLCONV OP * Perl_pp_say(pTHX); PERL_CALLCONV OP * Perl_pp_scalar(pTHX); |