[24331] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 6520 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 5 03:10:34 2004

Date: Wed, 5 May 2004 00:10:15 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 5 May 2004     Volume: 10 Number: 6520

Today's topics:
        Newbie question, logical operators <zturner_NOSPAM_0826@hotmail.com>
    Re: Newbie question, logical operators <xxala_qumsiehxx@xxyahooxx.com>
    Re: Newbie question, logical operators (Sam Holden)
    Re: Newbie question, logical operators <zturner_NOSPAM_0826@hotmail.com>
    Re: Newbie question, logical operators <jtc@shell.dimensional.com>
    Re: Newbie question, logical operators (Sam Holden)
    Re: Newbie question, logical operators <jtc@shell.dimensional.com>
    Re: Newbie question, logical operators <uri@stemsystems.com>
    Re: Perl Embedding Question <titus@nospam.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 05 May 2004 05:04:40 GMT
From: "Zachary Turner" <zturner_NOSPAM_0826@hotmail.com>
Subject: Newbie question, logical operators
Message-Id: <IB_lc.55213$hR1.36895@fe2.texas.rr.com>

Coming from a C++ background, I'm very familiar with logical operators, but
why in Perl do they work the way they do?  It doesn't seem like the
operators are logically the same.  Take an example: a = 0, b = 10.

C++:
    a || b returns true
    a && b returns false

Perl:
    $a || $b returns $b
    $a && $b returns $a

Now let a=5, b=10

C++:
    a || b returns true
    a && b returns true

Perl:
    $a || $b returns $a
    $a && $b returns $b

So in these two situations, we can see there is a difference in the two.  I
can see how logically speaking the operators WORK, but what is the reason
for returning the value?  It seems like you can't ever know what you're
getting back.  Can someone give me a non-trivial example of a function that
uses this feature in a clever way?  By non-trivial I don't mean implement a
fully functional web service, just something other than a 1 line test that
simply prints the return value of the operator.  Something that you can do
shorter and more easily in Perl than in, say, C++ because of this feature.

Thanks
Zach




------------------------------

Date: Wed, 05 May 2004 05:20:00 GMT
From: Ala Qumsieh <xxala_qumsiehxx@xxyahooxx.com>
Subject: Re: Newbie question, logical operators
Message-Id: <4Q_lc.45162$jC3.28888@newssvr29.news.prodigy.com>

Zachary Turner wrote:

> Coming from a C++ background, I'm very familiar with logical operators, but
> why in Perl do they work the way they do?  It doesn't seem like the
> operators are logically the same.  Take an example: a = 0, b = 10.

Why should they be? Perl is not C++. I don't see why Perl should conform 
to your C++ expectations since it is a different language. Yes, the 
logical operators are similar, and they look the same, but they are NOT 
identical.

Anyway, this behavior is referred to as 'short-circuiting' and is 
described very well in perlop. Perl's approach is more useful, IMHO, 
than C's.

Note that for the purposes of testing truth/falsehood, they are both 
indentical:

> C++:
>     a || b returns true
>     a && b returns false
> 
> Perl:
>     $a || $b returns $b

which is true (10).

>     $a && $b returns $a

which is false (0).

> Now let a=5, b=10
> 
> C++:
>     a || b returns true
>     a && b returns true
> 
> Perl:
>     $a || $b returns $a

which is true (5).

>     $a && $b returns $b

which is true (10).

> So in these two situations, we can see there is a difference in the two.  I
> can see how logically speaking the operators WORK, but what is the reason
> for returning the value?  It seems like you can't ever know what you're
> getting back. Can someone give me a non-trivial example of a function that
> uses this feature in a clever way?  By non-trivial I don't mean implement a
> fully functional web service, just something other than a 1 line test that
> simply prints the return value of the operator.  Something that you can do
> shorter and more easily in Perl than in, say, C++ because of this feature.

Sure. There are many common idioms that are used. Some examples:

1.  open(FH, $file) || die "Can't open $file: $!\n";
2.  my $val = $ENV{USER} || 'nobody';
3.  defined($x) && print "X = $x";
4.  my $arg = shift || 'default_value';

I'm sure others can cook up many more interesting ones too.

--Ala



------------------------------

Date: 5 May 2004 05:29:20 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: Newbie question, logical operators
Message-Id: <slrnc9gutg.rha.sholden@flexal.cs.usyd.edu.au>

On Wed, 05 May 2004 05:04:40 GMT,
	Zachary Turner <zturner_NOSPAM_0826@hotmail.com> wrote:
