Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

What does this mean?

by fritz1968 (Sexton)
on Aug 20, 2012 at 19:46 UTC ( #988494=perlquestion: print w/ replies, xml ) Need Help??
fritz1968 has asked for the wisdom of the Perl Monks concerning the following question:

I get to troubleshoot someone else's code. (yeah!). Anyhoo, I am trying to make sense of the following code:

package my_wsdl; sub new { my $class = shift; my $self = { _id => shift, _client => shift, _externalID => shift, _externalParentId => shift, _provider => shift, _action => shift, _priority => shift, _actionTime => shift, _state => shift, _result => shift, _locked => shift, _lockedTimes => shift, _sid => shift, _domain => shift, _samAccountName => shift, }; bless $self, $class; return $self; } sub setID { my ( $self, $id ) = @_; $self->{_id} = $id if defined($id); return $self->{_id};

Specifically, in human terms, what does the following line mean?

$self->{_id} = $id if defined($id);
Thanks, Frank

Comment on What does this mean?
Select or Download Code
Re: What does this mean?
by moritz (Cardinal) on Aug 20, 2012 at 19:51 UTC
Re: What does this mean?
by Perlbotics (Abbot) on Aug 20, 2012 at 19:55 UTC

    It's a common setter/getter idiom however with an unfortunate chosen method name setID.

    'get' attribute: no argument given, so $id is undefined, current value of $self->{_id} is returned

    my $id = $obj->setID(); # return current _id
    'set' attribute (and 'get' new value at the same time)
    my $new_id = $obj->setID( 42 ); # updates and returns new _id 42 # or just: $obj->setID( 42 );

      I don't know if it was necessarily intended to act as a getter+setter. It's probably just a setter that happens to include a quick check to prevent people from setting the ID to undef. It happens to also return the current ID, but I'd assume that's just incidental. A method needs to return something; might as well be something vaguely useful.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: What does this mean?
by Anonymous Monk on Aug 20, 2012 at 20:05 UTC

    You could have written it this way:

    if (defined($id)) { $self->{_id} = $id; }
    ... but this way's shorter. Perl is full of little "shortcuts" like that.
Re: What does this mean?
by linuxkid (Sexton) on Aug 20, 2012 at 21:00 UTC

    if ya want it even shorter, try $self->{_id} = defined($id) ? $id : $self->{_id}

    --linuxkid


    imrunningoutofideas.co.cc

      If by "shorter," you mean 14 characters longer, then absolutely.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

      defined $id and $$self{_id} = $id; or with newer perls $$self{_id} //= $id;
        $$self{_id} //= $id; checks the definedness of $$self{_id}, not of $id (as the other suggested snippets do), so it does something completely different (never reset the attribute, instead of prevent assignment of an undefined id).
Re: What does this mean?
by bulk88 (Priest) on Aug 20, 2012 at 22:28 UTC
    A statement "if" is not an operator or expression. Think of it as a block control keyword. Its precedence is dead last, below a ',', '=', '(', '[', '{', and '=>'. Last I played with it, if you try to treat it like an expression op, it will syntax error on you. If you see a statement if, in your head replace the "X if Y" with psuedo code "{X} if (Y)", any existing commas and parenthesis are irrelevant.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (21)
As of 2014-07-30 13:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (233 votes), past polls