Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Question about defined loop

by siddheshsawant (Sexton)
on Mar 11, 2010 at 16:58 UTC ( [id://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 .....
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Question about defined loop
by ikegami (Patriarch) 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"; }
    or
    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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-19 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found