Basic Directory Manipulation

#!/usr/bin/perl -w
############  Directories:  The Glob, Opendir, and Readdir Functions  ########
use Cwd;
print "Diamond glob operator\n";
print "---------------------\n\n";
foreach $i (@arr = <$ARGV[0]/*.pm $ARGV[0]/*.pl>) 
{
     print "$i\n";
}
print "Number of elements: ", scalar(@arr),"\n\n";

print "Glob function with multiple arguments\n";
print "-------------------------------------\n\n";
foreach $i (@arr = glob("$ARGV[0]/*.pl $ARGV[0]/*.pm")) 
{
     print "$i\n";
}
print "Number of elements: ", scalar(@arr),"\n\n";

print "Using readdir in a scalar context\n";
print "---------------------------------\n\n";
opendir(D, "junk") || die "Cannot open junk.\n";
while (defined($name = readdir D))  
{
     print $name,"\n" if $name =~ /(pl|pm)$/;
}
print "\n\n";

print "Using readdir in an array context\n";
print "---------------------------------\n\n";
opendir(D, "junk") || die "Cannot open junk.\n\n";
@names = readdir(D);  ###### Will it work in an array context?
foreach $name (@names)
{
     print "$name\n" if $name =~ /(pl|pm)$/;
}

###########################  Does Readdir Expand ~  ?  ######################

opendir(D, "~/perl/junk") or print "Cannot open ~/perl/junk\n\n";

########################   What About Glob and ~ ?  ##########################

@allfiles = glob("~/perl/junk/*");
print shift(@allfiles),"\n" while @allfiles;
print "\n";

##############  Using $ENV{HOME} Instead of ~ to Get the Home Dir.  ##########

opendir(D, "$ENV{HOME}/perl/junk") or die "Cannot open it!\n";
@allfiles = readdir D;
print shift(@allfiles),"\n" while @allfiles;
print cwd(),"\n";
#######################  Important Note:
#######################  Glob is the ONLY directory/file function which
#######################  can expand ~ -- all the directory/file commands
#######################  we have seen (such as open, readdir, etc.)
#######################  and will see later (such as copy, rename, etc.)
#######################  MUST use $ENV{HOME} for the home directory.
$ dirs.pl junk     #######  Output resulting from this lies below!!

Diamond glob operator
---------------------
junk/newpkg.pm
junk/pkg.pm
junk/defined.pl
Number of elements: 3

Glob function with multiple arguments
-------------------------------------
junk/defined.pl
junk/newpkg.pm
junk/pkg.pm
Number of elements: 3


Using readdir in a scalar context
---------------------------------
pkg.pm       #######  Notice that readdir does NOT precede filename
newpkg.pm    #######  with directory -- even if you give a directory
defined.pl   #######  spec (see output below).  Remember, you can
             #######  always glob(".") to get the current directory!

Using readdir in an array context
---------------------------------
pkg.pm
newpkg.pm
defined.pl

Cannot open ~/perl/junk     #######  Readdir did not understand ~ !!!!

Glob of junk directory
----------------------
/mnt/diska/staff/jwp2286/perl/junk/a
/mnt/diska/staff/jwp2286/perl/junk/c
/mnt/diska/staff/jwp2286/perl/junk/d
/mnt/diska/staff/jwp2286/perl/junk/defined.pl
/mnt/diska/staff/jwp2286/perl/junk/newpkg.pm
/mnt/diska/staff/jwp2286/perl/junk/pkg.pm
/mnt/diska/staff/jwp2286/perl/junk/x

Readdir of junk directory    ########  Notice LACK OF SORTED ORDER!!
-------------------------    ########  Use sort(readdir D) for sorted order.
.
..
pkg.pm
newpkg.pm
defined.pl
x
d
a
c
.foo
.bar

/mnt/diska/staff/jwp2286/perl   #####  Return value of cwd()