Scalar variable in void context before a loop

Discussion in 'Perl Misc' started by Mark Hobley, Oct 20, 2008.

  1. Mark Hobley

    Mark Hobley Guest

    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/
     
    Mark Hobley, Oct 20, 2008
    #1
    1. Advertising

  2. Mark Hobley

    Tim Greer Guest

    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!
     
    Tim Greer, Oct 20, 2008
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, Oct 20, 2008
    #3
  4. Mark Hobley

    Mark Hobley Guest

    Tim Greer <> 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/
     
    Mark Hobley, Oct 20, 2008
    #4
  5. Mark Hobley <> wrote:
    > Tim Greer <> 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/"
     
    Tad J McClellan, Oct 20, 2008
    #5
  6. (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
     
    Jürgen Exner, Oct 20, 2008
    #6
  7. Mark Hobley

    John Bokma Guest

    John Bokma, Oct 20, 2008
    #7
  8. Mark Hobley

    Tim Greer Guest

    Mark Hobley wrote:

    > Tim Greer <> 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!
     
    Tim Greer, Oct 20, 2008
    #8
  9. On 2008-10-20 13:03, Jürgen Exner <> wrote:
    > (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
     
    Peter J. Holzer, Oct 20, 2008
    #9
  10. Mark Hobley

    Mark Hobley Guest

    John Bokma <> 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/
     
    Mark Hobley, Oct 21, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    602
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    837
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    882
    S.Tobias
    Jul 22, 2005
  4. Replies:
    1
    Views:
    435
    Victor Bazarov
    May 23, 2007
  5. Florian Kaufmann

    Why isn't void context a 'real' context

    Florian Kaufmann, Dec 11, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    95
    Tad J McClellan
    Dec 13, 2007
Loading...

Share This Page