Movatterモバイル変換


[0]ホーム

URL:


functions /values
(source,CPAN)
You are viewing the version of this documentation from Perl 5.16.1.View the latest version
#values HASH
#values ARRAY
#values EXPR

In list context, returns a list consisting of all the values of the named hash. In Perl 5.12 or later only, will also return a list of the values of an array; prior to that release, attempting to use an array argument will produce a syntax error. In scalar context, returns the number of values.

When called on a hash, the values are returned in an apparently random order. The actual random order is subject to change in future versions of Perl, but it is guaranteed to be the same order as either thekeys oreach function would produce on the same (unmodified) hash. Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons (see"Algorithmic Complexity Attacks" in perlsec).

As a side effect, calling values() resets the HASH or ARRAY's internal iterator, see"each". (In particular, calling values() in void context resets the iterator with no other overhead. Apart from resetting the iterator,values @array in list context is the same as plain@array. (We recommend that you use void contextkeys @array for this, but reasoned that takingvalues @array out would require more documentation than leaving it in.)

Note that the values are not copied, which means modifying them will modify the contents of the hash:

for (values %hash)      { s/foo/bar/g }   # modifies %hash valuesfor (@hash{keys %hash}) { s/foo/bar/g }   # same

Starting with Perl 5.14,values can take a scalar EXPR, which must hold a reference to an unblessed hash or array. The argument will be dereferenced automatically. This aspect ofvalues is considered highly experimental. The exact behaviour may change in a future version of Perl.

for (values $hashref) { ... }for (values $obj->get_arrayref) { ... }

To avoid confusing would-be users of your code who are running earlier versions of Perl with mysterious syntax errors, put this sort of thing at the top of your file to signal that your code will workonly on Perls of a recent vintage:

use 5.012;# so keys/values/each work on arraysuse 5.014;# so keys/values/each work on scalars (experimental)

See alsokeys,each, andsort.

Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.

The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.


[8]ページ先頭

©2009-2025 Movatter.jp