| 1 | # -*- perl -*- | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # $Id: FromString.pm,v 1.4 2007/10/03 10:24:50 eserte Exp $ | ||||||||||||||||||||
| 5 | # Author: Slaven Rezic | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | # Copyright (C) 2001 Onlineoffice. All rights reserved. | ||||||||||||||||||||
| 8 | # Copyright (c) 2002 Slaven Rezic. All rights reserved. | ||||||||||||||||||||
| 9 | # This is free software; you can redistribute it and/or modify it under the | ||||||||||||||||||||
| 10 | # terms of the GNU General Public License, see the file COPYING. | ||||||||||||||||||||
| 11 | # | ||||||||||||||||||||
| 12 | # Mail: slaven@rezic.de | ||||||||||||||||||||
| 13 | # WWW: http://we-framework.sourceforge.net | ||||||||||||||||||||
| 14 | # | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | package WE::Util::GenericTree::FromString; | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | GenericTree::FromString - creating GenericTrees from a string representation | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | my $tree = new GenericTree::FromString <<EOF; | ||||||||||||||||||||
| 25 | A | ||||||||||||||||||||
| 26 | AA | ||||||||||||||||||||
| 27 | AB | ||||||||||||||||||||
| 28 | AC | ||||||||||||||||||||
| 29 | ACA | ||||||||||||||||||||
| 30 | ACB | ||||||||||||||||||||
| 31 | AD | ||||||||||||||||||||
| 32 | AE | ||||||||||||||||||||
| 33 | B | ||||||||||||||||||||
| 34 | C | ||||||||||||||||||||
| 35 | CA | ||||||||||||||||||||
| 36 | EOF | ||||||||||||||||||||
| 37 | $tree->isa("GenericTree"); # yes | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | 2 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | 2 | $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||
| 46 | # use base does not work for 5.005? | ||||||||||||||||||||
| 47 | 2 | ▶ | use WE::Util::GenericTree; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 2 | push @ISA, 'WE::Util::GenericTree'; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # from: http://groups.google.com/group/perl.perl5.porters/browse_frm/thread/6f5edc58ec8ee045 | ||||||||||||||||||||
| 51 | 12 | ▶ | sub rebless ($$){ | ||||||||||||||||||
|
Subroutine
rebless: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 52 | 12 | my ($self, $newclass) = @_; | |||||||||||||||||||
| 53 | 12 | ▶ | if ($] < 5.009) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 0 | bless $self, $newclass; | |||||||||||||||||||
| 55 | 0 | return $self; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | 12 | my $oldclass = ref $self; | |||||||||||||||||||
| 58 | 12 | require Hash::Util; # caveat; prototype does not work because of this | |||||||||||||||||||
| 59 | 12 | &Hash::Util::unlock_hash(\%$self); | |||||||||||||||||||
| 60 | 12 | bless $self, $newclass; | |||||||||||||||||||
| 61 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | 12 | &Hash::Util::lock_keys(\%$self, keys %{$oldclass.'::FIELDS'}); | |||||||||||||||||||
| 63 | 12 | return $self; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 12 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||
| 67 | 12 | my $proto = shift; | |||||||||||||||||||
| 68 | 12 | my $string_rep = shift; | |||||||||||||||||||
| 69 | 12 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 12 | my $root = my $tree = new WE::Util::GenericTree {Id => ""}; # root | |||||||||||||||||||
| 72 | 12 | my $last_level = -1; | |||||||||||||||||||
| 73 | 12 | foreach my $line (split /\n/, $string_rep) { | |||||||||||||||||||
| 74 | 80 | $line =~ /^(\s*)(.*)/; | |||||||||||||||||||
| 75 | 80 | ▶ | my $level = defined $1 ? length $1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 80 | my $value = $2; | |||||||||||||||||||
| 77 | 80 | ▶ | if ($level > $last_level+1) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 78 | 0 | die "Too big jump from level $last_level to level $level in line $line"; | |||||||||||||||||||
| 79 | } elsif ($level == $last_level+1) { | ||||||||||||||||||||
| 80 | 50 | my $subtree = $tree->subtree($value); | |||||||||||||||||||
| 81 | 50 | $last_level++; | |||||||||||||||||||
| 82 | 50 | $tree = $subtree; | |||||||||||||||||||
| 83 | } elsif ($level == $last_level) { | ||||||||||||||||||||
| 84 | 10 | $tree = $tree->parent->subtree($value); | |||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||
| 86 | #warn "$level .. $last_level $value"; | ||||||||||||||||||||
| 87 | 20 | for ($level .. $last_level-1) { | |||||||||||||||||||
| 88 | 25 | $tree = $tree->parent; | |||||||||||||||||||
| 89 | 25 | $last_level--; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | 20 | $tree = $tree->parent->subtree($value); | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 12 | rebless $root, $class; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 2 | 1; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||