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

My code doesn't work. It doesn't read the html files
use Cwd 'abs_path'; $dir=abs_path($0); $dir=~s/\\html.pl//g; opendir (DIR, $dir) or die $!; while ($fileName = readdir(DIR)) { if ($fileName=~/\.html/i) { $pathName="$dir\\$fileName"; $pathName=substr($pathName,0,length($pathName)-4); open(DATA, "+<${pathName}html") or die "Couldn't open file file.txt, $ +!"; #open(OUT, ">${pathName}html") or die "Couldn't open file file.txt, $! +"; while(<DATA>) { $line.=$_; } . . . . print OUT $line; } } close (DIR);

Replies are listed 'Best First'.
Re: read and write file in directory
by Corion (Patriarch) on Dec 01, 2014 at 12:54 UTC
    ... #open(OUT, ">${pathName}html") or die "Couldn't open file file.txt, $! +"; ... print OUT $line; ...

    Perl would warn you about this if you used warnings resp. run your script with -w - you print to a filehandle that was never opened.

    You haven't told us how your program fails exactly, but this might be one problem that makes it behave in a way you did not intend.

Re: read and write file in directory
by jellisii2 (Hermit) on Dec 01, 2014 at 12:44 UTC
    Use strict and warnings will possibly be useful.
    Also, a completely self-contained example of the problem would not be remiss here.
Re: read and write file in directory
by BrowserUk (Patriarch) on Dec 01, 2014 at 12:46 UTC

    Change this: $pathName="$dir\\$fileName"; to this: $pathName="$dir/$fileName";


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      may be he is on windows. As per his script it looks like windows path. I am not sure. he has to tell us.


      All is well
        may be he is on windows

        Doesn't matter. All the windows APIs (and thus all of Perl's) accept / as a path separator.

        The problem with his use of \\ is that whilst doubling the backslashes works when he constructs the string, when he interpolates it into another string, the double backslash has been reduced to a single backslash, so it gets seen as escaping the next character in the string and disappears completely.

        All windows/perl users should be aware and use / in preference to \ for paths, it makes life much easier.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: read and write file in directory
by vinoth.ree (Monsignor) on Dec 01, 2014 at 12:34 UTC

    Hi,

    what is the exact error perl gives you?


    All is well
Re: read and write file in directory
by vinoth.ree (Monsignor) on Dec 01, 2014 at 13:10 UTC

    Here the code I tried in my linux box works fine, it just reads a test.html and prints its content.

    use strict; use warnings; use Cwd 'abs_path'; my $dir=abs_path($0); print "$dir\n"; $dir=~s/pl.pl//g; print "after:$dir\n"; opendir (DIR, $dir) or die $!; my $fileName; while ($fileName = readdir(DIR)) { print "$fileName\n"; if ($fileName=~/\.html/i) { my $pathName="$dir/$fileName"; $pathName=substr($pathName,0,length($pathName)-4); open(DATA, "+<${pathName}html") or die "Couldn't open +file file.txt, $!"; while(<DATA>) { print "$_"; } close(DATA); } } closedir(DIR);
    You need to use closedir() to close the directory. close() is for file.

    All is well
Re: read and write file in directory
by Anonymous Monk on Dec 01, 2014 at 15:51 UTC

    Solution is simple, don't use readdir ;) use Path::Tiny  qw/ path /; for my $kid ( path( $dir )->children( qr/\.html$/ ) ){ ...