Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Doesn't like a Scalar, as a folder path

by ArifS (Beadle)
on Oct 27, 2014 at 18:23 UTC ( [id://1105186]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to read the folder name from a text file and open the contents of the folder with the following-
my $lines; my @lines; # read (1 line only). FOLDERZ. open INPUT, "<Output1.txt"; @lines = <INPUT>; close INPUT; foreach $lines (@lines){ print "Line: ", $lines, "\n"; my $linesA; #my @linesA = "c\:\\Other\\renamedir\\FOLDERZ"; my @linesA = "c\:\\Other\\renamedir\\$lines"; foreach $linesA (@linesA){ my $directory = "$linesA"; print ("Folder: ", $directory, "\n"); opendir (DIR, $directory) or die $!; while (my $fldr = readdir(DIR)) { print "Files & Folders: ", $fldr, "\n"; } closedir(DIR); } }
Getting the following error-
Line: FOLDERZ Folder: c:\Other\renamedir\FOLDERZ Invalid argument at c:\temp\dir4E63.tmp\lineAF.pl line 22. Press any key to continue . . .
Line 22: opendir (DIR, $directory) or die $!;

It works just fine if I replace $linesA with FOLDERZ. But doesn't like the scalar.
Please let me know.

Replies are listed 'Best First'.
Re: Doesn't like a Scalar, as a folder path
by McA (Priest) on Oct 27, 2014 at 18:47 UTC

    Hi,

    you will probably have a CR/LF at the end of your filename. You have to do a chomp $lines to get rid of it.

    McA

      good call. Totally forgot about that. Thanks.

        This is such a common error case that Perl even outputs a warning for it if you let it. Put use warnings; at the top of your program to tell Perl that you want it to tell you about common programming errors that it can recognize.

Re: Doesn't like a Scalar, as a folder path
by Discipulus (Canon) on Oct 28, 2014 at 08:55 UTC
    Why to you escape the colon in the path?
    it leads to a strange behaviour i cant explain:
    perl -e "print qq(c\:\\other)" c:\other perl -e "print qq(c:\\other)" c:\other #Consider in Perl on win you can use the backslash too in path # perl -e "$path = qq(c:/windows); print qq(Exists and is a directory!\n +) if -e -d $path" Exists and is a directory!

    HtH
    L*
    UPDATE: as explained by Corion this is due to:
    Because \: interpolates to : in double quotes
    Basically, the leading backslash includes the next character as is (unless it's a special combination, like \n, \r , \a, \b , ...

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Basically, the leading backslash includes the next character as is (unless it's a special combination, like \n, \r , \a, \b , ...

      The leading backslash includes the next character as is if it's a non-word character.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1105186]
Approved by crashtest
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-16 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found