Next:
Text::Wigwam::Library::Debug
Previous:
Text::Wigwam::Library::Conditionals
 [Table of Contents][Text::Wigwam Index]

Text::Wigwam::Library::Datatype



NAME

Text::Wigwam::Library::Datatype #array, #defined, #hash, #is_array, #is_blessed, #is_code_ref, #is_expr, #is_hash, #is_list, #is_num, #is_string, #is_scalar, #ref, #scalar

Description

This library provides directives which are useful for detecting and casting data types.

Usage

The directives within this library can be imported into specific branches of the directive tree (which transcends from Text::Wigwam::Directives) via plug-ins or modules using one of the following techniques.

 package Text::Wigwam::Directives::some::arbitrary::branch;
 # import everything
 use Text::Wigwam::Library::Datatype qw/:all/;

 package Text::Wigwam::Directives::another::arbitrary::branch;
 # import selectively
 use Text::Wigwam::Library::Datatype( qw/
  :scalar :array :hash :ref :defined :is_blessed :is_num
  :is_scalar :is_array :is_list :is_hash :is_expr :is_string :is_code_ref
 / );

Directives

#array arg

Casts arg into an array.

 [!!
  #define arr #array "foo"
  arr:0 /* foo */
 !!]

The #array directive is typically aliased to the @ character, so the previous example can also be written like so:

 [!!
  #define arr @ "foo"
  arr:0 /* foo */
 !!]

#defined var

Returns true if var contains an assigned value, even if it is a null string.

 [!!
  #define array ( "" )
  #define array:8 ""
  #for { #define x 0 } { #lt x array } { #inc x } {
    #if #defined array:[x] { 'element ' x ' is defined\n' }
  }
  /*  output:
 element 0 is defined
 element 8 is defined
 */
 !!]

#hash arg

The #hash directive casts arg into a hash. It is typically used to cast an array into a hash which makes it possible to define complex structures within templates.

 [!!
  #define complex #hash (
     "key1" "value"
     "key2" "value"
     "key3" "value"
     "nest" #hash (
         "key1" "value"
         "key2" "value"
     )
  )
 !!]

Perl equivalent:

 $varspace->{complex} = {
     key1 => "value",
     key2 => "value",
     key3 => "value",
     nest => {
         key1 => "value",
         key2 => "value"
     }
 }

The #hash directive is typically aliased to the % character, so the previous example can also be written like so:

 [!!
  #define complex %(
     "key1" "value"
     "key2" "value"
     "key3" "value"
     "nest" %(
         "key1" "value"
         "key2" "value"
     )
  )
 !!]

#is_array arg

Returns true if arg is an array.

 [!! #if #is_array thing "The thing is an array!" !!]

#is_blessed arg

Returns the name of the package that arg is blessed into, or false if it's unblessed.

 [!! "It's a " #or #is_blessed thing "native type" !!]

sub _proto_is_code_ref { [ ANY ] } sub _is_code_ref { $_[0]->is_code( $_[0]->get_arg ) }

#is_code_ref arg

Returns true if arg is a code reference.

 [!! #if #is_code_ref var "var is a code reference!" !!]

#is_expr arg

Returns true if arg is a Wigwam expression object.

 [!! #if #is_expr thing "The thing is a Wigwam expression object!" !!]

#is_hash arg

Returns true if arg is a hash.

 [!! #if #is_hash thing "The thing is a hash!" !!]

#is_list arg

Returns true if arg is an array (same as #is_array).

 [!! #if #is_list thing "The thing is an array!" !!]

#is_num arg

Returns true if arg is a valid numeric value.

 [!! #if #is_num "012.1E10" "Valid" /* output: Valid */ !!]

#is_string arg

Returns true if arg is a string.

 [!! #if #is_string thing "The thing is a string!" !!]

#is_scalar arg

Returns true if arg is scalar reference.

 [!! #if #is_scalar thing "The thing is a scalar reference!" !!]

#ref arg

Returns the reference type of arg.

 [!! #if #eq "ARRAY" #ref thing "thing contains an array!" !!]

#scalar arg

Casts arg to a scalar.

 [!! #scalar ( "a" "b" "c" ) /* output: 3 */ !!]

...which is the size of the array which we are casting to a scalar.

Typically, the #scalar directive is aliased to the $ character, in which case preceding example could also be coded like so:

 [!! $( "a" "b" "c" ) /* output: 3 */ !!]