Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Little things ;)

by Caillte (Friar)
on Dec 13, 2001 at 17:14 UTC ( #131605=perlmeditation: print w/ replies, xml ) Need Help??

My supervisor, who is a good perl programmer himself, asked me if I knew of a way to count the keys in a hash. 'Sure,' I replied and wrote down $#{[keys(%hash)]}+1.

He studied it for a moment or two, said 'ugh' and wandered off shaking his head in disgust.

People say perl is unintelligable..... for the life of me I cannot see why ;)

I was wondering if anyone had a similar story... how the use of a little syntactic gibberish has led to feelings of wonder/amazement/disgust in others (delete as appropriate).

This page is intentionally left justified.

Comment on Little things ;)
Download Code
Re: Little things ;)
by IlyaM (Parson) on Dec 13, 2001 at 17:27 UTC
      and

      my $numkeys = (keys %hash);

      is clearer still and not much longer.

      I believe very strongly in clarity when programming,
      "even" in Perl.

      Regards,
      Helgi Briem

        Update: hmm... on further reading, I think the original point was that you don't need the scalar instead of the notion that adding parens would clarify what keys was doing.... so, nevermind.


        Why would the parens make it any clearer? I think you might be misunderstanding whats happening here. From keys:

        In a scalar context [keys], returns the number of keys [in the hash]
        Comparing this to length (since it behaves in a similiar way) which of these is clearer:
        $x = length $y; $x = (length $y);

        It looks like you think (keys %y) behaves like @{[keys %y]}... i.e. return the list of keys, package them up into an array, then return the member count of the array.

        -Blake

Re: Little things ;)
by Masem (Monsignor) on Dec 13, 2001 at 17:28 UTC
    To nit, scalar keys %hash is probably much more easier to understand and may be faster as well (as you don't have to redeclare a new anon array.

    I've been striving to move away from "$#array" to do similar counts in favor of "scalar @array - 1", as the latter is much easier to read, and while I know that perl does not treat the # in $# as a comment starter, I've seen people get confused by that.

    Yes, there are times where 'cute' tricks work better and may help others to improve their programming skills (for example, using map or grep instead of for loops for some tasks), but there are others that border on obfuscation, and should be kept there. I wouldn't call the trick above strictly obfu, but it's not readily understandable and as you say, your super went away head scratching; you gave him a fish, but didn't show him how you got it.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Little things ;)
by MZSanford (Curate) on Dec 13, 2001 at 17:30 UTC
    I have some things that come back to bite me ... a few are :
    # getting just the year $y = (localtime($utc))[5]+1900; # Basic hash un-rolling # (this is an example, better var names in real life) foreach my $a (sort keys %hash) { foreach my $b (sort keys %{ $hash{$a} }) { # co-workers stumble at the line above } } # in an OO module sub print_stats { $_[0]->{fh}->stats(); } # lastly, co-workers are often confussed by : use strict;
    I am sure there are others, but these are ones i have had in the past few weeks.
    $ perl -e 'do() || ! do() ;' Undefined subroutine &main::try

      Perlsonally I think that, if we didn't do things like these from time to time, people would start to realise how easy our job is ;)

      This page is intentionally left justified.

        If we make life harder than it needs to be for us and for the people who follow us, then we don't deserve our jobs.

        There is far more job security in, "So and so gets things done and makes us all look good" than there is in, "Who does that so and so think he is? He thinks he can't be replaced because nobody else will understand what he did!" And, truth be told, this is rightly so. If you place subtle and not so subtle traps for those who would follow you, you are being a liability to those around you.

Re: Little things ;)
by snapdragon (Monk) on Dec 13, 2001 at 17:48 UTC
    Actually that's kinda spooky - something similar literally just happened. My boss asked my to find out how many 404 error messages out site was getting. So I typed in:

    grep '404' error.log | wc

    I gave him the answer and he was curious enough to ask what I'd just typed in meant. So I told him that I'd used a gloabl regular expression parser to look the text '404' and then pipe that output into word count. He just shook his head and said that I was 'such a geek'. Now I'm not offended by that (in fact I think it's kinda a compliment). But still I reckon that my explanation was pretty understandable.

    Oh well back to geeking out I guess :-)


      You could have done this. ;-)     grep -c 404 error.log

      --
      John.

        Or even this:
        perl -pe '$\+=/\b404\b/}{' error_log
        (I should probably attribute fwp-golf hole #3 for the inspiration behind this)

        -Blake

      <tease>
      You can tell your boss you aren't so much a geek.

      The p in grep stands for print, and is a reference to what it does when it matches on a line. (Or at least so Friedl claims in Mastering Regular Expressions.)
      </tease>

      I sort of know what you mean snapdragon,

      There is a profound difference in culture between the two firms I have worked at recently: At the first, calling someone a geek was a major compliment whereas at the second, when I asked who our resident "JavaScript geek" was, I was firmly told by one of the other consultants that the firm does not employ geeks. Oh well...

      Elgon

      "A nerd is someone who knows the difference between a compiled and an interpreted language, whereas a geek is a person who can explain it cogently over a couple of beers"
             - Elgon

Re: Little things ;) (or perhaps something else)
by dws (Chancellor) on Dec 13, 2001 at 22:41 UTC
    I suspect there may be a different lesson in here. Let's take another look at what you wrote.
    My supervisor, who is a good perl programmer himself, asked me if I knew of a way to count the keys in a hash. 'Sure,' I replied and wrote down $#{[keys(%hash)]}+1.

    He studied it for a moment or two, said 'ugh' and wandered off shaking his head in disgust.

    People say perl is unintelligable..... for the life of me I cannot see why ;)

    I was wondering if anyone had a similar story... how the use of a little syntactic gibberish has led to feelings of wonder/amazement/disgust in others (delete as appropriate).

    Without significant contortion of meaning, this can be rewritten as
    By supervisor, a good perl programmer, studied my syntactic gibberish, said 'ugh' and shook his head in disgust.
    Looked at this way, I would suspect that his comment might have been less about Perl than it was about your overcomplexifying what should have been a simple.

    Projecting a bit of my experience into this, a chronic problem that some technical managers have is a tendency of some developers to make simple things complicated (perhaps for the sheer joy of it).

    I recommend you have talk with your supervisor, and get clarity on what the 'ugh' was about.

