[Techtalk] Perl Frustration

Kai MacTane kmactane at GothPunk.com
Wed Nov 5 19:20:50 EST 2003


Whoops, I thought I'd replied to this already.

At 11/1/03 04:21 AM , Almut Behrens wrote:
>On Fri, Oct 31, 2003 at 04:18:39PM -0800, Kai MacTane wrote:
> > >
> > >    ${"main::${thisline}_foo"} = $thisline;
> > >  vs.
> > >    eval "\$main::${thisline}_foo = '$thisline'";
> >
> > Do you have any opinion on which would be more readable and understandable
> > to the average Perl coder?
>
>hard to tell... both incantations probably look like line noise to the
>uninitiated ;)

Well, so does much of Perl. I did say "the average *Perl* coder" for a 
reason! <g> Personally, I kind of prefer the former one... except that your 
later solution, which passes strict checking, seems even cooler.

>To spin this a little further, I should admit that I forgot to mention
>a third variant in my previous post:
>
>   chomp($thisline = <STDIN>);
>   while ($thisline =~/\w/) {
>       $main::{"${thisline}_foo"} = \"$thisline";
>       chomp($thisline = <STDIN>);
>   }
>
>This construct has the advantage of passing strict checking, so you
>don't need to fiddle with "no strict ...".

This construct started off looking like total Greek to me. By the end of 
your post, and your clear and wonderful explanation of exactly how it 
works, I find that I actually can understand it. And it gives me a bit more 
insight into how the other two work, as well.

>This is nothing more than a direct assignment into the package's symbol
>table hash (often called 'stash'), and thus is not subject to strict
>checking (Perl's inventors in this case obviously assumed you know what
>you're doing...).

An assumption that can really bite you, if you know just enough to be 
dangerous with it. (Yes, that phrase is pointed squarely back at myself.)

>Readability? Well ...

I think that can be fixed with comments. It's just one line, and it's not 
*that* heinous... I hope.

Given that I'm planning on using this in production/distribution code, 
should I credit you for this technique? This is something more advanced 
than I could have come up with myself, and I don't want to pass your work 
off as mine, unless you feel that this is just "a technique that any Perl 
coder should know", and that you're acting in the capacity of a teacher.

Thanks very much for your help!

                                                 --Kai MacTane
----------------------------------------------------------------------
"I'm terrified, intoxicated, starry-eyed and bollock naked,
  Child-bearing, adult-rated, and thoroughly re-educated..."
                                                 --Carter USM,
                                                  "Re-Educating Rita"



More information about the Techtalk mailing list