osdir.com
mailing list archive

Subject: The Fine Art of Naming Things - Re: array numerical name... - msg#00291

List: lang.perl.beginners

Date: Prev Next Index Thread: Prev Next Index

On Tuesday, April 30, 2002, at 09:18 , Jackson, Harry wrote:

Is the following more suitable or is it bollocks as well.

keeping timothy's kind and gentle rebuke in mind.....
{ my complements timothy!!! }

There are two sets of competing concerns:

a) get the code to work
b) have code that can be maintained

{ and when the money is right, they may even pay you to
write 'safe' code.... but never hold your breath on it
until the law suit backs them up against the wall on needing
to have 'security' right up there with 'quality' ... }

That the code runs under strict and with warning means that
it clearly passed a more stringent set of issues on (a) than
code that is 'in production' - the problem is with 'b' - and
hence my core concern with

my $TRY = "world";
my %TRY;

note that at a quick glance

$TRY{$h}=3;

it is possible to 'obsese' on the '$TRY' part...

So as a general rule of thumb try to lay out the variables
that you plan to use in a way that is less likely to leave
any shadow of a doubt as to what is really going on.

As chas' recent kvetchings noted - there comes that time,
at 'oh dark squat' when you are typing and not thinking....

Many of us 'old guys' were so excited when compilers were
able to use both upper and lower case letters, as that meant
that we could extend the range of our variables and function
names to things like

$DamnYankee = getCarpetBagger(@arglist); #honest it is one word...

rather than all in upper case. with the inclusion of "_" in
the token list of parsables that could be used we can "expand"
that line to what is argued to be a more readable

$Damn_Yankee = get_Carpet_Bagger(@arglist);

Your Mileage may vary - but this clearly easier to get an idea
about than say

$a = cb(@b);

So since one has a wealth of lexical tokens to create - why not
do the decent thing and avoid putting scalars and list variables
so close together in the name space.

Granted at times I tend to do sillies on gp:

my %tmpHash = ();

so that when I stumble across it later on as

$tmpHash{$key} = $val ;

I have the awareness that I am putting that val at that key
in that hash..... not that I am trying to do some triple
expansion value eval that will allow me the hyperAreobesque
into a much happier place... { that at times there are need
for things like @{<stuffhere>} should be left for when you
really need to be doing that... }

yes, I know that I have production code with embedded jokes:

@list_shrugged....

but at least at a glance I know that I have a list...

So the first trick is to protect the 'name space' - NOT
for the compiler - Pork The Compiler - but so that you are
not going to get lost in your own code - I am still in the
debate between the 'import' v. 'direct call' approach:

use Wetware::DTK;
...
my @cool_list_of_Things = &Wetware::DTK::getCool(@list_shrugged);

as opposed to

use Wetware::DTK qw/:cool :weird/;
...

my @cool_list_of_Things = getCool(@list_shrugged);

But that way leads us into where i expect a percentage
of folks will wind up - they did enough perl that they
wind up with enough basic 'site local' functions that
having a perl module is a rational extension....

So the naming the variables and functions games should start
simple and stay simple as long as you can ... to recycle the
old cobol joke:

"If you mother can read your code and understand it....
then there is half a chance management will too...."

And more importantly that YOU will at that odd hour.

Once one is in the habit of thinking about naming variables
appropriately - then one starts also natively thinking in
terms of what sort of 'data structures' come 'native' to
the coding language that one is in.

In /bin/sh - there is really no such thing as a list - and
worse yet one has no such critter as a hash so you wind up
doing things like

LONGHASH="key:val key1:val1 key2:val2"

so that one can do the

for entry in $LONGHASH
do
# suck out the key val pairs here
eval `echo $entry | awk '{
split($0,spot,":");
print "KEY="spot[1] , "VAL="spot[2] }'`

echo "Have $KEY and $VAL"

done
{ and people sed that things were so awkward in shell... }

So if one finds that the data starts laying out that way, and
one always did the above in /bin/sh - why do it in perl?

$LONGHASH = "key:val key1:val1 key2:val2";

why not simply DO the

%long_hash = (
key => 'val',
.....
);

and go with the ugly duckling

while ( my ($key, $val) = each %long_hash ) {
print "Have $key and $val\n";
# and did with them as I will....
}

All of which then leads to the:

Why try to globellate:

print @array$a[$b];

when one should look at the data - and decide if one
is trying to be a coder - or is one trying to find a
way to get something else to do the data analysis and
matching of the data to the data structure for you?

Simply so that one can funk around have have funky
expansions that look almost like

World0[$a] ....

IF the compiler actually generated up 'strings' to
manipulate - rather than FUNKY 'bit streams'....

The Text IS Your Side of the Line. The 'bit stream foo'
you should leave to the compiler.

At which point when you get to:

print ${"array$a"}[$b] ."\n"

you will notice that it required an inline 'interpolation'
of the double quoted string to find a reference to an array.

and you are of course out and away into the land of complete
WakkaDoodelEdge....

please READ Jeff's Comments:


Mark-Jason Dominus has an article about why using a variable as a name for
a variable is a bad idea. Symbolic references are a left-over artifact
from The Land Before Perl 5. They are wizardry, and should be avoided
99.9% of the time. There's about ONE place where you should use them, and
even then, you probably don't need to.

http://perl.plover.com/varvarname.html


please DO read mjd's article.

