Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: What operator should perl5porters use for safe dereferencing?

by ForgotPasswordAgain (Priest)
on May 29, 2012 at 14:21 UTC ( #973034=note: print w/replies, xml ) Need Help??

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

Does "None" include making -> do this already itself?

Replies are listed 'Best First'.
Re^2: What operator should perl5porters use for safe dereferencing?
by SuicideJunkie (Vicar) on May 29, 2012 at 15:07 UTC

    I'm not sure that the usage would be common enough. A lot of the time, you'd want to know and fix the problem.

    Perhaps have -> do safe dereferencing, but throw a warning?

Re^2: What operator should perl5porters use for safe dereferencing?
by pemungkah (Priest) on May 31, 2012 at 01:41 UTC
    Yes, and that's an alternative that's been proposed (by me and independently by others). The consensus is that it could be done using autobox. That's my personal preference, with the option to turn off a warning for it:
    $z = $not_defined->arbitrarymethod() Method not called at ...
    I personally think that's preferable to a whole new operator; the idea is to make it work in much the same way that sending messages to nil does in Objective-C: it does nothing, and returns nil (which equivalences to 0 in numeric context). For Perl, it'd do nothing and return undef (in list context it'd return an empty list).

      The thing is, I would want to use this in a scope as limited as possible. Which means I would end up with:

      my $object = $other_object->get_object; my $result = do { use safederef; $object->possible_method; };

      At which point I could basically write

      my $object = $other_object->get_object; my $result = $object ? $object->possible_method : undef;

      Which would mean we'd end up with a best-case scenario where the traditional way needs less code to provide the same. Compare this to:

      my $result = $other_object?->get_object?->possible_method;

      Ordinary morality is for ordinary people. -- Aleister Crowley
        I'd argue, as you do, that using it in a limited scope makes it not very worthwhile - and that's why I'd use it everywhere. With an added pragma to allow us to warn on messages to NIL, I think it'd be quite reasonable to do that.

        Consider: the objective of this is to eliminate code by making message-to-NIL safe. Adding the code to only use it certain places is essentially the same as saying if (defined $foo) and the point was to not need that.

        I'd say that if you feel uncomfortable with jumping out of the plane with this chute, so to speak, this approach isn't for you. I've used it a lot in Objective-C programming and I find it really simplifies things, but much as using $_ as the default variable, either it really works for you or it doesn't work at all.

      I've been using Perl for a while and with Perl's 'do what you want' it would seem that there shouldn't be a new operator. Returning undef seems fine with a warning
        I think it's mostly getting used to the different semantics. I love how much more compact the code is in Objective-C when I don't have to worry about sending messages to undef. But I can equally understand the desire to have that throw an exception. The "I'll add a new operator to provide this" seems like a harder way to have both options - but it does specifically call out "In this case, I am explicitly deciding to do safe deref", so...

        I'l try to get something working so it can at least be tried.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://973034]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2021-10-25 14:26 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (89 votes). Check out past polls.