Re: Little things ;)
by hsmyers (Canon) on Dec 14, 2001 at 08:19 UTC

    All of this makes want to ask a kind of 'Meta' question about language and tools in general.At what point does it become 'gibberish'? If the notion of more than one way to do it is inherent in the language is it gibberish or idomatic ignorance? I'm fairly sure that there is a line (the in the sand type) in there somewhere, but I'm not sure just where…

    –hsm

    "Never try to teach a pig to sing…it wastes your time and it annoys the pig."

      This was more the thing I had in mind when I started this debate. You see, when asked my mindset was heavily into dereferencing due to working on writing a program to extract data from a hashref of a hashref of a hashref. So it was probably mostly due to that that I responded the way I did instead of using the more familiar, and readable, scalar(keys %hash)).

      To me, this is the great advantage, as well as the greatest potential hazard, of using perl. It is easy to write code differently depending on how you are thinking at the time, the phase of the moon, the weather, the day of the week and a million other reasons. What I was interested in seeing was, not who can obfuscate; we have another section for that, but who has written down any of those little syntactic gems that are only possible in a language like perl.

      This page is intentionally left justified.

Re: Little things ;)
by TheDamian (Priest) on Dec 14, 2001 at 14:58 UTC
    I was wondering if anyone had a similar story... how the use of a little syntactic gibberish has led to feelings of wonder/amazement/disgust in others
    Some of us are trying to make an entire career of it! ;-)

      I abase myself at your feet!

      Some of those courses you are talking at look interesting... shame they are on the wrong side of the Atlantic :P

      This page is intentionally left justified.

        I abase myself at your feet!
        Err...please don't. After all, I'm just a fellow monk.
        ...shame they are on the wrong side of the Atlantic!
        Not all of them. I'll also be in Belfast, Zurich, and Bonn next month, as well as London and Munich (and probably a few other European and Asian places) later in the year.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://131605]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2014-09-17 19:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (98 votes), past polls