> Coming from a C++ background, I'm very familiar with logical operators, but
> why in Perl do they work the way they do?  It doesn't seem like the
> operators are logically the same.  Take an example: a = 0, b = 10.
> 
> C++:
>     a || b returns true
>     a && b returns false
> 
> Perl:
>     $a || $b returns $b
>     $a && $b returns $a
> 
> Now let a=5, b=10
> 
> C++:
>     a || b returns true
>     a && b returns true
> 
> Perl:
>     $a || $b returns $a
>     $a && $b returns $b
> 
> So in these two situations, we can see there is a difference in the two.  I

They aren't different, perl just encodes more information in the
result. In the first example $a is false and $b is true so the result
is equivalent to the C++ result, with some extra information included
free of charge.


> can see how logically speaking the operators WORK, but what is the reason
> for returning the value?  It seems like you can't ever know what you're
> getting back.  Can someone give me a non-trivial example of a function that
> uses this feature in a clever way?  By non-trivial I don't mean implement a
> fully functional web service, just something other than a 1 line test that
> simply prints the return value of the operator.  Something that you can do
> shorter and more easily in Perl than in, say, C++ because of this feature.

The usual use:

my $value = $value_from_user || $default_value;

Such as:

use CGI ':standard';

# ...
my $pagenumber = param('page') || 1;
# ...

To display the first page when no page parameter exists, of course 
this also displays page 1 when there is a page parameter and it has
a value of 0 or ''.

or:

$PAGER = $ENV{'PAGER'} || 'more';

To use a user specified pager, defaulting to "more". Again if the
user wants to specify a pager named '0' they are out of luck
(well they can use /usr/local/bin/0 in reality if they have such a 
strangely named pager).

-- 
Sam Holden


------------------------------

Date: Wed, 05 May 2004 05:33:05 GMT
From: "Zachary Turner" <zturner_NOSPAM_0826@hotmail.com>
Subject: Re: Newbie question, logical operators
Message-Id: <l0%lc.55249$hR1.10533@fe2.texas.rr.com>


"Ala Qumsieh" <xxala_qumsiehxx@xxyahooxx.com> wrote in message
news:4Q_lc.45162$jC3.28888@newssvr29.news.prodigy.com...
> Zachary Turner wrote:
>
> > Coming from a C++ background, I'm very familiar with logical operators,
but
> > why in Perl do they work the way they do?  It doesn't seem like the
> > operators are logically the same.  Take an example: a = 0, b = 10.
>
> Why should they be? Perl is not C++. I don't see why Perl should conform
> to your C++ expectations since it is a different language. Yes, the
> logical operators are similar, and they look the same, but they are NOT
> identical.

Sigh...  I didn't imply that it SHOULD conform to my "C++ expectations", it
was just something that surprised me and I couldn't think off the top of my
head of how it might be useful, although I knew that there were probably
many common idioms using it.  Hence I posted to the newsgroup, pointing out
that I realize they are logically equivalent, I just wanted an example of
how it's useful.

> Sure. There are many common idioms that are used. Some examples:
>
> 1.  open(FH, $file) || die "Can't open $file: $!\n";
> 2.  my $val = $ENV{USER} || 'nobody';
> 3.  defined($x) && print "X = $x";
> 4.  my $arg = shift || 'default_value';
Thanks.
Can you come up with one or two using && that set the result equal to
something and then use that result in some way?

Zach




------------------------------

Date: 4 May 2004 23:33:32 -0600
From: Jim Cochrane <jtc@shell.dimensional.com>
Subject: Re: Newbie question, logical operators
Message-Id: <slrnc9gv5c.r31.jtc@shell.dimensional.com>

In article <IB_lc.55213$hR1.36895@fe2.texas.rr.com>, Zachary Turner wrote:
> Coming from a C++ background, I'm very familiar with logical operators, but
> why in Perl do they work the way they do?  It doesn't seem like the
> operators are logically the same.  Take an example: a = 0, b = 10.
> 
> C++:
>     a || b returns true
>     a && b returns false
> 
> Perl:
>     $a || $b returns $b
>     $a && $b returns $a
> 
> Now let a=5, b=10
> 
> C++:
>     a || b returns true
>     a && b returns true
> 
> Perl:
>     $a || $b returns $a
>     $a && $b returns $b
> 
> So in these two situations, we can see there is a difference in the two.  I
> can see how logically speaking the operators WORK, but what is the reason
> for returning the value?  It seems like you can't ever know what you're

I don't think this will answer all of your questions, but, first, I think
it's more accurate to say:

with: my ($a, $b) = (0, 10):

     $a || $b returns 10 (b's value)
     $a && $b returns 0 (a's value)

etc.

Think in terms of the short-circuit functionality of || and && (which C++
also specifies, btw) - with: my ($a, $b) = (5, 10):

     $a || $b  # You get a's value because the rules say that if the left
               # expression evaluates to non-0, the right side is not
               # evaluated.
     $a && $b  # You get b's value because the rules say that if the left
               # expression evaluates to non-0, the right side must be
               # evaluated.

Combine this with the rule that Perl says the resulting value is that of
the last expression evaluated and the result, I believe, makes complete
sense.  For example, with $a || $b it doesn't make sense to get $b because
$b is never evaluated.

> getting back.  Can someone give me a non-trivial example of a function that
> uses this feature in a clever way?  By non-trivial I don't mean implement a
> fully functional web service, just something other than a 1 line test that
> simply prints the return value of the operator.  Something that you can do
> shorter and more easily in Perl than in, say, C++ because of this feature.
> 
> Thanks
> Zach
> 
> 


-- 
Jim Cochrane; jtc@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]


------------------------------

Date: 5 May 2004 05:37:45 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: Newbie question, logical operators
Message-Id: <slrnc9gvd9.rha.sholden@flexal.cs.usyd.edu.au>

On Wed, 05 May 2004 05:20:00 GMT,
	Ala Qumsieh <xxala_qumsiehxx@xxyahooxx.com> wrote:

[snip perl's && and || operators returning one of their arguments rather
than a simple true/false like C++ and what use that has...]

> 
> Sure. There are many common idioms that are used. Some examples:
> 
> 1.  open(FH, $file) || die "Can't open $file: $!\n";
> 2.  my $val = $ENV{USER} || 'nobody';
> 3.  defined($x) && print "X = $x";
> 4.  my $arg = shift || 'default_value';
> 
> I'm sure others can cook up many more interesting ones too.

Examples 1 and 3 use short circuiting and have nothing to do
with what is actually returned by && and || (that the result 
isn't used anywhere is the big hint of this :)

Number 3, in particular is a (in my experience) far more common in
C++ than in perl - C++ after all explodes if you dereference a null
pointer and hence :

	if (a_pointer && [some operation on *a_pointer])

if very common, OK maybe more so in C...

I'd write 3. as

	print "X = $x" if defined $x;

in perl, for example.

-- 
Sam Holden


------------------------------

Date: 4 May 2004 23:42:45 -0600
From: Jim Cochrane <jtc@shell.dimensional.com>
Subject: Re: Newbie question, logical operators
Message-Id: <slrnc9gvml.r31.jtc@shell.dimensional.com>

In article <4Q_lc.45162$jC3.28888@newssvr29.news.prodigy.com>, Ala Qumsieh wrote:
> Zachary Turner wrote:
> 
>> Coming from a C++ background, I'm very familiar with logical operators, but
>> why in Perl do they work the way they do?  It doesn't seem like the
>> operators are logically the same.  Take an example: a = 0, b = 10.
> 
> Why should they be? Perl is not C++. I don't see why Perl should conform 
> to your C++ expectations since it is a different language. Yes, the 
> logical operators are similar, and they look the same, but they are NOT 
> identical.
> 
> Anyway, this behavior is referred to as 'short-circuiting' and is 
> described very well in perlop. Perl's approach is more useful, IMHO, 
> than C's.

C's and Perl's approaches are the same, in the sense that C also short
circuits || and &&; but perhaps you were referring to something else.

-- 
Jim Cochrane; jtc@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]


------------------------------

Date: Wed, 05 May 2004 05:46:54 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Newbie question, logical operators
Message-Id: <x7brl38lyp.fsf@mail.sysarch.com>

>>>>> "ZT" == Zachary Turner <zturner_NOSPAM_0826@hotmail.com> writes:

  ZT> Can you come up with one or two using && that set the result equal to
  ZT> something and then use that result in some way?

from the soon to be released Sort::Maker:

	$key->{'no_case'} ||= !$key->{'case'} && $opts->{'no_case'} ;

this sets the no_case flag in the key if it has not been set. if the
case flag is not set (!$key->{'case'} will be true) it uses the default
no_case from %opts

i have used && elsewhere. || is by far the more common one in value
stuff. i use both for straight boolean stuff.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Wed, 05 May 2004 03:49:12 GMT
From: "Steve Titus" <titus@nospam.com>
Subject: Re: Perl Embedding Question
Message-Id: <YuZlc.3803$zq4.249602@twister.southeast.rr.com>

Ben,

Thanks very much for your help. Your post helped me out a bunch. I decided
to go with the following approach: I downloaded and built a version of Perl
(5.8.4) statically for use with my project, so that my library would always
be using a consistent version of Perl. When I built the executable, I linked
against this perl. This solves all issues except the GLIBC issue; for that,
I will just build two different versions of executable (for use on the 2
common glibc versions on boxes I am using). The static linking is
regrettable, but allows use of a consistent Perl version, and use of app in
cases when Perl is not installed on box, which is nice.



Steve




------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 6520
***************************************


home help back first fref pref prev next nref lref last post