seek and __DATA__ not behaving how I would hope

Discussion in 'Perl Misc' started by Chris Marshall, May 17, 2005.

  1. Hi

    I am trying to use something like the script below (perl 5.6.0) to
    iterate through the data at the bottom of the script several times (and
    put an incremented parameter in there). However it does not seem
    possible to seek back to the start of DATA. Can this be done in this
    way or would I be better off using a here doc for the data and
    iterating over the string many times ?

    thanks,
    Christian


    cat ./test.pl
    #!/usr/bin/perl
    use warnings;
    use strict;

    for my $num (1..5){
    while(<DATA>){
    s/XX_NUM_XX/$num/;
    print;
    }
    seek DATA, 0, 1;
    print "seek $num\n"; #debug
    }



    __DATA__
    hello XX_NUM_XX
    world XX_NUM_XX


    output:

    ../test.pl
    hello 1
    world 1
    seek 1
    seek 2
    seek 3
    seek 4
    seek 5
    Chris Marshall, May 17, 2005
    #1
    1. Advertising

  2. Chris Marshall

    Anno Siegel Guest

    Chris Marshall <> wrote in comp.lang.perl.misc:
    > Hi
    >
    > I am trying to use something like the script below (perl 5.6.0) to
    > iterate through the data at the bottom of the script several times (and
    > put an incremented parameter in there). However it does not seem
    > possible to seek back to the start of DATA. Can this be done in this
    > way or would I be better off using a here doc for the data and
    > iterating over the string many times ?


    The DATA filehandle is a read handle to your source file. When you
    get to see it first, it "happens to be" positioned right past __END__
    or __DATA__. When you seek to the beginning, it does just that and
    positions to the first source line.

    To reset the DATA handle to past __END__ you need to save the position
    you find the filehandle in:

    > cat ./test.pl
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >

    ( my $data_pos = tell DATA) >= 0 or die "DATA not seekable";
    > for my $num (1..5){
    > while(<DATA>){
    > s/XX_NUM_XX/$num/;
    > print;
    > }
    > seek DATA, 0, 1;


    You are positioning relative to the current position (second parameter
    WHENCE is 1). An offset of 0 isn't going to change the position. You
    meant "seek 0, 0", but really you want

    seek DATA $data_pos, 0;

    > print "seek $num\n"; #debug
    > }
    >
    > __DATA__
    > hello XX_NUM_XX
    > world XX_NUM_XX


    Try these changes (untested), it may do what you want.

    Anno
    Anno Siegel, May 17, 2005
    #2
    1. Advertising

  3. Chris Marshall

    Anno Siegel Guest

    Chris Marshall <> wrote in comp.lang.perl.misc:
    > Hi
    >
    > I am trying to use something like the script below (perl 5.6.0) to
    > iterate through the data at the bottom of the script several times (and
    > put an incremented parameter in there). However it does not seem
    > possible to seek back to the start of DATA. Can this be done in this
    > way or would I be better off using a here doc for the data and
    > iterating over the string many times ?


    The DATA filehandle is a read handle to your source file. When you
    get to see it first, it "happens to be" positioned right past __END__
    or __DATA__. When you seek to the beginning, it does just that and
    positions to the first source line.

    To reset the DATA handle to past __END__ you need to save the position
    you find the filehandle in:

    > cat ./test.pl
    > #!/usr/bin/perl
    > use warnings;
    > use strict;


    ( my $data_pos = tell DATA) >= 0 or die "DATA not seekable";

    > for my $num (1..5){
    > while(<DATA>){
    > s/XX_NUM_XX/$num/;
    > print;
    > }
    > seek DATA, 0, 1;


    You are positioning relative to the current position (second parameter
    WHENCE is 1). An offset of 0 isn't going to change the position. You
    meant "seek 0, 0", but really you want

    seek DATA, $data_pos, 0;

    > print "seek $num\n"; #debug
    > }
    >
    > __DATA__
    > hello XX_NUM_XX
    > world XX_NUM_XX


    Try these changes (untested), it may do what you want.

    Anno
    Anno Siegel, May 17, 2005
    #3
  4. Works perfectly - thanks for the fix and the explanation.

    Cheers,
    Christian
    Chris Marshall, May 17, 2005
    #4
  5. Chris Marshall

    Anno Siegel Guest

    Chris Marshall <> wrote in comp.lang.perl.misc:

    > Works perfectly - thanks for the fix and the explanation.


    Ugh. No attribution, no context. Oh right, X-Trace: posting.google.com.

    Anno
    Anno Siegel, May 17, 2005
    #5
  6. > Anno Siegel wrote:
    >Ugh. No attribution, no context. Oh right, X-Trace:

    posting.google.com.

    Sorry - you're completely right and I should have made that post much
    better. First time I've posted in ages and (as I've since read -
    everyone has found out) was caught out a little by the recent changes
    in Google (But I should have checked the preview first).

    Here is the completed example code with your corrections:

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

    for my $num (1..5){
    ( my $data_pos = tell DATA) >= 0 or die "DATA not seekable";
    # correction from Anno: remember position of DATA
    while(<DATA>){
    s/XX_NUM_XX/$num/;
    print;
    }
    seek DATA, $data_pos, 0; # correction from Anno: seek to
    position of DATA
    print "seek $num\n";
    }

    __DATA__
    hello XX_NUM_XX
    world XX_NUM_XX


    > Anno Siegel wrote:
    > Oh right, X-Trace: posting.google.com


    True...an alternative to searching usenet than via google/groups is one
    thing I've never looked into.
    Could you offer me any advice here ? Both for at home (where I can
    download or purchase any software but may or may not have news
    available by my ISP) or from the office (where I am a lot more
    restricted).

    thanks
    Christian
    Chris Marshall, May 17, 2005
    #6
  7. Chris Marshall

    Anno Siegel Guest

    Chris Marshall <> wrote in comp.lang.perl.misc:
    > > Anno Siegel wrote:
    > >Ugh. No attribution, no context. Oh right, X-Trace:

    > posting.google.com.
    >
    > Sorry - you're completely right and I should have made that post much
    > better. First time I've posted in ages and (as I've since read -
    > everyone has found out) was caught out a little by the recent changes
    > in Google (But I should have checked the preview first).


    Thanks for being so insightful.

    [reading Usenet]

    > True...an alternative to searching usenet than via google/groups is one
    > thing I've never looked into.
    > Could you offer me any advice here ? Both for at home (where I can
    > download or purchase any software but may or may not have news
    > available by my ISP) or from the office (where I am a lot more
    > restricted).


    Myself, I'm using trn, not because it's the best newsreader but because
    it's the one I use. There are certainly more modern ones but trn is good
    at what it does. It compiles on all (well, most) Unix systems. I'm sure
    there are Win* ports too, Google will clear that up easily.

    That's what Google is good at. The Usenet archive is nice, but they should
    pack up their posting interface. It's doing Usenet a disservice.

    What worries me most is that Google uses its immense prestige to turn
    Usenet into Google Groups. Chances are that an arbitrary newcomer to
    Usenet has come via Google, believing it's some kind of semi-static
    web chat-room run by Google. We see examples of that daily. People
    who know the difference may be a minority some not-too-far-away day.

    Anno
    Anno Siegel, May 17, 2005
    #7
  8. Chris Marshall

    David Combs Guest

    trn (was: Re: seek and __DATA__ not behaving how I would hope)

    In article <d6d6i2$a8c$-Berlin.DE>,
    Anno Siegel <-berlin.de> wrote:
    >Chris Marshall <> wrote in comp.lang.perl.misc:
    >
    >Myself, I'm using trn, not because it's the best newsreader but because
    >it's the one I use. There are certainly more modern ones but trn is good
    >at what it does. It compiles on all (well, most) Unix systems. I'm sure
    >there are Win* ports too, Google will clear that up easily.
    >


    NOT to start a *long* off-subject sub-thread, please let me know
    what might be newer and/or better than trn (or trn4, which is
    what I use -- probably you too?).

    Please, short and sweet -- but maybe a sentence or two on what
    you've heard about them.

    Thanks,

    David

    PS: This is to NOT grow into anything more than this and maybe
    two or three followups -- any more, and let's switch to that
    newsgroups on newsreaders...

    Thanks
    David Combs, Jun 4, 2005
    #8
  9. Re: trn (was: Re: seek and __DATA__ not behaving how I would hope)

    David Combs <> wrote:

    > please let me know
    > what might be newer and/or better than trn



    I switched from "trn" to "slrn" a few years ago because I wanted
    its article-scoring features.

    You can also survey User_Agent and/or X-Newsreader headers on posts
    to see what other people are using.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 4, 2005
    #9
    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. Travis Whitton

    IO.seek behaving strangely on FreeBSD 4.9

    Travis Whitton, Apr 1, 2004, in forum: Ruby
    Replies:
    5
    Views:
    201
    Yukihiro Matsumoto
    Apr 2, 2004
  2. botp
    Replies:
    0
    Views:
    117
  3. -did-not-set--mail-host-address

    HTML::TokeParser; __DATA__ as a filehandle

    -did-not-set--mail-host-address, Oct 24, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    132
    Brian Wilkins
    Oct 24, 2006
  4. Mladen Gogala

    Perl __DATA__ construct.

    Mladen Gogala, Jun 25, 2012, in forum: Python
    Replies:
    2
    Views:
    462
    Miki Tebeka
    Jun 25, 2012
  5. Replies:
    3
    Views:
    127
    Andreas Perstinger
    May 14, 2013
Loading...

Share This Page