[comp.lang.perl] Do be do be do.

flee@cs.psu.edu (Felix Lee) (02/01/91)

I spent fifteen minutes staring at this thing, wondering why it didn't
work right:
	sub put {
		local($x) = @_;
		print $x;
		$currentposition += length($x);
	}
	$currentposition = 1;
	$desiredposition = 1;
	&put(' ') while $currentposition < $desiredposition;

It prints one space rather than none.

Perl semantics are much too toxic for ordinary household use.
--
Felix Lee	flee@cs.psu.edu

rodney@sun.ipl.rpi.edu (Rodney Peck II) (02/02/91)

In article <ii7G7s5c@cs.psu.edu> flee@cs.psu.edu (Felix Lee) writes:
>I spent fifteen minutes staring at this thing, wondering why it didn't
>work right:
>	sub put {
>		local($x) = @_;
>		print $x;
>		$currentposition += length($x);
>	}
>	$currentposition = 1;
>	$desiredposition = 1;
>	&put(' ') while $currentposition < $desiredposition;
>
>It prints one space rather than none.
>
>Perl semantics are much too toxic for ordinary household use.
>--
>Felix Lee	flee@cs.psu.edu

That's an easy one.

you've got the while syntax wrong.  I don't know what your version does,
but it will most likely _always_ exec put(' ') once and then do something
about the while part.

The thing you are trying to say is this:


	while ($currentposition < $desiredposition)
	  { &put(' '); }


-- 
Rodney

tchrist@convex.COM (Tom Christiansen) (02/02/91)

From the keyboard of rodney@sun.ipl.rpi.edu (Rodney Peck II):
:The thing you are trying to say is this:
:
:	while ($currentposition < $desiredposition)
:	  { &put(' '); }

Or:

    do { &put(' '); } while $currentposition < $desiredposition;

I think that because this is so different from 

    &put(' ') while $currentposition < $desiredposition;

was why Felix was shaking his head.


--tom
--
"Hey, did you hear Stallman has replaced /vmunix with /vmunix.el?  Now
 he can finally have the whole O/S built-in to his editor like he
 always wanted!" --me (Tom Christiansen <tchrist@convex.com>)

tchrist@convex.COM (Tom Christiansen) (02/04/91)

From the keyboard of marc@mit.edu:
:I was under the impression that
:
:	&foo() while $bar;
:
:was just a sugar for
:
:	while ($bar) {
:		&foo();
:	}
:
:If not, what are the rules?  (Feel free to cite page numbers.  I got
:my Book :-)

It's in the section entitled ``Simple Statements'' in both the man page
and the Camel Book (see the top of page 94).  The essential text follows:

    The loop conditional is evaluation before the expression, except when
    applied to a do-BLOCK or a do_SUBROUTINE command, in which case the
    block executes once before the conditional is evaluated.

More details follow.  Check it out.

--tom
--
"Still waiting to read alt.fan.dan-bernstein using DBWM, Dan's own AI
window manager, which argues with you 10 weeks before resizing your window." 
### And now for the question of the month:  How do you spell relief?   Answer:
U=brnstnd@kramden.acf.nyu.edu; echo "/From: $U/h:j" >>~/News/KILL; expire -f $U

rbj@uunet.UU.NET (Root Boy Jim) (02/04/91)

In article <1991Feb04.015138.28575@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
>It's in the section entitled ``Simple Statements'' in both the man page
>and the Camel Book (see the top of page 94).  The essential text follows:
>
>    The loop conditional is evaluation before the expression, except when
>    applied to a do-BLOCK or a do_SUBROUTINE command, in which case the
>    block executes once before the conditional is evaluated.

Yeah, but it's not a "do SUBROUTINE", it's a "&SUBROUTINE".
Oh. They're the same thing? I don't think they should be.
-- 

	Root Boy Jim Cottrell <rbj@uunet.uu.net>
	Close the gap of the dark year in between

tchrist@convex.COM (Tom Christiansen) (02/04/91)

From the keyboard of rbj@uunet.UU.NET (Root Boy Jim):
:Yeah, but it's not a "do SUBROUTINE", it's a "&SUBROUTINE".
:Oh. They're the same thing? I don't think they should be.

Almost but not quite the same thing.  (Larry will catch me if I'm lying.)

    do SUBROUTINE();

is the same as

    &SUBROUTINE();

But *NOT* the same as 

    &SUBROUTINE;

The difference is whether we set up a new @_ array for our param list.
Doing so takes a wee more time and memory than not doing so.  

Whether it should be this way or not is another matter.  Perl isn't 
supposed to be pretty, you know, merely expedient. :-)

