Anusha_26 has asked for the wisdom of the Perl Monks concerning the following question:

I have a text file(like posted below),

anusha ramegowda <perl programming1234> "23rd June 2016" #Thursday, 11:05am# <perl programming5678> problem encountered

I'm trying to read the contents of the text file(titlefile) using

 open (my $fp, "<", $titlefile) or die "Can't open <$titlefile: $!";

It reads all contents except the ones within <chevrons> for e.g. I don't read <perl programming1234> and <perl programming5678>.

I want to read the string within chevrons to able to distinguish between various values. Can anybody please help?

Replies are listed 'Best First'.
Re: Open() : Not reading all lines in my text file
by choroba (Cardinal) on Jun 23, 2016 at 10:43 UTC
    Please, use <code>...</code> tags to improve readability of code and data samples.

    You only showed how you opened the file. You haven't shown how you read the file and how you verify what was read.

    The following works for me:

    #! /usr/bin/perl use warnings; use strict; while (<DATA>) { print; } __DATA__ <perl programming1234> "23rd June 2016" #Thursday, 11:05am# <perl programming5678> problem encountered

    Maybe you checked the output in a browser? You need to inspect the page source to see the tag-like strings, or wrap the whole output in <pre> tags.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Open() : Not reading all lines in my text file
by Anusha_26 (Initiate) on Jun 23, 2016 at 10:16 UTC

    Sorry I messed up with formatting earlier on. I'm re-posting my query again

    Here's my text file below:

    anusha ramegowda

    <perl programming1234>

    "23rd June 2016"

    #Thursday, 11:05am#

    <perl programming5678> problem encountered

    I'm trying to read the contents of the text file(titlefile) using

     open (my $fp, "<", $titlefile) or die "Can't open <$titlefile: $!";

    It reads all contents except the ones within <chevrons> for e.g. I don't read <perl programming1234> and <perl programming5678>.

    I want to read the string within chevrons to able to distinguish between various values. Can anybody please help?

      Your code only shows the opening of the file, but not how you actually read from the file and also not how you output the content. Please post the relevant code, preferrably within <code>...</code> tags, so that we can try to replicate your situation.

      If you are outputting data that is to be interpreted as HTML, note that you will need to escape the chevrons. See for example HTML::Entities on how to properly escape ><& (and some more).

Re: Open() : Not reading all lines in my text file
by perlfan (Parson) on Jun 23, 2016 at 18:34 UTC
    You're only opening the file handle.
    open my $fp, "<", $titlefile or die "Can't open <$titlefile: $!"; my @lines = <$fp>; # now iterate over @lines
Re: Open() : Not reading all lines in my text file
by $h4X4_&#124;=73}{ (Monk) on Jun 24, 2016 at 08:38 UTC

    Here is one way to read the hole file at one time.

    my $File_Data = get_file_data('/path/to/the/file/to/read'); print $File_Data; sub get_file_data { my $titlefile = shift; open F, "< $titlefile" or die "Couldn't open `$titlefile': $!"; local $/ = undef; # Read entire file at once my $contents = <F>; # Return file as one single `line' close F; return $contents; }
    Updated: have to wrap local in a sub.