On Monday, June 3, 2002, at 03:21 , Hanson, Robert wrote:

> I think the general answer you will get is "don't do it that way" for
> various reasons.  It is only ever a good idea if there are absolutely no
> alternatives, and even then you should always rethink doing it that way.

below I will try to offer you some support as to why such is
convolution creation enablement HELL....

> This code will do exactly the same thing except it uses keys in a hash to
> give you the ability to dynamically specify the name of the data, and gets
> rid of the warning.
>
> #!/usr/bin/perl -w
> my $hello='helloworld';
> $data{$hello} = 'foo';
> print STDOUT $data{helloworld}."\n";

Robert - you would of course wish to add:

        use strict;
        my %data;

to make sure that you got safely on down the line.

[..]
> -----Original Message-----
> From: Alan John Drew [mailto:[EMAIL PROTECTED]]
> Sent: Monday, June 03, 2002 6:15 AM
[..]
> [PbFe]$ more foobar.pl
> #!/usr/bin/perl -w
> #my $helloworld;
> my $hello='helloworld';
> ${$hello}='foo';
> print STDOUT $helloworld."\n";
>
> [PbFe]$ ./foobar.pl
> Name "main::helloworld" used only once: possible typo at ./test.pl line 7.
> foo
> [PbFe]$
[..]
> Can't declare scalar deref in my at ./foobar.pl line 4, near "}="
> Execution of ./foobar.pl aborted due to compilation errors.
[..]

part of what you need to do is 'use it again' if all you want is to
turn off the warning....

the problem of course is that you are on the fast path to making
side effect driven buggy code.

Note that you can not easily use strict to protect you - so you
can not do the

        # my $helloworld;

since that would lead to the circular problem of what was the
${$hello} really suppose to be referencing...

notice what happens with:

        my $hello='helloworld';
        ${$hello}='foo';
        print "\${\$hello} is $helloworld.\n";

        ${$hello}='bar';
        print "\${\$hello} is $helloworld.\n";

        $hello='badWorld';
        ${$hello}='spazMe';

        print "\${\$hello} is $helloworld at ${$hello}\n";
        $helloworld='FremongeNoid';
        print "\${\$hello} is $helloworld at ${$hello}\n";

this will generate

        ${$hello} is foo.
        ${$hello} is bar.
        ${$hello} is bar at spazMe
        ${$hello} is FremongeNoid at spazMe

so while your original piece offered the 'hint' that you would
be able to do 'cool things' - within only a few tweeks you have
begun to lose track of who changed what when where and why - without
much real improvement on the code. PLUS you have had to give up

        a) use strict
        b) initialization of variables
                { hence no chance to use 'default values' }

All so that you could imply to yourself the potential equivolencey of

        $helloworld sometimes is ${$hello}

so while yes, it can be done - the question really is whether
or not you wish to be that RUDE to yourself when you come back in
a while and have to maintain that code ... { or will your future
you come back and just slap your YabaHo's around the room.... }

The other principle reason you hear people advocating against
this approach is that it allows a security issue.... Think what
would happen if you took in data somewhere and assigned it to
this $hello variable.... and you had taken this sort of habit,
suddenly your code would be breeding variables all over the place
in its run time environment.... mjd's article is out there....

ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to