/usr/share/perl5/Graph/Easy/Base.pm is in libgraph-easy-perl 0.75-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | #############################################################################
# A baseclass for Graph::Easy objects like nodes, edges etc.
#
#############################################################################
package Graph::Easy::Base;
$VERSION = '0.75';
use strict;
use warnings;
#############################################################################
{
# protected vars
my $id = 0;
sub _new_id { $id++; }
sub _reset_id { $id = 0; }
}
#############################################################################
sub new
{
# Create a new object. This is a generic routine that is inherited
# by many other things like Edge, Cell etc.
my $self = bless { id => _new_id() }, shift;
my $args = $_[0];
$args = { name => $_[0] } if ref($args) ne 'HASH' && @_ == 1;
$args = { @_ } if ref($args) ne 'HASH' && @_ > 1;
$self->_init($args);
}
sub _init
{
# Generic init routine, to be overriden in subclasses.
my ($self,$args) = @_;
$self;
}
sub self
{
my $self = shift;
$self;
}
#############################################################################
sub no_fatal_errors
{
my $self = shift;
$self->{fatal_errors} = ($_[1] ? 1 : 0) if @_ > 0;
~ ($self->{fatal_errors} || 0);
}
sub fatal_errors
{
my $self = shift;
$self->{fatal_errors} = ($_[1] ? 0 : 1) if @_ > 0;
$self->{fatal_errors} || 0;
}
sub error
{
my $self = shift;
# If we switched to a temp. Graphviz parser, then set the error on the
# original parser object, too:
$self->{_old_self}->error(@_) if ref($self->{_old_self});
# if called on a member on a graph, call error() on the graph itself:
return $self->{graph}->error(@_) if ref($self->{graph});
if (defined $_[0])
{
$self->{error} = $_[0];
if ($self->{_catch_errors})
{
push @{$self->{_errors}}, $self->{error};
}
else
{
$self->_croak($self->{error}, 2)
if ($self->{fatal_errors}) && $self->{error} ne '';
}
}
$self->{error} || '';
}
sub error_as_html
{
# return error() properly escaped
my $self = shift;
my $msg = $self->{error};
$msg =~ s/&/&/g;
$msg =~ s/</</g;
$msg =~ s/>/>/g;
$msg =~ s/"/"/g;
$msg;
}
sub catch_messages
{
# Catch all warnings (and errors if no_fatal_errors() was used)
# these can later be retrieved with warnings() and errors():
my $self = shift;
if (@_ > 0)
{
if ($_[0])
{
$self->{_catch_warnings} = 1;
$self->{_catch_errors} = 1;
$self->{_warnings} = [];
$self->{_errors} = [];
}
else
{
$self->{_catch_warnings} = 0;
$self->{_catch_errors} = 0;
}
}
$self;
}
sub catch_warnings
{
# Catch all warnings
# these can later be retrieved with warnings():
my $self = shift;
if (@_ > 0)
{
if ($_[0])
{
$self->{_catch_warnings} = 1;
$self->{_warnings} = [];
}
else
{
$self->{_catch_warnings} = 0;
}
}
$self->{_catch_warnings};
}
sub catch_errors
{
# Catch all errors
# these can later be retrieved with errors():
my $self = shift;
if (@_ > 0)
{
if ($_[0])
{
$self->{_catch_errors} = 1;
$self->{_errors} = [];
}
else
{
$self->{_catch_errors} = 0;
}
}
$self->{_catch_errors};
}
sub warnings
{
# return all warnings that occured after catch_messages(1)
my $self = shift;
@{$self->{_warnings}};
}
sub errors
{
# return all errors that occured after catch_messages(1)
my $self = shift;
@{$self->{_errors}};
}
sub warn
{
my ($self, $msg) = @_;
if ($self->{_catch_warnings})
{
push @{$self->{_warnings}}, $msg;
}
else
{
require Carp;
Carp::carp('Warning: ' . $msg);
}
}
sub _croak
{
my ($self, $msg, $level) = @_;
$level = 1 unless defined $level;
require Carp;
if (ref($self) && $self->{debug})
{
$Carp::CarpLevel = $level; # don't report Base itself
Carp::confess($msg);
}
else
{
Carp::croak($msg);
}
}
#############################################################################
# class management
sub sub_class
{
# get/set the subclass
my $self = shift;
if (defined $_[0])
{
$self->{class} =~ s/\..*//; # nix subclass
$self->{class} .= '.' . $_[0]; # append new one
delete $self->{cache};
$self->{cache}->{subclass} = $_[0];
$self->{cache}->{class} = $self->{class};
return;
}
$self->{class} =~ /\.(.*)/;
return $1 if defined $1;
return $self->{cache}->{subclass} if defined $self->{cache}->{subclass};
# Subclass not defined, so check our base class for a possible set class
# attribute and return this:
# take a shortcut
my $g = $self->{graph};
if (defined $g)
{
my $subclass = $g->{att}->{$self->{class}}->{class};
$subclass = '' unless defined $subclass;
$self->{cache}->{subclass} = $subclass;
$self->{cache}->{class} = $self->{class};
return $subclass;
}
# not part of a graph?
$self->{cache}->{subclass} = $self->attribute('class');
}
sub class
{
# return our full class name like "node.subclass" or "node"
my $self = shift;
$self->error("class() method does not take arguments") if @_ > 0;
$self->{class} =~ /\.(.*)/;
return $self->{class} if defined $1;
return $self->{cache}->{class} if defined $self->{cache}->{class};
# Subclass not defined, so check our base class for a possible set class
# attribute and return this:
my $subclass;
# take a shortcut:
my $g = $self->{graph};
if (defined $g)
{
$subclass = $g->{att}->{$self->{class}}->{class};
$subclass = '' unless defined $subclass;
}
$subclass = $self->{att}->{class} unless defined $subclass;
$subclass = '' unless defined $subclass;
$self->{cache}->{subclass} = $subclass;
$subclass = '.' . $subclass if $subclass ne '';
$self->{cache}->{class} = $self->{class} . $subclass;
}
sub main_class
{
my $self = shift;
$self->{class} =~ /^(.+?)(\.|\z)/; # extract first part
$1;
}
1;
__END__
=head1 NAME
Graph::Easy::Base - base class for Graph::Easy objects like nodes, edges etc
=head1 SYNOPSIS
package Graph::Easy::My::Node;
use Graph::Easy::Base;
@ISA = qw/Graph::Easy::Base/;
=head1 DESCRIPTION
Used automatically and internally by L<Graph::Easy> - should not be used
directly.
=head1 METHODS
=head2 new()
my $object = Graph::Easy::Base->new();
Create a new object, and call C<_init()> on it.
=head2 error()
$last_error = $object->error();
$object->error($error); # set new messags
$object->error(''); # clear the error
Returns the last error message, or '' for no error.
When setting a new error message, C<< $self->_croak($error) >> will be called
unless C<< $object->no_fatal_errors() >> is true.
=head2 error_as_html()
my $error = $object->error_as_html();
Returns the same error message as L<error()>, but properly escaped
as HTML so it is safe to output to the client.
=head2 warn()
$object->warn('Warning!');
Warn on STDERR with the given message.
=head2 no_fatal_errors()
$object->no_fatal_errors(1);
Set the flag that determines whether setting an error message
via C<error()> is fatal, e.g. results in a call to C<_croak()>.
A true value will make errors non-fatal. See also L<fatal_errors>.
=head2 fatal_errors()
$fatal = $object->fatal_errors();
$object->fatal_errors(0); # turn off
$object->fatal_errors(1); # turn on
Set/get the flag that determines whether setting an error message
via C<error()> is fatal, e.g. results in a call to C<_croak()>.
A true value makes errors fatal.
=head2 catch_errors()
my $catch_errors = $object->catch_errors(); # query
$object->catch_errors(1); # enable
$object->...(); # some error
if ($object->error())
{
my @errors = $object->errors(); # retrieve
}
Enable/disable catching of all error messages. When enabled,
all previously caught error messages are thrown away, and from this
poin on new errors are non-fatal and stored internally. You can
retrieve these errors later with the errors() method.
=head2 catch_warnings()
my $catch_warns = $object->catch_warnings(); # query
$object->catch_warnings(1); # enable
$object->...(); # some error
if ($object->warning())
{
my @warnings = $object->warnings(); # retrieve
}
Enable/disable catching of all warnings. When enabled, all previously
caught warning messages are thrown away, and from this poin on new
warnings are stored internally. You can retrieve these errors later
with the errors() method.
=head2 catch_messages()
# catch errors and warnings
$object->catch_messages(1);
# stop catching errors and warnings
$object->catch_messages(0);
A true parameter is equivalent to:
$object->catch_warnings(1);
$object->catch_errors(1);
See also: L<catch_warnings()> and L<catch_errors()> as well as
L<errors()> and L<warnings()>.
=head2 errors()
my @errors = $object->errors();
Return all error messages that occured after L<catch_messages()> was
called.
=head2 warnings()
my @warnings = $object->warnings();
Return all warning messages that occured after L<catch_messages()>
or L<catch_errors()> was called.
=head2 self()
my $self = $object->self();
Returns the object itself.
=head2 class()
my $class = $object->class();
Returns the full class name like C<node.cities>. See also C<sub_class>.
=head2 sub_class()
my $sub_class = $object->sub_class();
Returns the sub class name like C<cities>. See also C<class>.
=head2 main_class()
my $main_class = $object->main_class();
Returns the main class name like C<node>. See also C<sub_class>.
=head1 EXPORT
None by default.
=head1 SEE ALSO
L<Graph::Easy>.
=head1 AUTHOR
Copyright (C) 2004 - 2008 by Tels L<http://bloodgate.com>.
See the LICENSE file for more details.
X<tels>
X<bloodgate>
X<license>
X<gpl>
=cut
|