Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Question about defined loop

by siddheshsawant (Sexton)
on Mar 11, 2010 at 16:58 UTC ( #828085=perlquestion: print w/replies, xml ) Need Help??
siddheshsawant has asked for the wisdom of the Perl Monks concerning the following question:

I guys, I have a question about perl's defined loop.I have written a following code:

sub not_exposed_reason { my ($self, $not_exposed_reason) = @_; $self->{_not_exposed_reason} = $not_exposed_reason if defined($not +_exposed_reason); return $self->{_not_exposed_reason}; }

When there is no exposed reason I wants to return "No exposed reason" string. My question how to return it?

Kindly let me know...Thanks in advance !!!!

Replies are listed 'Best First'.
Re: Question about defined loop
by ssandv (Hermit) on Mar 11, 2010 at 17:13 UTC

    defined is not a loop, it is a function. That may sound pedantic, but it's difficult to help people who use words wrong, because it's hard to understand them.

    One of many ways to do what (I *think*) you want would be to make the last line
    return defined($not_exposed_reason)?$self->{_not_exposed_reason}:"No e +xposed reason";
    which uses the ? : construct (sometimes called the ternary conditional operator) to produce the value before the : if the condition before the ? is true, and the one after the : if the condition is false. I'm not entirely sure how helpful that is, because your question is pretty hard to parse.

      Thanks a lot dude!!!!!Thats helped out .....
        please to learn english. Thanks a lot dude !!!! Thanks in advance !!!!
Re: Question about defined loop
by ikegami (Pope) on Mar 11, 2010 at 17:28 UTC
    Depending on when you want to set $self->{_not_exposed_reason},
    sub not_exposed_reason { my ($self, $not_exposed_reason) = @_; return $self->{_not_exposed_reason} = defined($not_exposed_reason) ? $not_exposed_reason : "No exposed reason"; }
    sub not_exposed_reason { my ($self, $not_exposed_reason) = @_; if (defined($not_exposed_reason)) { return $self->{_not_exposed_reason} = $not_exposed_reason; } else { return "No exposed reason"; } }

    Note that

    defined(X) ? X : Y
    can be written as
    X // Y
    since Perl 5.10.
Re: Question about defined loop
by JavaFan (Canon) on Mar 11, 2010 at 17:10 UTC
    I'm not quite sure I understand your question. Perhaps you want:
    sub not_exposed_reason { my ($self, $not_exposed_reason) = @_; $self->{_not_exposed_reason} = $not_exposed_reason // "No exposed +reason"; }
    On older Perls, you may want to write that as:
    sub not_exposed_reason { my ($self, $not_exposed_reason) = @_; $self->{_not_exposed_reason} = defined $not_exposed_reason ? $not_ +exposed_reason : "No exposed reason"; }
Re: Question about defined loop
by mje (Curate) on Mar 11, 2010 at 17:04 UTC
    return defined($not_exposed_reason) || 'No exposed reason';
      That returns 1 if $not_exposed_reason is defined. I doubt that's what the OP wants.

        Oops, you are of course right.

        return $not_exposed_reason // 'No exposed reason';

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://828085]
Approved by lostjimmy
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2017-01-21 20:27 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (185 votes). Check out past polls.