Tough Camel

avik on 2003-05-07T19:23:01

Programming in Perl is difficult, because it is not intuitive. It is good, though. Sometimes I experience a certain relief when I find an elegant solution to an old dilema. And more often than before I put comments like # THANK YOU, LORD! or # Glory to God in the Highest! inside my code. Why? Because I'm not a perl programmer, I am a perl user, I suppose. Although I managed to write quite an extensive application in Perl, I feel like my learning of this language will never end. Unfortunately.

Don't you wish you would be able to say "I speak Perl fluently". How many of you out there can say that honestly? Does Perl have to be difficult?

How about one of my recent eurica's:

sub copy_button {
    my ($item, $item_id, $parent_id) = @_;
    my $kids = { 
        pilot => 'survey',
        survey => 'section',
        section => 'question',
        question => 'option' 
    };
             
    my $ref = $dbh->selectall_arrayref("
        SELECT ID, NAME FROM $$kids{$item}s
        WHERE $item\_id = $item_id
    ");
    
    return submit({
        name => 'copy_'.$item.'_'.$parent_id,
        value => 'Copy'
    }) 
    . popup_menu({
        name => 'source',
        value => [ map ($$_[0], @$ref ) ],
        label => { map {$$_[0], $$_[1]} @$ref }  # Glory to God in the Highest!  
    });    
}

I wish it would be simpler. Should I try Ruby?..


Fat comma

rafael on 2003-05-07T19:56:43

Perl is like natural languages, it becomes more and more intuitive once you assimilate the idioms.

Your code would be easier to read if you write
    { map {$$_[0] => $$_[1]} @$ref }
to underline the fact it builds a hash. It's a common idiom.

(On the other hand, you can write
    { map @$_, @$ref }
as well. I wouldn't recommend it.)

My learning of perl has not ended yet either. I don't think it's discouraging.

Natural language

jmm on 2003-05-07T20:56:03

Like natural languages, you can talk baby talk in Perl and communicate usefully. And, like with natural languages, there are always new levels of mastery possible. Most of us are not yet capable of writing the equivalent of Romeo and Juliet in Perl.

Re:Natural language

dreadpiratepeter on 2003-05-08T03:00:54

Maybe, but Damian's SelfGOL has to be James Joyce's Ulysses.

Simpler != Better

chaoticset on 2003-05-08T05:32:20

If you want to jump in the deep end, or just need to make this half as long and a little more complex, or want to learn some Perl idioms as quickly and painfully as possible (figuratively speaking), go to the Monastery and post in Seekers, indicating that you're looking to make this shorter and sweeter. Ask nicely. (Read the Friendly FAQ on posting first, it'll save you a lot of time and trouble later.) You'll get a lot of responses, most of which will include rewrites of your code in maybe one-third as many lines. If you can take constructive criticism, it's a good way to improve stuff.

Re:Simpler != Better

avik on 2003-05-12T02:54:05

I still think that simpler is better. Specially, if it requires to be passed around to another (unsuspecting) individual, other than yourself. So called "Monastery" is a good idea, can't argue with that, but, excuse me, Perl is not a religion, it does not require a monastery, monks or following... It's a programming language... Right?...

Re:Simpler != Better

chaoticset on 2003-07-31T12:43:51

Yes and no. Some people like it so much that they become, more or less, "disciples of the faith".

I think it's more about what it is to you. If you want it to be a regular ol' programming language, it's certainly that. But there is a sense of community that follows the language around the battered* landscape of the Net, and the resource that the community can be is invaluable compared to the language.

I know it's frustrating at times. Just yesterday night I banged my head against regex after regex, and they're easy regex, they're not really that complex, and I should know better by now.

It's a long, winding path. So is every other language. One of the differences with Perl that I like so much is that more advanced features don't typically require lots of extra code -- usually, you just slap a module into the appropriate place.

So, yes, it is -- but to some people, it's more than that. If it's not to you, nobody will fault you for it. I'd still recommend you check out CPAN, though. :)

Re:Simpler != Better

jdavidb on 2003-07-31T17:14:06

Perl is a society, and we use a lot of analogies for that society.

If it makes you feel better, pretend the monks are Jedi adepts. Stronger in the force I feel you growing, young padawan! :)

Keep growing, er, going

VSarkiss on 2003-05-08T18:53:23

So you're saying you're a novice. That's OK. As Larry said, "You can write baby Perl and we won't laugh." But you may want to learn the seven stages of a Perl programmer and see how soon you will become a Perl Adept. (I'm still working on it myself.)

Re:Keep growing, er, going

avik on 2003-05-12T02:56:21

Ay-mart, I want not to become a Perl Adept, just a Perl programmer... Okay, let me look at your link :) Thanks!

You'll never stop learning Perl...

schwern on 2003-07-30T22:49:25

...or any other language. Languages capable of expressing the whole of human existence are large, complex things. Languages capable of expressing this *elegantly* are even more so. Fortunately, we don't need to know the whole language to be fluent in it. Even though I don't know what "pseudoconcha" means, I can get along without that piece of information. More importantly, I know how to find out what it means.

As if that wasn't enough, Perl is constantly growing. The community, the coding standards, the code on CPAN. What was considered good style five years ago is now looked on as a bit amatuerish. We learn, we grow, we change. Such is the stuff of life. In the other direction lies COBOL, FORTRAN, even LISP. Good languages that didn't or couldn't adapt.

So don't despair, we're all in the same boat.

Should you try Ruby? Yes. Its a wonderful language borrowing elements from Perl 5 and Smalltalk and even preempting chunks of Perl 6.

Could your code be simpler? Sure. Apply a few idioms you might not know:

        name => 'copy_'.$item.'_'.$parent_id,

using the less ambiguous ${foo} syntax, this can become:

        name => "copy_${item}_$parent_id",

Honestly, I've never understood why popup_menu() requires you to put in *both* the values and the labels since the value entry is then redundant. So write a wrapper:

        sub my_popup_menu {
                my $args = shift;
                $args->{values} = [ keys $self->{labels} ]
                        if $self->{labels} && !$self->{values};
                popup_menu($args);
        }

and then use it:

        my_popup_menu({
                name => 'source',
                labels => { map {$_->[0], $_->[1] } @$items }
        })

On a more grand scale, if you're doing a lot of database work it would behoove you to look at modules like Ima::DBI, Class::DBI. Alazbo, etc... which abstracts away a lot of this work. Modules like Template::Toolkit mean you won't have to stick HTML formatting code in your program. More stuff to learn, but it makes coding easier.

Jarkko Hietaniemi, 5.8 pumpking, puts at the end of every email: "There is this special biologist word we use for 'stable'. It is 'dead'." If you ever learned the entirety of Perl, or any language, it would be dead.

Re:You'll never stop learning Perl...

avik on 2003-11-21T15:09:53

This is deep. Thanks for your comment back in July. I'm getting better in learning Perl idioms and ways to do job better. Still use VBA for quick analytical projects, though, it's just so much easier to put something "quick and dirty" together than it would be in Perl. But Perl really saves a lot of my time on some things where VBA just gives up.