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

This is a small idea I'm working on.. it should open the directory where the favorite links' files are (in my case: C:\WINDOWS\Desktop\Perl\scriptz\miei\opendir\1 ) but I get an error : bad file descriptor at line 10 ..

what can I do? I guess somebody already solved this problem because it seemed so obvious at the beginning but I can't figure out what is wrong..

$dir='C:\Windows\Desktop\Perl\scriptz\miei\opendir\1\ '; opendir(DIR, $dir) || die "no $dir?: $!"; @filez=<DIR>; foreach $name (sort readdir(DIR)) { print "$name\n"; open FU, $name || die "can't read URL :$!"; my @contfile=<FU>; $url=substr($contfile[1],8,); print "$url\n"; close FU || die "$!"; }
I thank you all in advance and hope you will excuse me for my ignorance of this language :)

Replies are listed 'Best First'.
Re: Favorite Links printer
by tachyon (Chancellor) on Feb 06, 2003 at 00:28 UTC

    Your path delimiters are invalid. You need to use \\ to get a literal backslash (although you can use / in perl on windows and all will be OK) - see Paths in Perl. You don't want a space after the last \\ Also you need a chdir() or the full path for your open. Finally if you use || you need to do open(FU, $name) || die 'blah' with parentheses due to binding precedence of || being higher than the , operator. Alternatively most people use or operator as shown where you can skip parentheses as or is lower binding precedence than , This should work:

    Note you would expect that '\' should represent a literal \ (non interpolating single quotes) but perl sometimes gets confused with that so you typically use "\\" which works reliably.

    $dir="C:\\Windows\\Desktop\\Perl\\scriptz\\miei\\opendir\\1\\"; opendir DIR, $dir or die "no $dir?: $!"; foreach $name (sort readdir(DIR)) { # skip anything that is not a file ie . .. and other dirs next unless -f $dir.$name; print "$dir$name\n"; open FU, $dir.$name or die "can't open $dir.$name :$!"; my @contfile=<FU>; $url=substr($contfile[1],8,); print "$url\n"; close FU or die "$!"; }

    $countfile[1] is the second line in your file as the first line is $countfile[0]. If you just want a single line you only need to read the file that far ie

    my $line; my $line_you_want = 2; $line = <FU> for 1..$line_you_want; # $line will now contain just the content of line number $line_you_wan +t;

    This avoids reading the whole file into memory and storing it in an array when you only want one line. If you actually just want the first line only than you can use $line = <FU>; which will just read in the first line.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Favorite Links printer
by blokhead (Monsignor) on Feb 06, 2003 at 01:06 UTC
    I don't think you want the @filez=<DIR> line. For one, you don't even seem to be using that array. Also, the angle bracket operator is looking for the filehandle at DIR, and not a dirhandle. If you want the list of files, use readdir like you have in the foreach loop.

    I don't know if this is the source of your error, but you may want to get rid of it anyway. BTW, knowing which line is line 10 would help a lot!

    blokhead