Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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 !!!!

Comment on Question about defined loop
Download Code
Re: Question about defined loop
by mje (Deacon) 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';
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 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"; }
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-08-21 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (128 votes), past polls