about return
----ALT--LDGq1n0x1297318063
Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: 8bit
hello,
when in the case "return undef" I prefer just "return" coz in list context it will return an empty list.
my $exist = ...
if ($exist) {
return 1;
} else {
return;
}
the code above can work, but having many lines.
So I want:
return $exist ? 1 : (...);
what should be put in (...) to get the same effect as just "return" (not return undef)?
regards.
----ALT--LDGq1n0x1297318063--
Re: about return
--20cf3054a66d29a7da049be77cf0
Content-Type: text/plain; charset=UTF-8
Is this what you mean
($exist) ? return 1 : return undef
I think even this should work
($exist) ? 1 : 0;
~Parag
2011/2/9 terry peng <terry.peng [at] mail.ru>
>
> hello,
>
> when in the case "return undef" I prefer just "return" coz in list context
> it will return an empty list.
>
> my $exist = ...
> if ($exist) {
> return 1;
> } else {
> return;
> }
>
> the code above can work, but having many lines.
> So I want:
>
> return $exist ? 1 : (...);
>
> what should be put in (...) to get the same effect as just "return" (not
> return undef)?
>
> regards.
>
--20cf3054a66d29a7da049be77cf0--
Re: about return
>>>>> "tp" == terry peng <terry.peng [at] mail.ru> writes:
tp> when in the case "return undef" I prefer just "return" coz in list
tp> context it will return an empty list.
tp> my $exist = ...
tp> if ($exist) {
tp> return 1;
tp> } else {
tp> return;
tp> }
tp> the code above can work, but having many lines.
tp> So I want:
tp> return $exist ? 1 : (...);
tp> what should be put in (...) to get the same effect as just
tp> "return" (not return undef)?
what have you tried? an important point to remember is that return
executes in the context of the call. so [at] foo = bar() would put that
return in list context. ?: will also pass context to its 2 values. so
the question is what is value that in list context will be an empty
list? easy to answer now!
i wouldn't use ?: there anyhow. it is much cleaner to do multiple
returns like this:
return 1 if $exists ;
return ;
then it is easy to see what conditions will return what values. no need
for if/else blocks or noisy ?: ops.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re[2]: about return
>
> return 1 if $exists ;
> return ;
>
> then it is easy to see what conditions will return what values. no need
> for if/else blocks or noisy ?: ops.
>
That's a good solution.
Thanks much. :)
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: about return
>>>>> "PK" == Parag Kalra <paragkalra [at] gmail.com> writes:
PK> Is this what you mean
PK> ($exist) ? return 1 : return undef
PK> I think even this should work
PK> ($exist) ? 1 : 0;
he wants an empty list in a list context when returning the false value
so neither of those works.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: about return
On Feb 9, 10:07=A0pm, terry.p... [at] mail.ru (terry peng) wrote:
> hello,
>
> when in the case "return undef" I prefer just "return" coz in list contex=
t it will return an empty list.
>
> my $exist =3D ...
> if ($exist) {
> =A0 =A0 return 1;
>
> } else {
> =A0 =A0 return;
> }
>
> the code above can work, but having many lines.
> So I want:
>
> return $exist ? 1 : (...);
>
> what should be put in (...) to get the same effect as just "return" (not =
return undef)?
>
return $exist ? 1 : ();
--
Charles DeRykus
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re[2]: about return
Wed, 9 Feb 2011 22:44:10 -0800 (PST) письмо от "C.DeRykus" <derykus [at] gmail.com>:
> On Feb 9, 10:07 pm, terry.p... [at] mail.ru (terry peng) wrote:
> > hello,
> >
> > when in the case "return undef" I prefer just "return" coz in list context
> it will return an empty list.
> >
> > my $exist = ...
> > if ($exist) {
> > return 1;
> >
> > } else {
> > return;
> > }
> >
> > the code above can work, but having many lines.
> > So I want:
> >
> > return $exist ? 1 : (...);
> >
> > what should be put in (...) to get the same effect as just "return" (not
> return undef)?
> >
>
> return $exist ? 1 : ();
>
Does return () in the caller's list context will return an undef?
regards.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: about return
On Thursday 10 Feb 2011 09:13:34 terry peng wrote:
> Wed, 9 Feb 2011 22:44:10 -0800 (PST) =D0=C9=D3=D8=CD=CF =CF=D4 "C.DeRykus=
"
<derykus [at] gmail.com>:
> > On Feb 9, 10:07 pm, terry.p... [at] mail.ru (terry peng) wrote:
> > > hello,
> > >
> > > when in the case "return undef" I prefer just "return" coz in list
> > > context
> >
> > it will return an empty list.
> >
> > > my $exist =3D ...
> > > if ($exist) {
> > >
> > > return 1;
> > >
> > > } else {
> > >
> > > return;
> > >
> > > }
> > >
> > > the code above can work, but having many lines.
> > > So I want:
> > >
> > > return $exist ? 1 : (...);
> > >
> > > what should be put in (...) to get the same effect as just "return"
> > > (not
> >
> > return undef)?
> >
> >
> > return $exist ? 1 : ();
>
> Does return () in the caller's list context will return an undef?
>
In List context it will return an empty list. You may have meant in scalar=
context in which case:
[code]
#!/usr/bin/perl
use strict;
use warnings;
sub mysub
{
my $verdict =3D shift;
return $verdict ? 1 : ();
}
my $foo =3D mysub(0);
print +((!defined($foo)) ? "(undef)\n" : "$foo\n");
[/code]
[output]
(undef)
[/output]
Regards,
Shlomi Fish
=2D-
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/
Chuck Norris can make the statement "This statement is false" a true one.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re[2]: about return
Thu, 10 Feb 2011 10:04:55 +0200 письмо от Shlomi Fish <shlomif [at] iglu.org.il>:
>
> In List context it will return an empty list. You may have meant in scalar
> context in which case:
>
>
Sorry , my typo.
I did mean the scalar context.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
RE: Re[2]: about return
Terry,
Here is my understanding:
You can try defined function to verify the r eturn value=
in List context, you can find the return value is =
(undef) instead of undef once the condition is false. B=
ecause you are using [at] result=3D&yoursubName instea d of $res=
ult=3D&yoursubName.
Please correct me if I were wrong.
Just refer to chapter 3.7 in "Learning Perl"=2 04th Editio=
n.
Thanks & Regards,
Alex Wang
-----Original Message-----
From: terry peng [mailto:terry.peng [at] mail.ru]
Sent: Thursday, February 10, 2011 3:14 PM
To: beginners
Subject: Re[2]: about return
Wed, 9 Feb 2011 22:44:10 -0800 (PST) =D0=C9=D3 =D8=CD=CF =
=CF=D4 "C.DeRykus" <derykus [at] gmail.com>:
> On Feb 9, 10:07 pm, terry.p... [at] mail.ru (terr y peng) w=
rote:
> > hello,
> >
> > when in the case "return undef" I pref er just "=
return" coz in list context
> it will return an empty list.
> >
> > my $exist =3D ...
> > if ($exist) {
> > return 1;
> >
> > } else {
> > return;
> > }
> >
> > the code above can work, but having ma ny lines.
> > So I want:
> >
> > return $exist ? 1 : (...);
> >
> > what should be put in (...) to get t he same e=
ffect as just "return" (not
> return undef)?
> >
>
> return $exist ? 1 : ();
>
Does return () in the caller's list context will return=
an undef?
regards.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl .org
http://learn.perl.org/
**************** CAUTION - Disclaimer ************** ***
This e-mail contains PRIVILEGED AND CONFIDENTIAL=2 0INFORMATION=
intended solely
for the use of the addressee(s). If you are= 20not the=
intended recipient, please
notify the sender by e-mail and delete the o riginal mes=
sage. Further, you are not
to copy, disclose, or distribute this e-mail o r its con=
tents to any other person and
any such actions are unlawful. This e-mail may contain =
viruses. Infosys has taken
every reasonable precaution to minimize this ris k, but is=
not liable for any damage
you may sustain as a result of any virus i n this e-=
mail. You should carry out your
own virus checks before opening the e-mail or= 20attachment.=
Infosys reserves the
right to monitor and review the content of a ll messages=
sent to or from this e-mail
address. Messages sent to or from this e-mail= 20address m=
ay be stored on the
Infosys e-mail system.
***INFOSYS******** End of Disclaimer ********INFOSYS ***
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: about return
>>>>> "AW" == Alex Wang02 <Alex_Wang02 [at] infosys.com> writes:
AW> Terry,
AW> Here is my understanding:
AW> You can try defined function to verify the return value in List
AW> context, you can find the return value is (undef) instead of undef
AW> once the condition is false. Because you are using
AW> [at] result=&yoursubName instead of $result=&yoursubName.
that is wrong. a plain return in list context returns an empty
list. calling defined on that makes no sense. you test that by checking
the length of the returned list (put the array in scalar context).
look at this example closely:
sub foo { return }
sub bar { return undef }
my $has_foo = foo() ;
my [at] has_foos = foo() ;
my $has_bar = bar() ;
my [at] has_bars = bar() ;
my ( $foo ) = foo() ;
my ( $bar ) = bar() ;
my ( $foo2 ) = bar() ;
my ( $bar2 ) = foo() ;
now tell me what values will be in each of the vars? use Data::Dumper to
verify your answers. the final question is can you tell what kind of
return was executed in the last four cases? note that i swapped the
calls in the last two. this is tricky and subtle and very important to
know.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: about return
--0-2088184396-1297392469=:95508
Content-Type: text/plain; charset=us-ascii
From: terry peng <terry.peng [at] mail.ru>
To: beginners <beginners [at] perl.org>
Sent: Thu, February 10, 2011 2:07:43 PM
Subject: about return
>hello,
>
>when in the case "return undef" I prefer just "return" coz in list context it
>will return an empty list.
>
>my $exist = ...
>if ($exist) {
> return 1;
>} else {
> return;
>}
>
>the code above can work, but having many lines.
>So I want:
>
>return $exist ? 1 : (...);
>
>what should be put in (...) to get the same effect as just "return" (not return
>undef)?
>
>regards.
Will this work for you, Terry?
my $exist = ......
$exist ? return 1: return;
Rgds.
--0-2088184396-1297392469=:95508--