Alt-CWB-ambs

 view release on metacpan or  search on metacpan

lib/CWB/CEQL/String.pm  view on Meta::CPAN

package CWB::CEQL::String;

use warnings;
use strict;

use Carp;

use overload
  '""' => \&value,
  '.=' => \&append,
  '~' => \&type,
  'cmp' => \&cmp,
  ;

=head1 NAME

CWB::CEQL::String - String-like objects with category information (as return values of DPP rules)

=head1 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

=head1

=head1 DESCRIPTION

B<** TODO **>

Note: automatic conversion to number in numerical expression does usually not work -- use value() method explicitly in this case


=head1 METHODS

=over 4

=item I<$obj> = B<new> CWB::CEQL::String I<$string> [, I<$type>];

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

=cut

sub new {
  my ($class, $value, $type) = @_;
  my $self = {
              VALUE => $value,
              TYPE => $type,  # undef if not specified
              ATTRIBUTE => {},
             };
  return bless($self, $class);
}

=item I<$string> = I<$obj>->B<value>;

=item I<$string> = "I<$obj>";

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

=item I<$obj>->B<value>(I<$string>);

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

=cut

sub value {
  my ($self, $new_val) = @_;
  if (defined $new_val) {
    $self->{VALUE} = $new_val;
  }
  return $self->{VALUE};
}

=item I<$obj>->B<append>(I<$string>);

=item I<$obj> .= I<$string>;



( run in 3.210 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )