in reply to Re: Question on File Handler and Subroutines in Perl
in thread Question on File Handler and Subroutines in Perl

Hi thanos1983,

Thank you for your detailed reply! (:

I will take a look at your suggested solution. :) One thing I am still a bit puzzled about, is how to open a file that contains the names of the files in the current directory, and then iterate through them to print out lines that matches the pattern I want. I am currently still working on that.

Thank you once again!

  • Comment on Re^2: Question on File Handler and Subroutines in Perl

Replies are listed 'Best First'.
Re^3: Question on File Handler and Subroutines in Perl
by thanos1983 (Parson) on Mar 05, 2019 at 10:32 UTC

    Hello again Anonymous Monk,

    You are welcome :)

    Update: Sorry I got your last question completely wrong :). You can repeat the same procedure as you open the files and instead of @ARGV use @files.

    Sample of code:

    #!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; my @files = io('fileName.txt')->chomp->slurp; print Dumper \@files; __END__ $ perl test.pl $VAR1 = [ 'a.txt', 'b.txt', 'c.txt' ];

    Update2: I think I finally understood what you mean. You want to provide a file as a parameter to the script that contains the names of the files that you want to search through for the keyword. If this is the case see bellow:

    #!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; sub grepFileFromSubroutine { my @files = io(shift)->chomp->slurp; my @final; foreach my $file (@files) { if ( -e $file ) { # double check existence my @lines = io($file)->chomp->slurp; # Chomp as you slurp if (my @matches = grep( /string/, @lines ) ) { push @final, "Found in File: '$file':", @matches; } } } return \@final; } print Dumper grepFileFromSubroutine(@ARGV); __END__ $ perl test.pl fileName.txt $VAR1 = [ 'Found in File: \'a.txt\':', 'Line two interested, string', 'Found in File: \'b.txt\':', 'Line one interested, string', 'Line two interested, string' ]; __DATA__ $ cat a.txt b.txt c.txt Line one not interested Line two interested, string Line one interested, string Line two interested, string Line one not interested Line two not interested Line three not interested

    If still is not what you want provide us more information to assist you more :)

    Regarding your last question read this past question which contains also examples and a variety of solutions (Find filename that has the pattern in a directory).

    I usually prefer to use File::Find::Rule. A previously asked question with a sample of code with this module see (Re: Capturing and then opening multiple files).

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Hi thanos1983,

      Thank you very much for your detailed reply, and for coming back to my question despite having given a detailed reply previously.

      I am sorry for not wording my question properly too.

      No worries, I think reading the codes given by all of you gave me more ideas on how to write a Perl program in future. :D I have not learnt about the modules and how to deal with the scoping operator as I am only at Chapter 2 of "Learn Perl the Hard Way". I will get there and return to this thread to look at all of your (including the other PerlMonks') answers. (:

      Thank you once again! :D

      I realized you cancelled the sentences in the last 2 paragraphs too. May I ask if those are references for me when I get to the, let's say, more advanced stages of Perl?

      Thank you (:

        Hello again Anonymous Monk,

        I did not cancelled them, I just strike them through (HTML Tag.) as irrelevant to your question.

        I love Perl and in general high level programming languages because they provide the use of modules. When I started coding years ago, I started with C language. For me that was the lowest level language that I started learning. On C you need to implement everything step by step and define everything very specifically. Perl has the beauty that you can call a module for example File::Find::Rule and you can simply use a method from the module by just using one line and you will get the desired output.

        With other programming languages e.g. C you can still do that creating your own function and calling it but you need to do all the coding yourself. This will increase the possibility of having flaws/bugs on your program/script and also you will consume a lot more time.

        So Perl for my point of view provides you this advantage among others :)

        Remember when you have a problem Google is your friend, read many solutions to your problem try to understand them and try them. This way you will learn much more on the flow. Always check past questions on this forum through Super Search you will find great examples. :)

        Hope this helps, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!