http://www.perlmonks.org?node_id=973022


in reply to What operator should perl5porters use for safe dereferencing?

Meh!

I would have really liked a NO poll option

I voted  ->> but  ~> also looks about the same, plus its one char shorter

Even if this does get introduced, I'll be using  my $foo = eval { ro->sham->bo->die };

dor is actuall usefuly ( //= ) but this, not so much, eval{} works

Replies are listed 'Best First'.
Re^2: What operator should perl5porters use for safe dereferencing?
by Jenda (Abbot) on May 29, 2012 at 14:49 UTC

    eval{} surely works. But it works too well. Far too well! It'll happily eat any exception whatsoever. Sure you will be able to tell whether it's the kind of exception that happens when calling a method on an undef, but will it be one of the calls you have here or is it somewhere deep down within one of the methods?

    <!-- Node text goes above. Div tags should contain sig only - ->

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      eval{} surely works. But it works too well. Far too well! It'll happily eat any exception whatsoever. Sure you will be able to tell whether it's the kind of exception that happens when calling a method on an undef, but will it be one of the calls you have here or is it somewhere deep down within one of the methods?

      For me, I don't see that as a problem, either I want exceptions, or I want to ignore them

      I doubt there exists a situation where I would want to ignore only this particular exception:

      Can't call method "%" on an undefined value

      But if there was, I'm sure I'd use one of cpan's exception filtering modules instead of  ~>

        I doubt there exists a situation where I would want to ignore all exceptions.

        There do exist situations in which I will want to catch and log any exception and move on to the next task though, but this is a different thing. There's a huge difference between being able to shorten

        my $age = do { my $boss = $company->boss(); my $wife = $boss ? $boss->wife() : undef; $wife ? $wife->age() : undef };
        to just
        my $age = $company~>boss()~>wife()~>age();
        and just ignoring anything that might happen while getting that info. Because, you know, if the company has no boss or the boss has no wife, then the age is actually undefined. On the other hand if there was a problem loading the data from the database or where ever, I want to know, because then the value is not undefined. It could not be "computed".

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re^2: What operator should perl5porters use for safe dereferencing?
by de-merphq (Beadle) on May 29, 2012 at 13:44 UTC
    Ill add a "none" option. Anyway, stick with eval if you want, me personally I think using eval for this purpose is pretty close to using a nuke to swat a fly, and about as efficient.
    ---
    alter ego of demerphq

      How about a "No clue - I don't even know what this is about" for the dummies among us? ;-)

      Anyway, stick with eval if you want, me personally I think using eval for this purpose is pretty close to using a nuke to swat a fly, and about as efficient.

      lol, are you wearing EPIC colored glasses or something?

        Looks to me like you didn't understand my reply at all...

        ---
        alter ego of demerphq
Re^2: What operator should perl5porters use for safe dereferencing?
by ikegami (Patriarch) on May 30, 2012 at 16:02 UTC

    eval is not equivalent. Not even close. While it does the job, it does significant collateral damage in the process, so it doesn't work at all as far as I'm concerned.

    I'm not saying we need this feature. I'm simply saying "just use eval" is wrong.

      eval is not equivalent. Not even close. While it does the job, it does significant collateral damage in the process, so it doesn't work at all as far as I'm concerned.

      wow, you're rally onto something

Re^2: What operator should perl5porters use for safe dereferencing?
by Anonymous Monk on Jun 04, 2012 at 04:51 UTC
    ~> is less characters, but is more travel. I like ->> because it's the same input as -> but with the last character repeated.

    ~> also looks too similar to -> on some fonts at a glance, so ->> has the advantage of being shaped differently. Of course it does have the disadvantage that a junior might think it's a typo and 'fix' it.

      I agree,
      ~> and
      -> are just too similar.

      Depending on the font (and you can not always choose the one you like, especially online), you really have to look hard. I pretty much guarantee that code audits as well as helping out on SoPW will get a lot harder.

      As for the typing effort, a character more or less really doesn't matter. Those of us who use non-english keyboards (so, most of us really) already have a hard time coding Perl and other programming languages. Some examples from my german keyboard:

      • ~ is (Alt Gr)(+)
      • \ is (Alt Gr)(ß)
      • { is (Alt Gr)(7)
      • [ is (Alt Gr)(8)

      In effect, a lot of the "standard" metacharacters require awkward right-hand-only acrobatics. I thought about using an english/american keyboard, but since i also require german umlauts that would just shift the problem. Just changing the keyboard layout in software is also a bit awkward, since then the printing on the keys does not match what i type (which i find rather confusing).

      In short, i don't care if i have to type two or three characters, since something like ?-> is easier to read and harder to overlook (and in this case actually easier to type on my german keyboard as well).

      And, from my personal view, i always associate ~ operations with some kind of regular expression stuff, whereas ? does not.

      "You have reached the Monastery. All our helpdesk monks are busy at the moment. Please press "1" to instantly donate 10 currency units for a good cause or press "2" to hang up. Or you can dial "12" to get connected directly to second level support."