--tom
--
"Still waiting to read alt.fan.dan-bernstein using DBWM, Dan's own AI
window manager, which argues with you 10 weeks before resizing your window." 
### And now for the question of the month:  How do you spell relief?   Answer:
U=brnstnd@kramden.acf.nyu.edu; echo "/From: $U/h:j" >>~/News/KILL; expire -f $U

tchrist@convex.COM (Tom Christiansen) (02/04/91)

From the keyboard of rbj@uunet.UU.NET (Root Boy Jim):
:[Tom explains diff between "do sub;" != "do sub()" == "&sub()" != "&sub"

    do sub;

is illegal.  you need the parens.

:	function(a,r,g,s) while condition
:with
:	&Function(a,r,g,s) while condition
:
:I see no reason to treat a subroutine call different from any
:other expression. This is counterintuitive. 

I hope I haven't led you to believe that it is different.
In both cases, the while gets evaluated first; that's just
how statement modifiers work.  Only for 
    do { FOO } while EXPR;
should the loop execute at least once (FORTRAN 66 mode :-) irrespective
of the EXPR's value.  because the while is NOT evaluated until the end,
Otherwise people would be surprised.

So these will not happen:
    &FOO while 0;
    do FOO() while 0;
but this will:
    do { &FOO; } while 0;

--tom
--
"Still waiting to read alt.fan.dan-bernstein using DBWM, Dan's own AI
window manager, which argues with you 10 weeks before resizing your window." 
### And now for the question of the month:  How do you spell relief?   Answer:
U=brnstnd@kramden.acf.nyu.edu; echo "/From: $U/h:j" >>~/News/KILL; expire -f $U

flee@cs.psu.edu (Felix Lee) (02/05/91)

I was annoyed enough about
	&foo() while $bar;
that I'm now using
	0, &foo() while $bar;
which is actually pretty silly.
--
Felix

tchrist@convex.COM (Tom Christiansen) (02/05/91)

From the keyboard of tchrist@convex.COM (Tom Christiansen):

I lied, and Larry hasn't even chastised me yet. :-)

:So these will not happen:
:    &FOO while 0;
:    do FOO() while 0;
:but this will:
:    do { &FOO; } while 0;

This isn't true!!!!  They all happen once!!!  Thanks to Felix
for happily pointing this out to me.  His

    0, &foo() while $bar;

solution, ugly though it may be, gets around this.

I'm wondering whether Larry did this intentionally.  It doesn't
seem to follow the priniple of least astonishment.  Or perhaps
I'm just easily astonished.

--tom
--
"Still waiting to read alt.fan.dan-bernstein using DBWM, Dan's own AI
window manager, which argues with you 10 weeks before resizing your window." 
### And now for the question of the month:  How do you spell relief?   Answer:
U=brnstnd@kramden.acf.nyu.edu; echo "/From: $U/h:j" >>~/News/KILL; expire -f $U

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/05/91)

In article <121266@uunet.UU.NET> rbj@uunet.UU.NET (Root Boy Jim) writes:
: In article <1991Feb04.015138.28575@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
: >It's in the section entitled ``Simple Statements'' in both the man page
: >and the Camel Book (see the top of page 94).  The essential text follows:
: >
: >    The loop conditional is evaluation before the expression, except when
: >    applied to a do-BLOCK or a do_SUBROUTINE command, in which case the
: >    block executes once before the conditional is evaluated.
: 
: Yeah, but it's not a "do SUBROUTINE", it's a "&SUBROUTINE".
: Oh. They're the same thing? I don't think they should be.

Neither do I.  This is something I've known about for several months
(my brother-in-law Mark Biggar pointed it out when he was writing the big*
packages), but I hadn't had a chance to fix it.  In 4.0,

	do foo() while $something;

will always execute at least once, and

	&foo() while $something

may execute 0 times.

Larry

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/05/91)

In article <$ecGn8-e@cs.psu.edu> flee@cs.psu.edu (Felix Lee) writes:
: I was annoyed enough about
: 	&foo() while $bar;
: that I'm now using
: 	0, &foo() while $bar;
: which is actually pretty silly.

Mark's workaround was

	+&foo() while $bar;

which doesn't LOOK as silly.

