Re: my, my, my
by dws (Chancellor) on Nov 14, 2002 at 18:48 UTC
|
Something smells about that subroutine. On first glance it looks like a refactoring to split it into two methods (to get rid of the $nodes_only) parameter might make the code clearer. It also gives you the opportunity to pick better names (e.g., don't suggest that a routine returns a list when it really returns a hash).
I'm also curious why you would return the instance and the node list. If you're trying to hide details, handing back that instances raises a flag that there's something amiss in whichever callers are using the nodes_only form. Also, it seems odd to return undef rather than an empty hash if there no divisions. Is that special case really an abnormal error?
A refactoring might look like:
sub getDivisionListNodes {
my($agency) = @_;
return undef unless defined($agency);
return new($agency)->{ts}->get_node_list('division');
}
sub getDivisionNameToIdMap {
my($agency) = @_;
return undef unless defined($agency);
my %nameToId;
foreach my $node ( getDivisionListNodes($agency) ) {
$nameToId{$node->value{'divname')} = $node->value('siebel_id')
+;
}
return %nameToId;
}
Note that the 'my' count has been reduced.
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
This gets especially onerous when you have 80 divisions and need to call getDivisionList80 times that spells 80 object creations and destructions instead of just one object creation and 80 method calls with the initialized object.
Without knowing what kind of additional overhead is involved with creating an instance of an object, it's hard to know whether it's worth worrying about a relatively small number of creates and DESTROYs. If there's a lot of overhead involved, you might consider creating a special DivisionLister class that can hang on to a single instance of the object.
| [reply] |
Re: my, my, my
by Abigail-II (Bishop) on Nov 14, 2002 at 16:53 UTC
|
Then don't use them. use strict isn't mandatory.
It's an option.
Abigail | [reply] [d/l] |
Re: my, my, my
by YuckFoo (Abbot) on Nov 14, 2002 at 18:16 UTC
|
I hear ya brother. I'm also tired of brushing my teeth and checking my rearview mirror. | [reply] |
|
|
| [reply] [d/l] |
|
|
No, that wouldn't help. There has to be a way to discriminate declaration from use if you want to be able to tell a use of a mistyped variable apart from a declaration of a new one. I don't think three characters is a high price to pay for that service - especially considering you can, if the occasion lends itself to it, declare multiple variables with a single my.
Your code has room for clarification and shortening in other areas; I'd write it like this:
sub getDivisionList {
(my ($agency_name, $nodes_only) = @_) or return;
my $obj = __PACKAGE__->new($agency_name);
my @node = $obj->{ts}->get_node_list('division');
return
(!@node) ? () :
$nodes_only ? ($o, @n) :
map {($n->value('divname'), $n->value('siebel_id'))} @n;
}
(The jury's still out on how to indent the dangling last "default" expression, btw. I'm not entirely satisfied with any of the options so far.)
Makeshifts last the longest. | [reply] [d/l] |
|
|
|
|
|
Re: my, my, my
by John M. Dlugosz (Monsignor) on Nov 14, 2002 at 19:33 UTC
|
I'd like to use the mathematical symbol ∃ to declare variables. Now that you mention it, the word "foreach" is a bit long and tedious, as well, and "while" isn't far behind. So I'd like to use ∀ for looping.
—
John | [reply] |
|
|
Perl6 will let you, with operator-subs, and unicode-named subs. Of course, being able to type ∃ and ∀ is up to you. (You can do this in p5 with a source filter, but you might have trouble if you use ∃ or ∀ for other things in your code, and the filter isn't written correctly.
Of course, you were joking, weren't you. Perl6 will allow implementation of all sorts of new jokes.
Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).
| [reply] |
|
|
Doesn't Perl 5 allow Unicode in sub names, just as in any variable name?
Those are math symbols, not "letters", so it won't match \W and would not be legal in identifiers, as things stand now.
But I can certainly see the use for non-letter names of one character (similar to today's punctuation-mark names), as that will open up the math operators and other symbols.
| [reply] |
|
|
|
|
|
Re: my, my, my
by diotalevi (Canon) on Nov 14, 2002 at 15:30 UTC
|
I take it you want undeclared variables to be used in a lexical scope instead of global. I don't see what functionality you're adding except for making 'my' optional. Nice idea though. I'd probably want a something-scoped prama to select which behaviour undeclared variables go to though. I'd guess you could implement this for perl6 if you wanted it bad enough.
__SIG__
use B;
printf "You are here %08x\n", unpack "L!", unpack "P4", pack
"L!", B::svref_2object(sub{})->OUTSIDE;
| [reply] [d/l] |