[28865] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 109 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 9 22:31:08 2007

Date: Fri, 9 Feb 2007 19:30:35 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 9 Feb 2007     Volume: 11 Number: 109

Today's topics:
        modifying a variable twice in a statement (comparison t <ivan.d.novick@gmail.com>
    Re: modifying a variable twice in a statement (comparis <1usa@llenroc.ude.invalid>
    Re: modifying a variable twice in a statement (comparis <ivan.d.novick@gmail.com>
    Re: modifying a variable twice in a statement (comparis <1usa@llenroc.ude.invalid>
    Re: modifying a variable twice in a statement (comparis <cwilbur@chromatico.net>
    Re: modifying a variable twice in a statement (comparis <evad.notyals@liamg.moc>
    Re: modifying a variable twice in a statement (comparis <nobull67@gmail.com>
    Re: modifying a variable twice in a statement (comparis <hjp-usenet2@hjp.at>
    Re: modifying a variable twice in a statement (comparis <uri@stemsystems.com>
    Re: modifying a variable twice in a statement (comparis <bik.mido@tiscalinet.it>
    Re: modifying a variable twice in a statement (comparis <ben.usenet@bsb.me.uk>
    Re: modifying a variable twice in a statement (comparis <ivan.d.novick@gmail.com>
    Re: modifying a variable twice in a statement (comparis <jurgenex@hotmail.com>
    Re: modifying a variable twice in a statement (comparis <uri@stemsystems.com>
    Re: modifying a variable twice in a statement (comparis <cwilbur@chromatico.net>
    Re: modifying a variable twice in a statement (comparis <jgibson@mail.arc.nasa.gov>
    Re: modifying a variable twice in a statement (comparis <abigail@abigail.be>
    Re: modifying a variable twice in a statement (comparis <abigail@abigail.be>
    Re: modifying a variable twice in a statement (comparis <jgibson@mail.arc.nasa.gov>
    Re: modifying a variable twice in a statement (comparis <abigail@abigail.be>
    Re: modifying a variable twice in a statement (comparis <hjp-usenet2@hjp.at>
    Re: modifying a variable twice in a statement (comparis <wahab-mail@gmx.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 4 Feb 2007 09:48:47 -0800
From: "Ivan Novick" <ivan.d.novick@gmail.com>
Subject: modifying a variable twice in a statement (comparison to c)
Message-Id: <1170611327.022503.293050@j27g2000cwj.googlegroups.com>

Hi,

In C, you are not allowed to modify a variable 2 times in one
statement.

In perl, this code does not complain at all (similar code in C is
illegal).

Can someone varify that this is legal perl syntax?

####################################
#!/usr/bin/perl
use strict;
use warnings;

my $test = 1;

$test = ++$test;

print "$test\n";
####################################

Thanks,
Ivan
http://www.0x4849.net



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

Date: Sun, 04 Feb 2007 18:03:05 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <Xns98CD850905705asu1cornelledu@127.0.0.1>

"Ivan Novick" <ivan.d.novick@gmail.com> wrote in 
news:1170611327.022503.293050@j27g2000cwj.googlegroups.com:

> In C, you are not allowed to modify a variable 2 times in one
> statement.
> 
> In perl, this code does not complain at all (similar code in C is
> illegal).
> 
> Can someone varify that this is legal perl syntax?
> 
> ####################################
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> my $test = 1;
> 
> $test = ++$test;

I don't know the innards of Perl enough to tell you whether that 
statement will always result in all versions of Perl and all platforms 
in what you expect (BTW, I don't know what you expect the result to be).

On the other hand, why not just a simple

++$test;

???

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



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

Date: 4 Feb 2007 10:43:30 -0800
From: "Ivan Novick" <ivan.d.novick@gmail.com>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <1170614610.220020.196230@q2g2000cwa.googlegroups.com>

On Feb 4, 10:03 am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> "Ivan Novick" <ivan.d.nov...@gmail.com> wrote innews:1170611327.022503.293050@j27g2000cwj.googlegroups.com:
> > In C, you are not allowed to modify a variable 2 times in one
> > statement.
>
> > In perl, this code does not complain at all (similar code in C is
> > illegal).
>
> > Can someone varify that this is legal perl syntax?
>
> > ####################################
> > #!/usr/bin/perl
> > use strict;
> > use warnings;
>
> > my $test = 1;
>
> > $test = ++$test;
>
> I don't know the innards of Perl enough to tell you whether that
> statement will always result in all versions of Perl and all platforms
> in what you expect (BTW, I don't know what you expect the result to be).

Not sure what the result should be, in C it is undefined, that is why
I am asking this question.

> On the other hand, why not just a simple
>
> ++$test;
>
Obviously the problem can be solved by changing the code, my question
is about how perl works, not how to solve some specific problem.

---
Ivan Novick
http://www.0x4849.net



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

Date: Sun, 04 Feb 2007 18:55:07 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <Xns98CD8DDB6E8F2asu1cornelledu@127.0.0.1>

"Ivan Novick" <ivan.d.novick@gmail.com> wrote in
news:1170614610.220020.196230@q2g2000cwa.googlegroups.com: 

> On Feb 4, 10:03 am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
>> "Ivan Novick" <ivan.d.nov...@gmail.com> wrote
>> innews:1170611327.022503.293050@j27g2000cwj.googlegroups.com: 
>> > In C, you are not allowed to modify a variable 2 times in one
>> > statement.
>>
>> > In perl, this code does not complain at all (similar code in C is
>> > illegal).
>>
>> > Can someone varify that this is legal perl syntax?
>>
>> > ####################################
>> > #!/usr/bin/perl
>> > use strict;
>> > use warnings;
>>
>> > my $test = 1;
>>
>> > $test = ++$test;
>>
>> I don't know the innards of Perl enough to tell you whether that
>> statement will always result in all versions of Perl and all
>> platforms in what you expect (BTW, I don't know what you expect the
>> result to be). 
> 
> Not sure what the result should be, in C it is undefined, that is why
> I am asking this question.
> 
>> On the other hand, why not just a simple
>>
>> ++$test;
>>
> Obviously the problem can be solved by changing the code, my question
> is about how perl works, not how to solve some specific problem.
> 

Apparently, perldoc perlop provides the answer in this surprisingly 
named section:


Auto-increment and Auto-decrement

  "++" and "--" work as in C. That is, if placed before a variable, they
  increment or decrement the variable by one before returning the value,
  and if placed after, increment or decrement after returning the value.

      $i = 0;  $j = 0;
      print $i++;  # prints 0
      print ++$j;  # prints 1

  Note that just as in C, Perl doesn't define when the variable is
  incremented or decremented. You just know it will be done sometime
  before or after the value is returned. This also means that modifying
  a variable twice in the same statement will lead to undefined
  behaviour.
  Avoid statements like:

    $i = $i ++;
    print ++ $i + $i ++;

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



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

Date: 04 Feb 2007 13:57:03 -0500
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <87fy9lrbsw.fsf@mithril.chromatico.net>

>>>>> "IN" == Ivan Novick <ivan.d.novick@gmail.com> writes:

    IN> Obviously the problem can be solved by changing the code, my
    IN> question is about how perl works, not how to solve some
    IN> specific problem.

The C answer is to consult the Standard.

The Perl answer is the same, except that we don't have a Standard, we
have a reference implementation.  Try it and see.

And if you want to be sure how it works, look for what the perldocs
say.  If the perldocs don't say anything on the matter, there's no
guarantee it will continue to work that way.

Charlton



-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Sun, 4 Feb 2007 12:00:48 -0700
From: "Dave Slayton" <evad.notyals@liamg.moc>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <xo2dnRWsZ7vHsFvYnZ2dnUVZ_qWvnZ2d@comcast.com>

"Ivan Novick" <ivan.d.novick@gmail.com> wrote in message 
news:1170611327.022503.293050@j27g2000cwj.googlegroups.com...
> Hi,
>
> In C, you are not allowed to modify a variable 2 times in one
> statement.
>
> In perl, this code does not complain at all (similar code in C is
> illegal).
>
> Can someone varify that this is legal perl syntax?
>
> ####################################
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $test = 1;
>
> $test = ++$test;
>
> print "$test\n";
> ####################################
>
> Thanks,
> Ivan
> http://www.0x4849.net
>


Why stop there?  Even more fun is available!:

$wheeeee = 5;
$wheeeee = ++($wheeeee = ++($wheeeee = ++$wheeeee));

etc.

:) 




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

Date: 4 Feb 2007 11:04:12 -0800
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <1170615852.116610.304810@p10g2000cwp.googlegroups.com>

On Feb 4, 5:48 pm, "Ivan Novick" <ivan.d.nov...@gmail.com> wrote:

> In C, you are not allowed to modify a variable 2 times in one
> statement.
>
> In perl, this code does not complain at all (similar code in C is
> illegal).

Is it? I thought it was just that it's semantics were undefined.

> Can someone varify that this is legal perl syntax?

Yes it's perfectly legal _syntax_.

> $test = ++$test;

Semnatically it's is probably best considered ambiguous.



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

Date: Sun, 4 Feb 2007 21:39:28 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <slrnesch40.30u.hjp-usenet2@yoyo.hjp.at>

On 2007-02-04 19:04, Brian McCauley <nobull67@gmail.com> wrote:
> On Feb 4, 5:48 pm, "Ivan Novick" <ivan.d.nov...@gmail.com> wrote:
>> In C, you are not allowed to modify a variable 2 times in one
>> statement.
>>
>> In perl, this code does not complain at all (similar code in C is
>> illegal).
>
> Is it? I thought it was just that it's semantics were undefined.

Which is the same thing as "illegal" in C. "Undefined behaviour" means
absolutely anything can happen. The compiler may produce some code which
conforms to some people's expectations of what that code should do. It
may produce code which crashes and/or formats the hard disk. It may
abort compilation with an error message. It may send rude emails to your
boss.

	hp


-- 
   _  | Peter J. Holzer    | Es ist ganz einfach ihn zu verstehen, wenn
|_|_) | Sysadmin WSR       | man nur alle wichtigen Worte im Satz durch
| |   | hjp@hjp.at         | andere ersetzt.
__/   | http://www.hjp.at/ |	-- Nils Ketelsen in danr


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

Date: Sun, 04 Feb 2007 16:20:45 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <x7tzy1d3gy.fsf@mail.sysarch.com>

>>>>> "PJH" == Peter J Holzer <hjp-usenet2@hjp.at> writes:

  PJH> It may send rude emails to your boss.

i know i used a c compiler which did that when i double modified a
variable. could have been an early gcc.

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: Sun, 04 Feb 2007 22:38:52 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <a2kcs2dohaqmenpvhfev26080qv1i1ill2@4ax.com>

On Sun, 04 Feb 2007 18:55:07 GMT, "A. Sinan Unur"
<1usa@llenroc.ude.invalid> wrote:

>Apparently, perldoc perlop provides the answer in this surprisingly 
>named section:
[...]
>  Note that just as in C, Perl doesn't define when the variable is
>  incremented or decremented. You just know it will be done sometime
>  before or after the value is returned. This also means that modifying
>  a variable twice in the same statement will lead to undefined
>  behaviour.

Whoa! It's true that you learn something new everyday... I see no
reason why one would have to do so, but I wouldn't have expected it to
be undefined either. Naively I would have assumed that the rhs would
be evaluated first, which would comprise the variable modification as
a side effect, and that then the resulting value would be assigned to
the the lhs. Thinking of it better... if there were a more complex
lhs, which would be evaluated first: the latter or the rhs? In fact
you generally do not ask yourself this question, because you generally
don't modify a variable twice in the same statement...


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Mon, 05 Feb 2007 02:33:34 +0000
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <87veihcozl.fsf@bsb.me.uk>

"Ivan Novick" <ivan.d.novick@gmail.com> writes:

> Hi,
>
> In C, you are not allowed to modify a variable 2 times in one
> statement.

Small point, but in C you *can* modify a variable multiple times in
one statement.  You are not allowed to do that between sequence points
(and a statement end is one place you get a sequence point) but you
can, legally, write:

  (x = 1) && ++x;

because && gives you another one.

As has been said, the perldocs refer to multiple modification in one
statement so Perl seems to have adopted a simpler restriction.

-- 
Ben.


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

Date: 4 Feb 2007 19:20:25 -0800
From: "Ivan Novick" <ivan.d.novick@gmail.com>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <1170645625.168861.16970@k78g2000cwa.googlegroups.com>

On Feb 4, 10:55 am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> Apparently, perldoc perlop provides the answer in this surprisingly
> named section:
>
> Auto-increment and Auto-decrement
>
>   "++" and "--" work as in C. That is, if placed before a variable, they
>   increment or decrement the variable by one before returning the value,
>   and if placed after, increment or decrement after returning the value.
>
>       $i = 0;  $j = 0;
>       print $i++;  # prints 0
>       print ++$j;  # prints 1
>
>   Note that just as in C, Perl doesn't define when the variable is
>   incremented or decremented. You just know it will be done sometime
>   before or after the value is returned. This also means that modifying
>   a variable twice in the same statement will lead to undefined
>   behaviour.

I would have expected perl to either reject the syntax or accept as
valid.

In C just because it compiles and runs does not mean it conforms to
the standard, but since in Perl the implementation is the standard, it
makes things tricky when certain things that seem to work in the
reference implementation are actually undefined.

I guess perldoc is like a mini language standard, and if its not in
perldoc then you go by what Perl actually does.

Ivan Novick
http://www.0x4849.net



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

Date: Mon, 05 Feb 2007 04:08:28 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <05yxh.1708$xu4.816@trndny04>

Ivan Novick wrote:
> On Feb 4, 10:55 am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
>> Apparently, perldoc perlop provides the answer in this surprisingly
>> named section:
>>
>> Auto-increment and Auto-decrement
>>
>>   "++" and "--" work as in C. That is, if placed before a variable,
>>   they increment or decrement the variable by one before returning
>>   the value, and if placed after, increment or decrement after
>> returning the value.
>>
>>       $i = 0;  $j = 0;
>>       print $i++;  # prints 0
>>       print ++$j;  # prints 1
>>
>>   Note that just as in C, Perl doesn't define when the variable is
>>   incremented or decremented. You just know it will be done sometime
>>   before or after the value is returned. This also means that
>>   modifying a variable twice in the same statement will lead to
>>   undefined behaviour.
>
> I would have expected perl to either reject the syntax or accept as
> valid.

Obviously perl accepts the syntax as valid or do you get a syntax error?

> In C just because it compiles and runs does not mean it conforms to
> the standard,

You are confusing syntax and semantic.

jue 




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

Date: Sun, 04 Feb 2007 23:31:13 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <x74pq1cjji.fsf@mail.sysarch.com>

>>>>> "IN" == Ivan Novick <ivan.d.novick@gmail.com> writes:

  IN> I would have expected perl to either reject the syntax or accept as
  IN> valid.

you can't reject the syntax because it is legal syntax. perl accepts it
as valid it is just bad code from a semantic point of view. why do you
keep harping on this? it is also documented to be an undefined situation
as someone quoted the docs on it.

  IN> In C just because it compiles and runs does not mean it conforms to
  IN> the standard, but since in Perl the implementation is the standard, it
  IN> makes things tricky when certain things that seem to work in the
  IN> reference implementation are actually undefined.

the perl docs are very good and accurate. did you read the quoted part
which covers this? what about it don't you understand?

  IN> I guess perldoc is like a mini language standard, and if its not in
  IN> perldoc then you go by what Perl actually does.

that may be true but the difference between what the docs say and what
perl does is very small. and if you have a legitimate discrepency then
report it to p5p. double modification is not one of those and why do you
care so much about it as it is bad coding in all languages (except for
the few that specify behavior for it).

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: 05 Feb 2007 00:23:11 -0500
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <878xfdqitc.fsf@mithril.chromatico.net>

>>>>> "UG" == Uri Guttman <uri@stemsystems.com> writes:

    UG> and why do you care so much about it as it is bad coding in
    UG> all languages (except for the few that specify behavior for
    UG> it).

I'd say it's bad coding in general, on the principle that code is
supposed to communicate clearly to the maintainer.  Any expression in
which a variable is accessed or modified more than once can be
rewritten so as to express the intent of the programmer clearly and
unambiguously.

Perl golf is another matter entirely; there, all bets are off.

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Mon, 05 Feb 2007 11:36:07 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <050220071136072145%jgibson@mail.arc.nasa.gov>

In article <xo2dnRWsZ7vHsFvYnZ2dnUVZ_qWvnZ2d@comcast.com>, Dave Slayton
<evad.notyals@liamg.moc> wrote:

> 
> Why stop there?  Even more fun is available!:
> 
> $wheeeee = 5;
> $wheeeee = ++($wheeeee = ++($wheeeee = ++$wheeeee));

Or one of my favorite C statements:

  x = x+++++x;

which is valid C syntax, or used to be anyway (try it). This would give
different results on different systems (Sun, SGI) when I tested it many
years ago. Perl won't let me do the equivalent :(

perl -e '$x=1;$x=$x+++++$x;print "$x\n";'
Can't modify postincrement (++) in postincrement (++) at -e line 1,
near "++++"
Execution of -e aborted due to compilation errors.

although I can do:

perl -e '$x=1;$x=$x++ + ++$x;print "$x\n";'
4

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


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

Date: 05 Feb 2007 20:05:45 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <slrnesf3gd.ekq.abigail@alexandra.abigail.be>

Jim Gibson (jgibson@mail.arc.nasa.gov) wrote on MMMMCMVI September
MCMXCIII in <URL:news:050220071136072145%jgibson@mail.arc.nasa.gov>:
##  In article <xo2dnRWsZ7vHsFvYnZ2dnUVZ_qWvnZ2d@comcast.com>, Dave Slayton
## <evad.notyals@liamg.moc> wrote:
##  
## > 
## > Why stop there?  Even more fun is available!:
## > 
## > $wheeeee = 5;
## > $wheeeee = ++($wheeeee = ++($wheeeee = ++$wheeeee));
##  
##  Or one of my favorite C statements:
##  
##    x = x+++++x;
##  
##  which is valid C syntax, or used to be anyway (try it). This would give

Not according to Kerninghan and Ritchie (and they should know):

    If the input stream has been separated into tokens up to a 
    given character, the next token is the longest string of
    characters that could constitute a token.

                          Brian W. Kerninghan and Dennis M. Ritchie:
                          "The C Programming Language", 2n ed,
                          Prentice Hall, 1988. Appendix A2.1, pp 192.

##  different results on different systems (Sun, SGI) when I tested it many
##  years ago. Perl won't let me do the equivalent :(

That's because Perls tokenizer (just like many other compilers) is context
free, and greedy.

##  perl -e '$x=1;$x=$x+++++$x;print "$x\n";'
##  Can't modify postincrement (++) in postincrement (++) at -e line 1,
##  near "++++"
##  Execution of -e aborted due to compilation errors.
##  
##  although I can do:
##  
##  perl -e '$x=1;$x=$x++ + ++$x;print "$x\n";'
##  4


Indeed. Just like any C compiler I've worked with.



Abigail
-- 
# Count the number of lines; code doesn't match \w. Linux specific.
()=<>;$!=$=;($:,$,,$;,$")=$!=~/.(.)..(.)(.)..(.)/;
$;++;$*++;$;++;$*++;$;++;`$:$,$;$" $. >&$*`; 


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

Date: 05 Feb 2007 20:09:34 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <slrnesf3ni.ekq.abigail@alexandra.abigail.be>

Ivan Novick (ivan.d.novick@gmail.com) wrote on MMMMCMV September MCMXCIII
in <URL:news:1170611327.022503.293050@j27g2000cwj.googlegroups.com>:
@@  Hi,
@@  
@@  In C, you are not allowed to modify a variable 2 times in one
@@  statement.
@@  
@@  In perl, this code does not complain at all (similar code in C is
@@  illegal).
@@  
@@  Can someone varify that this is legal perl syntax?
@@  
@@  ####################################
@@  #!/usr/bin/perl
@@  use strict;
@@  use warnings;
@@  
@@  my $test = 1;
@@  
@@  $test = ++$test;
@@  
@@  print "$test\n";
@@  ####################################


Legal syntax, but undefined semantics.

Just like C, BTW.

gcc warns on "i = ++ i;" (just a warning, it's not a syntax error):

    warning: operation on `i' may be undefined


Abigail
-- 
perl -e '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 
         % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %;
         BEGIN {% % = ($ _ = " " => print "Just Another Perl Hacker\n")}'


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

Date: Mon, 05 Feb 2007 12:50:26 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <050220071250262809%jgibson@mail.arc.nasa.gov>

In article <slrnesf3gd.ekq.abigail@alexandra.abigail.be>, Abigail
<abigail@abigail.be> wrote:

> Jim Gibson (jgibson@mail.arc.nasa.gov) wrote on MMMMCMVI September
> MCMXCIII in <URL:news:050220071136072145%jgibson@mail.arc.nasa.gov>:

> ##  Or one of my favorite C statements:
> ##  
> ##    x = x+++++x;
> ##  
> ##  which is valid C syntax, or used to be anyway (try it). This would give
> 
> Not according to Kerninghan and Ritchie (and they should know):
> 
>     If the input stream has been separated into tokens up to a 
>     given character, the next token is the longest string of
>     characters that could constitute a token.
> 
>                           Brian W. Kerninghan and Dennis M. Ritchie:
>                           "The C Programming Language", 2n ed,
>                           Prentice Hall, 1988. Appendix A2.1, pp 192.
> 

I am not sure how this quoted statement invalidates the C statement
above. I had assumed that the statement "x+++++x" was being interpreted
as "(x++) + (++x)", but according to the above, perhaps it should be
interpreted as "((x++)++) + (x)". That seems to be invalid, as I get
"error: invalid lvalue in increment" from "gcc version 4.0.1 (Apple
Computer, Inc. build 5363)" for both "x+++++x" and "((x++)++)+(x)".

Nevertheless, the statement "x+++++x" was compiled without error by
every C compiler I tried at the time (mid 90s). I think it was 2 or 3
compilers on different systems. I got at least 2 different numerical
results on the systems available to me at the time.

> ##  
> ##  perl -e '$x=1;$x=$x++ + ++$x;print "$x\n";'
> ##  4
> 
> 
> Indeed. Just like any C compiler I've worked with.

But the above-mentioned gcc compiler gives 5, not 4, for the equivalent:

jgibson 92% cat increment.c
#include <stdio.h>
int main()
{
        int x;
        x = 1;
        x = x++ + ++x;
        printf("x = %d\n", x);
}
jgibson 93% gcc -o increment increment.c
jgibson 94% increment
x = 5

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


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

Date: 05 Feb 2007 21:16:16 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <slrnesf7kk.ekq.abigail@alexandra.abigail.be>

Jim Gibson (jgibson@mail.arc.nasa.gov) wrote on MMMMCMVI September
MCMXCIII in <URL:news:050220071250262809%jgibson@mail.arc.nasa.gov>:
;;  In article <slrnesf3gd.ekq.abigail@alexandra.abigail.be>, Abigail
;; <abigail@abigail.be> wrote:
;;  
;; > Jim Gibson (jgibson@mail.arc.nasa.gov) wrote on MMMMCMVI September
;; > MCMXCIII in <URL:news:050220071136072145%jgibson@mail.arc.nasa.gov>:
;;  
;; > ##  Or one of my favorite C statements:
;; > ##  
;; > ##    x = x+++++x;
;; > ##  
;; > ##  which is valid C syntax, or used to be anyway (try it). This would give
;; > 
;; > Not according to Kerninghan and Ritchie (and they should know):
;; > 
;; >     If the input stream has been separated into tokens up to a 
;; >     given character, the next token is the longest string of
;; >     characters that could constitute a token.
;; > 
;; >                           Brian W. Kerninghan and Dennis M. Ritchie:
;; >                           "The C Programming Language", 2n ed,
;; >                           Prentice Hall, 1988. Appendix A2.1, pp 192.
;; > 
;;  
;;  I am not sure how this quoted statement invalidates the C statement
;;  above. I had assumed that the statement "x+++++x" was being interpreted
;;  as "(x++) + (++x)", but according to the above, perhaps it should be
;;  interpreted as "((x++)++) + (x)". That seems to be invalid, as I get
;;  "error: invalid lvalue in increment" from "gcc version 4.0.1 (Apple
;;  Computer, Inc. build 5363)" for both "x+++++x" and "((x++)++)+(x)".

It's quite simple. According the above statement, 'x+++++x' is parsed as
follows:

    Unparsed stream     Maximal token    Parsed tokens

    x+++++x             x                x
    +++++x              ++               x ++
    +++x                ++               x ++ ++
    +x                  +                x ++ ++ +
    x                   x                x ++ ++ + x
                                         
But a compiler will flag a syntax error as soon as it finds 'x ++ ++'.

It cannot be parsed as 'x ++ + ++ x' because after parsing the 'x ++'
the longest string of characters thatn could constitute a token is '++'.

;;  
;;  Nevertheless, the statement "x+++++x" was compiled without error by
;;  every C compiler I tried at the time (mid 90s). I think it was 2 or 3
;;  compilers on different systems. I got at least 2 different numerical
;;  results on the systems available to me at the time.
;;  
;; > ##  
;; > ##  perl -e '$x=1;$x=$x++ + ++$x;print "$x\n";'
;; > ##  4
;; > 
;; > 
;; > Indeed. Just like any C compiler I've worked with.
;;  
;;  But the above-mentioned gcc compiler gives 5, not 4, for the equivalent:
;;  
;;  jgibson 92% cat increment.c
;;  #include <stdio.h>
;;  int main()
;;  {
;;          int x;
;;          x = 1;
;;          x = x++ + ++x;
;;          printf("x = %d\n", x);
;;  }
;;  jgibson 93% gcc -o increment increment.c
;;  jgibson 94% increment
;;  x = 5


Oh, I was just refering to the fact that Perl parses "$x++ + ++$x"
and rejects "$x+++++$x". What is the same between Perl and C is that
the result of "($)x++ + ++($)x" is undefined.

Basically, we have three things about this that are the same in Perl
and C:

    x+++++x     invalid syntax
    x++ + ++x   valid syntax
    x++ + ++x   undefined result



Abigail
-- 
perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'


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

Date: Mon, 5 Feb 2007 23:10:59 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <slrnesfarj.ker.hjp-usenet2@yoyo.hjp.at>

On 2007-02-05 21:16, Abigail <abigail@abigail.be> wrote:
> Jim Gibson (jgibson@mail.arc.nasa.gov) wrote on MMMMCMVI September
> MCMXCIII in <URL:news:050220071250262809%jgibson@mail.arc.nasa.gov>:
> ;;  In article <slrnesf3gd.ekq.abigail@alexandra.abigail.be>, Abigail
> ;; <abigail@abigail.be> wrote:
> ;;  
> ;; > Jim Gibson (jgibson@mail.arc.nasa.gov) wrote on MMMMCMVI September
> ;; > MCMXCIII in <URL:news:050220071136072145%jgibson@mail.arc.nasa.gov>:
> ;;  
> ;; > ##  Or one of my favorite C statements:
> ;; > ##  
> ;; > ##    x = x+++++x;
> ;; > ##  
> ;; > ##  which is valid C syntax, or used to be anyway (try it). This would give
> ;; > 
> ;; > Not according to Kerninghan and Ritchie (and they should know):
> ;; > 
> ;; >     If the input stream has been separated into tokens up to a 
> ;; >     given character, the next token is the longest string of
> ;; >     characters that could constitute a token.
> ;; > 
> ;; >                           Brian W. Kerninghan and Dennis M. Ritchie:
> ;; >                           "The C Programming Language", 2n ed,
> ;; >                           Prentice Hall, 1988. Appendix A2.1, pp 192.
> ;; > 
> ;;  
> ;;  I am not sure how this quoted statement invalidates the C statement
> ;;  above. I had assumed that the statement "x+++++x" was being interpreted
> ;;  as "(x++) + (++x)", but according to the above, perhaps it should be
> ;;  interpreted as "((x++)++) + (x)". That seems to be invalid, as I get
> ;;  "error: invalid lvalue in increment" from "gcc version 4.0.1 (Apple
> ;;  Computer, Inc. build 5363)" for both "x+++++x" and "((x++)++)+(x)".
>
> It's quite simple. According the above statement, 'x+++++x' is parsed as
> follows:
>
>     Unparsed stream     Maximal token    Parsed tokens
>
>     x+++++x             x                x
>     +++++x              ++               x ++
>     +++x                ++               x ++ ++
>     +x                  +                x ++ ++ +
>     x                   x                x ++ ++ + x
>                                          
> But a compiler will flag a syntax error as soon as it finds 'x ++ ++'.

That's not a syntax error, that's a constraint violation. Syntactically,
'x ++ ++' is a valid postfix-expression:

postfix-expression: postfix-expression ++
postfix-expression: postfix-expression ++ ++
postfix-expression: primary-expression ++ ++
postfix-expression: identifier ++ ++
postfix-expression: x ++ ++

However, it violates the constraint, that the operand of ++ must be an
lvalue. x++ isn't an lvalue, so x++ ++ violates this constraint.

> ;;  Nevertheless, the statement "x+++++x" was compiled without error by
> ;;  every C compiler I tried at the time (mid 90s).

Then you didn't try any C-compiler conforming to ISO-9899-1990. A
constraint violation must be diagnosed.

> ;;  But the above-mentioned gcc compiler gives 5, not 4, for the equivalent:
> ;;  
> ;;  jgibson 92% cat increment.c
> ;;  #include <stdio.h>
> ;;  int main()
> ;;  {
> ;;          int x;
> ;;          x = 1;
> ;;          x = x++ + ++x;
> ;;          printf("x = %d\n", x);
> ;;  }
> ;;  jgibson 93% gcc -o increment increment.c
> ;;  jgibson 94% increment
> ;;  x = 5
>

That's an interesting result. From looking at the assembler output, the
Compiler obviously does something like this:

"x++"? Ok, that means to take the current value of x, and increment x
later, so transform "x = x++ + ++x" into "x = x + ++x, x = x + 1;"

"++x"? Ok, that means increment x now, and use the new value. So we
transform "x = x + ++x, x = x + 1;" into
"x = x + 1, x = x + x, x = x + 1"

Nice.

	hp

-- 
   _  | Peter J. Holzer    | Es ist ganz einfach ihn zu verstehen, wenn
|_|_) | Sysadmin WSR       | man nur alle wichtigen Worte im Satz durch
| |   | hjp@hjp.at         | andere ersetzt.
__/   | http://www.hjp.at/ |	-- Nils Ketelsen in danr


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

Date: Tue, 06 Feb 2007 15:25:20 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: modifying a variable twice in a statement (comparison to c)
Message-Id: <eqa3ea$64j$1@mlucom4.urz.uni-halle.de>

Peter J. Holzer wrote:
> On 2007-02-05 21:16, Abigail <abigail@abigail.be> wrote:
>> Jim Gibson (jgibson@mail.arc.nasa.gov) wrote on MMMMCMVI September
>> ;;  jgibson 92% cat increment.c
>> ;;  #include <stdio.h>
>> ;;  int main()
>> ;;  {
>> ;;          int x;
>> ;;          x = 1;
>> ;;          x = x++ + ++x;
>> ;;          printf("x = %d\n", x);
>> ;;  }
>> ;;  jgibson 93% gcc -o increment increment.c
>> ;;  jgibson 94% increment
>> ;;  x = 5
>>
> 
> That's an interesting result. From looking at the assembler output, the
> Compiler obviously does something like this:
> 
> "x++"? Ok, that means to take the current value of x, and increment x
> later, so transform "x = x++ + ++x" into "x = x + ++x, x = x + 1;"
> 
> "++x"? Ok, that means increment x now, and use the new value. So we
> transform "x = x + ++x, x = x + 1;" into
> "x = x + 1, x = x + x, x = x + 1"

int x;                 pushl	%ebp
                        movl	%esp, %ebp
                        subl	$36, %esp

x = 1;                 movl	$1, -8(%ebp)      #  WRITE 1 into [stack] (location for x)

x = x++ + ++x;         addl	$1, -8(%ebp)      #  ADD 1 to [stack]              (now 2)
                        movl	-8(%ebp), %eax    #  WRITE [stack] into [register] (copy 2)
                        addl	%eax, -8(%ebp)    #  ADD [register] to [stack]     (now 4)
                        addl	$1, -8(%ebp) 	  #  ADD 1 to [stack]              (now 5)



Regards

M.




























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

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 V11 Issue 109
**************************************


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