Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Scalar variable in void context before a loop

Reply
Thread Tools

Scalar variable in void context before a loop

 
 
Mark Hobley
Guest
Posts: n/a
 
      10-20-2008
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.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      10-20-2008
Mark Hobley wrote:

> 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?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-20-2008
Mark Hobley wrote:
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Mark Hobley
Guest
Posts: n/a
 
      10-20-2008
Tim Greer <(E-Mail Removed)> wrote:

> 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.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      10-20-2008
Mark Hobley <(E-Mail Removed)> wrote:
> Tim Greer <(E-Mail Removed)> wrote:
>
>> Are you sure it wasn't $| before the loop?

>
> What is that?



Perl's special variables are described in

perldoc perlvar


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) (Mark Hobley) wrote:
>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
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      10-20-2008
(E-Mail Removed) (Mark Hobley) wrote:

> In my professional perl programming guide,


Which guide is that?

--
John http://johnbokma.com/ - Hacking & Hiking in Mexico

Perl help in exchange for a gift:
http://johnbokma.com/perl/help-in-ex...or-a-gift.html
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-20-2008
Mark Hobley wrote:

> Tim Greer <(E-Mail Removed)> wrote:
>
>> 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.
>


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.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      10-20-2008
On 2008-10-20 13:03, Jürgen Exner <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Mark Hobley) wrote:
>>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 $|;
> }


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
 
Reply With Quote
 
Mark Hobley
Guest
Posts: n/a
 
      10-21-2008
John Bokma <(E-Mail Removed)> wrote:

> 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.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why isn't void context a 'real' context Florian Kaufmann Perl Misc 2 12-13-2007 04:24 AM
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments