Scalar variable in void context before a loop

M

Mark Hobley

In my professional perl programming guide, some of the examples put the
variable to be used as an iterator in void context before the loop. For
example:

$l;
for ($l = 0; $l < 10; $l++) {
print $l;
}

I am curious as to what reasons there are for doing this, because there
does not appear to be any mention of it anywhere within the book.

Mark.
 
T

Tim Greer

Mark said:
In my professional perl programming guide, some of the examples put
the variable to be used as an iterator in void context before the
loop. For example:

$l;
for ($l = 0; $l < 10; $l++) {
print $l;
}

I am curious as to what reasons there are for doing this, because
there does not appear to be any mention of it anywhere within the
book.

Mark.

Are you sure it wasn't $| before the loop? Or, perhaps it was a "my
$l"? Can you type the entire code here up to that point (or the
relevant portions anyway) -- the actual code, rather than an example,
so you can get the appropriate answer?
 
J

John W. Krahn

Mark said:
In my professional perl programming guide, some of the examples put the
variable to be used as an iterator in void context before the loop. For
example:

$l;
for ($l = 0; $l < 10; $l++) {
print $l;
}

I am curious as to what reasons there are for doing this, because there
does not appear to be any mention of it anywhere within the book.

There is no reason, and in fact if you had had warnings enabled then
perl would have informed you that there was no reason. You should have
these two lines at the beginning of your program:

use warnings;
use strict;

To help you catch mistakes like this.



John
 
M

Mark Hobley

Tim Greer said:
Are you sure it wasn't $| before the loop?

What is that?
Or, perhaps it was a "my $l"?

Now that would have made more sense. This occurs a couple of times
throughout the book. I bet it is a misprint.
Can you type the entire code here up to that point (or the
relevant portions anyway) -- the actual code, rather than an example,

Unfortunately, the entire code is just the example.

Mark.
 
J

Jürgen Exner

In my professional perl programming guide, some of the examples put the
variable to be used as an iterator in void context before the loop. For
example:

$l;
for ($l = 0; $l < 10; $l++) {
print $l;
}

I am curious as to what reasons there are for doing this, because there
does not appear to be any mention of it anywhere within the book.

Of ocurse I don't know what the author was thinking. But _I_ would write
this as
for my $| (0..9) {
print $|;
}

Another question is why he would possibly want to assign 0 to 9 to the
autoflush variable. It's a binary variable, so the last 8 assignments
don't have any effect.

jue
 
T

Tim Greer

Mark said:
What is that?


Now that would have made more sense. This occurs a couple of times
throughout the book. I bet it is a misprint.


Unfortunately, the entire code is just the example.

Mark.

Sorry, I'm not familiar with the book, I just wanted to be sure that was
the full code from their example, as typed (a $l and $| might look very
similiar in print form, for example, and one could make more sense than
the other). As for $| and it's meaning, see: perldoc -q buffer Still,
it would be unlikely someone would have $|; just randomly there. It
does sound like a typo. If this is all over the book's examples, I'd
have to wonder if this is a good book to follow.
 
P

Peter J. Holzer

Of ocurse I don't know what the author was thinking. But _I_ would write
this as
for my $| (0..9) {
print $|;
}

I hope not.

Another question is why he would possibly want to assign 0 to 9 to the
autoflush variable.

A third question is why you use a font which apparently uses the same
glyph for the pipe symbol and the lower case ell.

hp
 
M

Mark Hobley

John Bokma said:
Which guide is that?

In this case it is a Wrox programming guide, but it is no longer on
their website, so I have not been able to obtain an Errata for this.

Anyhow, It looks like it is definately wrong, so I shall just make
appropriate amendments to my copy.

Regards,

Mark.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top