IF you still feel an exception emotional need to work in
that space - feel free to find a planet that matches the
colour of the sky in your world..... and do send postcards....

8-)


ciao
drieux

---


Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

directory browsing in TK

Hi Guys, I am wondering which module and documentations I should look for if I need to allow users of this GUI program to browse their directory and pick a file. Thanks Eric

Next Message by Date: click to view message preview

RE: Beyond Book Learning - the problem of Re: array numerical name...

> On Tuesday, April 30, 2002, at 07:32 , David Gray wrote: > [..] > > my $fred = 'one,two,three,four'; > > my $a = 0; > > @{"array$a"} = split ',', $fred; > > > > for(0..3) { > > print ${"array$a"}[$b] > > } > > File "untitled text 2"; Line 21: Name "main::b" used only > once: possible > typo > File "untitled text 2"; Line 18: Can't use string ("array0") > as an ARRAY > ref while "strict refs" in use > > > yeah I can clean that up with > > my $fred = 'one,two,three,four'; > my $a = 0; > no strict 'refs'; > @{"array$a"} = split ',', $fred; > > foreach my $b(0..3) { > print ${"array$a"}[$b] ."\n" > } > > so as to get the output > > one > two > three > four Yeah, that would be a clever typo, like it says in the cryptic error message. Sorry about that. > All of which drives me back to the question - why did we compel > ourselves to have to insert the > > "no strict 'refs';" > > given the quandery: > > > basically i want to name an array with a subscript ie > > world0[0] and world1[0] the 0/1 being a variable, i have tried to > > produce a simple example.... > > > why take the convolutions??? <http://www.m-w.com/cgi-bin/dictionary?book=Dictionary&va=quandery> Question, perhaps? <http://www.m-w.com/cgi-bin/dictionary?book=Dictionary&va=convolutions> 3. a complication or intricacy of form, design, or structure I don't use these "convolutions" of perl for large programming projects or for sensitive cgi scripts, unless they are in a very otherwise static and controlled environment, like in that example. I also find that it's only more confusing to be sesquipedalian in response to posts that only need a simple, clear answer. > What have we added to the long term maintainability of the > code by carbuncling it like this???? <http://www.m-w.com/cgi-bin/dictionary?book=Dictionary&va=carbuncling> I'll assume you mean that code is confusing. But it's not. It's very simple. It's a very easy way to perform aggregate functions on several sets of data, as long as you're careful to make sure your code doesn't have any stupid side effects, like what Jeff described. > Why not compel the author down the road to understanding > how to do good data structures??? Everyone else who responded to the thread did that, and very well I thought. Perhaps, however, the OP has a simple script that would be simplified even more if he knew how to use dynamic variables. Sometimes, when a simple script is the easiest solution to the problem, it's worth more to just get it working than to spend time developing a complex data structure. -dave

Previous Message by Thread: click to view message preview

RE: array numerical name...

On Apr 30, Jackson, Harry said: >Is the following more suitable or is it bollocks as well. > >my $TRY = "world"; >my $h = 0; > >my %TRY; >$TRY{$h}=3; >print "$TRY" . "$h" . " $TRY{$h}\n"; Well, except for the fact that $TRY and $TRY{$h} have nothing to do with each other... that is perfectly fine. You might want to use an array instead of a hash, though, if you're using numbers to differentiate between variables. my @foo; my $i = 0; $foo[$i] = "bar"; -- Jeff "japhy" Pinyan japhy@xxxxxxxxx http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ]

Next Message by Thread: click to view message preview

RE: array numerical name...

While I agree with the premise of this article, I've objected in the past to sending people there to learn about the pitfalls of the so-called "dynamically named variables". In general, if you want to convince someone of something, you don't tell them that they're stupid for wanting to do it. It's argumentative and not conducive to getting your point across. Especially when it's a logical leap for most people that is quite intuitive, even though it is a VERY BAD IDEA. Better to lead people down the right path than insult them outright. -----Original Message----- From: Jeff 'japhy' Pinyan [mailto:jeffp@xxxxxxxxxx] Sent: Tuesday, April 30, 2002 8:47 AM To: David Gray Cc: beginners@xxxxxxxx; Steven_Massey@xxxxxxxxxxxxxxxx Subject: RE: array numerical name... On Apr 30, David Gray said: >> Hi all - should be simple - but I cannot figure it out >> >> basically i want to name an array with a subscript ie >> world0[0] and world1[0] the 0/1 being a variable, i have tried to >> produce a simple example.... >> >> For any help - thanks.. >> ---------------------------------------------------------- >> @fred = "one,two,three,four"; >> >> $a=0; >> >> @array$a=split(/,/, @fred) >> >> for ($b=0;$b<4;$b++) { >> print @array$a[$b]; >> } > >Just FYI, it is possible to do that, like so: NO! Don't teach how to do this, please. It can lead to security problems in your program; it also leads to having an unknown number of variables, and possibly, variables with names you don't know. Mark-Jason Dominus has an article about why using a variable as a name for a variable is a bad idea. Symbolic references are a left-over artifact from The Land Before Perl 5. They are wizardry, and should be avoided 99.9% of the time. There's about ONE place where you should use them, and even then, you probably don't need to. http://perl.plover.com/varvarname.html -- Jeff "japhy" Pinyan japhy@xxxxxxxxx http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx For additional commands, e-mail: beginners-help@xxxxxxxx
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by