Anyway, like I said, it's fixed in 4.0.  The do-SUBR-while thing predated
the &SUBR syntactic sugar by a fair amount, and it just sort of snuck
past.  Sorry for only having one brain...

I just hope nobody out there has been DEPENDING on it to work that way.

Larry

piet@cs.ruu.nl (Piet van Oostrum) (02/06/91)

>>>>> In message <1991Feb04.015138.28575@convex.com>, tchrist@convex.COM (Tom Christiansen) (TC) writes:

TC> It's in the section entitled ``Simple Statements'' in both the man page
TC> and the Camel Book (see the top of page 94).  The essential text follows:

TC>     The loop conditional is evaluation before the expression, except when
TC>     applied to a do-BLOCK or a do_SUBROUTINE command, in which case the
TC>     block executes once before the conditional is evaluated.

As I pointed out before, it is NOT in the man ``page''. The man ``page''
only says that do-BLOCK has the once-at-least semantics, not do-SUBR.
Please change this, Larry.

Btw, in case of discrepancy, shouldn't the man ``page'' be the ultimate ref, as
it is updated more often (presumably). Hmmm... I'm getting tired of writing
"``page''" rather than "page". 
-- 
Piet* van Oostrum, Dept of Computer Science, Utrecht University,
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands.
Telephone: +31 30 531806   Uucp:   uunet!mcsun!ruuinf!piet
Telefax:   +31 30 513791   Internet:  piet@cs.ruu.nl   (*`Pete')

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/07/91)

In article <4796@ruuinf.cs.ruu.nl> piet@cs.ruu.nl (Piet van Oostrum) writes:
: >>>>> In message <1991Feb04.015138.28575@convex.com>, tchrist@convex.COM (Tom Christiansen) (TC) writes:
: 
: TC> It's in the section entitled ``Simple Statements'' in both the man page
: TC> and the Camel Book (see the top of page 94).  The essential text follows:
: 
: TC>     The loop conditional is evaluation before the expression, except when
: TC>     applied to a do-BLOCK or a do_SUBROUTINE command, in which case the
: TC>     block executes once before the conditional is evaluated.
: 
: As I pointed out before, it is NOT in the man ``page''. The man ``page''
: only says that do-BLOCK has the once-at-least semantics, not do-SUBR.
: Please change this, Larry.

Ok.

: Btw, in case of discrepancy, shouldn't the man ``page'' be the ultimate ref,
: as it is updated more often (presumably).

In some sense, yes.  But the only section of the man page that should
actually be trusted to be more authoritative is the Errata and Addenda
section.  The man page will always remain more cryptic than the book, but
the man page should of course be fixed when it's inaccurate.

: Hmmm... I'm getting tired of writing "``page''" rather than "page". 

If we had to quote all of our metaphors, we'd be in sad shape.  Man pages
departed from being single sheets of paper long ago.  They're only slightly
more closely related than pages of memory.  That page of history in which
we had to quote 'man "page"' is past.

Larry

piet@cs.ruu.nl (Piet van Oostrum) (02/08/91)

>>>>> In message <11330@jpl-devvax.JPL.NASA.GOV>, lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (LW) writes:


LW> In some sense, yes.  But the only section of the man page that should
LW> actually be trusted to be more authoritative is the Errata and Addenda
LW> section.  The man page will always remain more cryptic than the book, but
LW> the man page should of course be fixed when it's inaccurate.

Yes, that sounds good.

LW> : Hmmm... I'm getting tired of writing "``page''" rather than "page". 

LW> If we had to quote all of our metaphors, we'd be in sad shape.  Man pages
LW> departed from being single sheets of paper long ago.  They're only slightly
LW> more closely related than pages of memory.  That page of history in which
LW> we had to quote 'man "page"' is past.

I forgot the :=). It is just that it is so much bigger than the others I'm
used to.

By the way, is there a commonly used man page that is bigger than perl's?
-- 
Piet van Oostrum <piet@cs.ruu.nl>

gpvos@cs.vu.nl (Gerben 'P' Vos) (02/13/91)

piet@cs.ruu.nl (Piet van Oostrum) writes:
>By the way, is there a commonly used man page that is bigger than perl's?

nn's manual is a little bit larger, about 2K.

-                                       Gerben
--
--- Gerben Vos - Aconet: BIGBEN!Gerben Vos - Internet: gpvos@cs.vu.nl
---- The fact that the "new world order" Bush is speaking about has to be
----- established by means of a war, makes me wonder what this new world order
------ is all about.