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

  • SEARCH
  • Home
  • 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 6860695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:26:27+00:00 2026-05-27T02:26:27+00:00

I’m currently writing a Perl program that is to read a given file (either

  • 0

I’m currently writing a Perl program that is to read a given file (either command-line or hard-coded) and then recursively print (and open if extension is .bragi) files and directories listed. For example:

~
    hello.bragi
    subdir/
~/subdir
    check.bragi

where

master.bragi:

~/hello.bragi

and

hello.bragi:

subdir/

and

check.bragi:

main.c

The program would open master.bragi, see hello.bragi listed, open it to find a directory listed, open that directory, then repeat.

I currently have this code:

#!/usr/bin/perl -w

use strict;
use File::Basename;

sub isdir {
    return (-d $_[0]);
}

sub isfile {
    return (-f $_[0]);
}

sub getfn {
    my $path = $_[1];
    my (undef, undef, my $ext) = fileparse($_[0], qr"\..*");
    print "arg:\t".$path."\n";
    if ($ext eq ".bragi") {
    open FILE, "<", $path.$_[0] or die $!;
    my @lines = <FILE>;
    foreach my $line (@lines) {
        chomp($line);
        if (isfile($line)) {
        print "file:\t".$path.$line."\n";
        }
        if (isdir($line)) {
        print "DIR:\t".$line."\n";
        opendir my ($dh), $path.$line or die "Filename does not exist: $!";
        my @files = readdir $dh;
        closedir $dh;
        #print $files[0].":\t".$path.$line."/\n";
        foreach my $f (@files) {
            my $next = $path.$line."/";
            getfn($f, $next);
        }
        }
    }
    }
}

getfn("master.bragi", "/home/tekknolagi/twentytwelve/fs/");

Except I get some errors like No such file or directory at ./files.pl line 19, <FILE> line 3.

And I’m not entirely sure what I’m doing. Thoughts?

Expected output (in order):

master.bragi
hello.bragi
check.bragi
main.c
  • 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-27T02:26:28+00:00Added an answer on May 27, 2026 at 2:26 am


    One problem is that you’re not using the core module File::Find. This is designed to make directory traversals easier.

    Another problem is that you have use strict; commented out.

    Another problem is that you don’t create my variables for the parameters to getfn(). At the very least that is aconventional; using good variable names makes it much easier to understand the code.

    I take back the previous comment about File::Find. Here’s a hacked version of your script that seems to work:

    #!/usr/bin/perl -w
    
    use strict;
    use File::Basename;
    use constant debug => 0;
    
    sub isdir {
        return (-d $_[0]);
    }
    
    sub isfile {
        return (-f $_[0]);
    }
    
    my $level = 0;
    
    sub getfn {
        my($file, $path) = @_;
        my (undef, undef, $ext) = fileparse($file, qr"\.[^.]+$");
        $level++;
        print "-->>getfn($level): $file : $path\n" if debug;
        print "arg:\t$file\t$path ($ext)\n" if debug;
        if ($ext eq ".bragi") {
            open my $FILE, "<", "$path/$file" or die "Failed to open $path/$file: $!";
            my @lines = <$FILE>;
            close $FILE;
            foreach my $line (@lines) {
                chomp($line);
                my $fullpath = "$path/$line";
                print "---- $fullpath\n" if debug;
                if (isfile($fullpath)) {
                    print "file:\t$fullpath\n";
                    getfn($line, $path);
                }
                elsif (isdir($fullpath)) {
                    print "DIR:\t$fullpath\n";
                    opendir my ($dh), $fullpath or
                        die "$fullpath does not exist or is not a directory: $!";
                    my @files = readdir $dh;
                    closedir $dh;
                    foreach my $f (@files) {
                        getfn($f, "$fullpath");
                    }
                }
            }
        }
        print "<<--getfn($level)\n" if debug;
        $level--;
    }
    
    getfn("master.bragi", $ENV{PWD});
    

    I created a test environment in the current directory like this:

    mkdir subdir
    echo hello.bragi > master.bragi
    echo subdir > hello.bragi
    echo main.c > subdir/check.bragi
    echo hello > subdir/main.c
    

    The output of the command is:

    file:   /Users/jleffler/tmp/soq/hello.bragi
    DIR:    /Users/jleffler/tmp/soq/subdir
    file:   /Users/jleffler/tmp/soq/subdir/main.c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.