Thu 9 Nov 2006
The author of this module chose to use some file scoped variables in a module. This effectively created some global variables used outside the sub. This is not the recommended way to write a subroutine, but I needed an example and this one was handy.
-
my $htmltree;
-
my $node;
-
my @prevnodes;
-
my $htmloutput;
-
-
sub start {
-
my $newnode = {};
-
-
$newnode->{tag} = $tagname;
-
$newnode->{$key} = $attr->{$key};
-
}
-
$newnode->{content} = [];
-
$node = $newnode->{content};
-
}
The author also decided to use a hash reference ($newnode
) for the data structure. Had he used a hash instead things might have been simpler. Take a look at the addition of the keys from %{$attr}
. This same thing can occur with a hash slice. Let's let an example tell the story.
-
my $newnode = {};
-
-
$newnode->{tag} = $tagname;
-
$newnode->{$key} = $attr->{$key};
-
}
-
$newnode->{content} = [];
-
}
Could be written as:
Which could be written as:
And the whole sub becomes:
While keys
is not guaranteed to be in the same order each time it is called, it is guaranteed to be in the same order as values
as long as no operations to change the hash have occurred.
Leave a Reply
You must be logged in to post a comment.