_learning perl_, ch12, 7.I.2002, nh DIRECTORY ACCESS changing directories in perl uses the 'chdir' command ^^^^^ takes one argument ------------------------------------------------------- | chdir ("/etc") || die "cannot cd to /etc ($!)\n"; | ---~~~~~----------------------------------------------- "every process has its own current directory. when a new process is launched, it inherits its parent's current directory, but that's the end of the connection. if your perl program changes its director, it won't affect the parent shell that launched the perl process. likewise, the processes that the perl program creates cannot affect that perl program's current direectory. the current directory for these new processes are inherited from the perl program's current directory." - p. 130 globbing ~~~~~~~~ returns the same that an echo command with the same parameters would: @a = ; # @a gets all files that begin with 'host' @a = glob("/etc/host*"); # in the /etc directory | `-> either one works fine multiple arguments permitted: @a = ; if you want to interpolate a variable *by itself*, do it like: <${var}> -- otherswise it'd be a filehandle!! directory handles ~~~~~~~~~~~~~~~~~ * use all UPPERCASE * use the directory handle to read a list of files w/in the directory * always read-only opening & closing directories ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ opendir(ETC,"/etc") || die "cannot opendir /etc: $!\n"; closedir(ETC); # automatically closed before they're reopened # or at the end of the program reading a directory handle ~~~~~~~~~~~~~~~~~~~~~~~~~~ read list of names with readdir ^^^^^^^ takes one parameter - the direcotry handle * each invocation returns the next filename (just the basename, not the complete path) * if no more names, returns "undef" opendir(ETC,"/etc") || die "no etc?: $!\n"; while ($name = readdir(ETC)) { print $name\n"; } closedir(ETC); ***sorted list of names*** opendir(ETC,"/etc") || die "no etc?: $!\n"; foreach $name (sort readdir(ETC)) { print "$name\n"; } closedir(ETC);