-3

I've made a Perl script to list the contents of a specified file, but I also want to see the line number of the file content. What functions in Perl enable this?

This is my code:

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

    print "Specify the file you want to look at:\n";
    my $file_name = <STDIN>;
    chomp $file_name;

    open(FH, '<', $file_name) or die "Cannot open $file_name: $!";
    print "This is the listed content of: $file_name\n";

    while(<FH>){
    print $_;
    }

    close(FH);

This is what happens when I run the script, and this is what I would like it to do.

Actual result                 Wished result

Hello                         1. Hello
my                            2. my
name                          3. name
is                            4. is
Janne                         5. Janne
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hackerNuB
  • 13
  • 7

1 Answers1

-1

You can do something like this:

my $line_n = 1;
while(<FH>){
    print "$line_n. $_";
    $line_n++;
}
Jindřich
  • 10,270
  • 2
  • 23
  • 44
  • 2
    See `man perlvar` for a better way. – Shawn Apr 27 '19 at 15:56
  • 3
    What about `$. ` ? Please, don't accept this answer @hackerNuB but have a look at the duplicate question which provides better ways. – Dada Apr 28 '19 at 06:51