Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Change the behavior of Perl's IRS

by LighthouseJ (Sexton)
on Jul 14, 2007 at 19:59 UTC ( [id://626661]=note: print w/replies, xml ) Need Help??


in reply to Re: Change the behavior of Perl's IRS
in thread Change the behavior of Perl's IRS

But see, that's precisely what I'm trying to avoid, anything except the absolute minimum of code which I'd like to think Perl strives for.  All I want to do is write something like the following and have it work properly.
{ $/ = 'myrecordsep'; while (<DATA>) { # do the actual work on text here } } __DATA__ myrecordsep field1=item1 field2=item2 myrecordsep ...
I want Perl to read a chunk at a time following that model, that's absolutely all I'm looking for. Like I mentioned before, I've written different scripts that utilized different methods but I'm exploring this particular avenue. I appreciate the attention to the problem though.
"The three principal virtues of a programmer are Laziness, Impatience, and Hubris. See the Camel Book for why." -- `man perl`

Replies are listed 'Best First'.
Re^3: Change the behavior of Perl's IRS
by BrowserUk (Patriarch) on Jul 14, 2007 at 20:10 UTC

    It's not complicated, just discard the first separator:

    #! perl use strict; { $/ = "myrecordsep\n"; scalar <DATA>; ##discard the first; while (<DATA>) { chomp; print "'$_'\n"; } } __DATA__ myrecordsep field1=item1 field2=item2 myrecordsep field1=item1 field2=item2 myrecordsep field1=item1 field2=item2 myrecordsep field1=item1 field2=item2

    Produces:

    C:\test>junk2 'field1=item1 field2=item2 ' 'field1=item1 field2=item2 ' 'field1=item1 field2=item2 ' 'field1=item1 field2=item2 '

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Change the behavior of Perl's IRS
by ikegami (Patriarch) on Jul 15, 2007 at 07:57 UTC

    But see, that's precisely what I'm trying to avoid, anything except the absolute minimum of code which I'd like to think Perl strives for.

    True. This is often done by placing reusable code in modules. I wrote the solution to be reusable so you could place it in a module. All that's left is two lines:

    my $rec_reader = make_rec_reader('myrecordsep'); while (my $rec = $rec_reader->($fh)) { ... }
        Sounded to me that he wanted alternatives to what he already had.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://626661]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2025-03-17 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (54 votes). Check out past polls.