The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

CWB::CEQL::String - Typed strings with annotations (return values of DPP rules)

SYNOPSIS

  use CWB::CEQL::String;

  $op = new CWB::CEQL::String ">=";
  $op->type("Operator");
  ## SAME AS: $op = new CWB::CEQL::String ">=", "Operator";

  print "42 $op 0\n"; # prints "42 >= 0"
  if ($op->type eq "Operator") { ... }

  $string = new CWB::CEQL::String "my string", "String";
  $string .= " is beautiful";       # changes string, but not its type
  $string->value("another string"); # $string = "..."; would replace with ordinary string
  print $string->value, "\n";       # access string value explicitly

  $string->attribute("charset", "ascii"); # declare and/or set user-defined attribute
  if ($string->attribute("charset") eq "utf8") { ... }

  $new_string = $string->copy;      # $new_string = $string; would point to same object

DESCRIPTION

This module implements a class of typed, string-like objects that are used as return values of DPP grammar rules (e.g. to distinguish between different categories of symbols (IDs, operators, etc.).

In appropriate contexts, a CWB::CEQL::String object behaves like an ordinary string, whose type can be determined with the type method. Optional further annotations can be added and retrieved with the attribute method.

Important note: automatic conversion of CWB::CEQL::String objects to a number in numerical context usually does not work. Use the value method explicitly in this case.

METHODS

$obj = new CWB::CEQL::String $string [, $type];

Returns new CWB::CEQL::String object $obj holding string value $string. If $type is given, $obj is assigned to the specified type.

$string = $obj->value;
$string = "$obj";

Return string value of CWB::CEQL::String object $obj. Overloading ensures that this value is accessed automatically if $obj is used in a string context (such as interpolation).

$obj->value($string);

Change string value of $obj. Note that a simple assignment $obj = $string would overwrite $obj with a plain string.

$obj->append($string);
$obj .= $string;

Append $string to string value of $obj.

$obj->type($type);

Set or change type of $obj (returns previous value).

$type = $obj->type;
$type = ~$obj;

Return type of the CWB::CEQL::String object. The returned value may be undef if $obj hasn't been assigned to a type.

$obj->attribute($name, $value);

Define new user attribute $name with value $value, or change value of existing attribute.

$value = $obj->attribute($name);

Returns value of user attribute $name. It is an error to read an attribute that has not been defined before.

$new_obj = $obj->copy;

Returns a copy of the CWB::CEQL::String object $obj. Note that after a simple assignment $new_obj = $obj, the two variables would contain the same object (so changing one of them would also modify the other).

The copy method makes a flat copy of the internal hash of user attributes. Therefore, complex data structures used as attribute values will be shared between $new_obj and $obj.

$result = $obj->cmp($obj2 [, $reverse]);

The cmp method implements string comparison operators for CWB::CEQL::String objects. The second operand $obj2 must either be a plain string or another CWB::CEQL::String object. If the optional argument $reverse is TRUE, the comparison is reversed (so a string as first operand can be compared with a CWB::CEQL::String object).

COPYRIGHT

Copyright (C) 2005-2022 Stephanie Evert [https://purl.org/stephanie.evert]

This software is provided AS IS and the author makes no warranty as to its use and performance. You may use the software, redistribute and modify it under the same terms as Perl itself.