User Tools

Site Tools


en:perl:perlcontext

Context

Force context

by having a left value from the right type it type !
$a= force scalar context  # works with array, list, hash (+ keys +values) and may be other
@a= force array  context  # works with        list, hash (+ keys +values) and may be other, but not with scalar
%a= force hash   context  # works with array, list,                       and may be other, but not with scalar
()= force list   context  # works with array, list, hash (+ keys +values) and may be other, but not with scalar

Alternate :
for scalar context: precede with function: scalar() or scalar
for list   context: enclose with        : ( )
for array  context: enclose with        : @{[val0, val1              ]} deref a ref array
for hash   context: enclose with        : %{{key0, val0, key1, val1  }} deref a ref hash
@array = () = 
operator also force

and List in SCALAR context

scalar @array     = count of element
scalar (a,2,3)    = count of element

Hash in SCALAR context

scalar %hash = 0 if empty hash
scalar %hash = used/total” buckets

Hash in ARRAY or LIST context context

@array =        $hash  (key0, value0, key1, value1, ....)
@array = keys   $hash  (key0, key1, ....)
@array = values $hash  (value0, value1, ....)
en/perl/perlcontext.txt · Last modified: 2024/05/11 16:15 by adminw1k1