Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:53:31+00:00 2026-05-26T19:53:31+00:00

In the perl , how to read the contents between two marks. Source data

  • 0

In the perl , how to read the contents between two marks. Source data like this

START_HEAD
ddd
END_HEAD

START_DATA
eee|234|ebf
qqq|              |ff
END_DATA

--Generate at 2011:23:34

then I only want to get data between “START_DATA” and “END_DATA”. How to do this ?

sub readFile(){ 
    open(FILE, "<datasource.txt") or die "file is not found";

    while(<FILE>){      
        if(/START_DATA/){           
            record(\*FILE);#start record;
        }
    }
}

sub record($){
    my $fileHandle = $_[0];

    while(<fileHandle>){
        print $_."\n";      
        if(/END_DATA/) return ;         
    }
}

I write this code, it doesn’t work. do you know why ?

Thanks

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:53:32+00:00Added an answer on May 26, 2026 at 7:53 pm

    Besides a few typos, your code is not too far off. Had you used

    use strict;
    use warnings;
    

    You might have figured it out yourself. Here’s what I found:

    • Don’t use prototypes if you do not need them, or know what they do.

    Normal sub declaration is sub my_function (prototype) {, but you can leave out the prototype and just use sub my_function {.

    • while (<fileHandle>) { is missing the $ sign to denote that it is
      a variable (scalar) and not a global. Should be $fileHandle.
    • print $_."\n"; will add an extra newline. Just print; will do
      what you expect.
    • if(/END_DATA/) return; is a syntax error. Brackets are not optional
      in perl in this case. Unless you reverse the statement.

    Use either:

    return if (/END_DATA/);
    

    or

    if (/END_DATA/) { return }
    

    Below is the cleaned up version. I commented out your open() while testing, so this would be a functional code example.

    use strict;
    use warnings;
    
    readFile();
    
    sub readFile { 
        #open(FILE, "<datasource.txt") or die "file is not found";
        while(<DATA>) {      
            if(/START_DATA/) {
                recordx(\*DATA); #start record;
            }
        }
    }
    
    sub recordx {
        my $fileHandle = $_[0];
        while(<$fileHandle>) {
            print;
            if (/END_DATA/) { return }         
        }
    }
    
    __DATA__
    START_HEAD
    ddd
    END_HEAD
    
    START_DATA
    eee|234|ebf
    qqq|              |ff
    END_DATA
    
    --Generate at 2011:23:34
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I get Perl to read the contents of a given directory into
I'm trying to read data from SQL Server database using Perl and the DBI
Is there any Perl script to read multiple PDF files and get the number
I am working on a Perl script to read CSV file and do some
Perl seems to be killing my array whenever I read a file: my @files
When I read a directory in Perl with opendir , readdir , and closedir
I recently read Object Oriented Exception Handling in Perl Perl.com article. Is there any
I want to read UTF-8 input in Perl, no matter if it comes from
I have a Perl script that uses WWW::Mechanize to read from a file and
I am trying to read values from an input file in Perl. Input file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.