Answers to Chapter 14 Exercises
Here’s one way to do it:
chdir "/" or die "Can't chdir to root directory: $!"; exec "ls", "-l" or die "Can't exec ls: $!";The first line changes the current working directory to the root directory as our particular hardcoded directory. The second line uses the multiple-argument
execfunction to send the result to standard output. We could have used the single-argument form, but it doesn’t hurt to do it this way.Here’s one way to do it:
open STDOUT, ">ls.out" or die "Can't write to ls.out: $!"; open STDERR, ">ls.err" or die "Can't write to ls.err: $!"; chdir "/" or die "Can't chdir to root directory: $!"; exec "ls", "-l" or die "Can't exec ls: $!";The first and second lines reopen
STDOUTandSTDERRto a file in the current directory before we change directories. After the directory change, the directory listing command executes, sending the data back to the files opened in the original directory.Where would the message from the last
diego? Why, it would go intols.errsince that’s whereSTDERRis going at that point. Thediefromchdirwould go there, too. But where would the message go if we can’t re-openSTDERRon the second line? It goes to the oldSTDERR. For the three standard filehandles,STDIN,STDOUT, andSTDERR, if re-opening them fails, the old filehandle is still open.Here’s one way to do it:
if (`date` =~ /^S/) { print "go play!\n"; } else { print "get to work!\n"; }Since both Saturday and Sunday start with an S and the day of the week ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access