Harry: On Tue, Jan 27, 2015 at 9:37 AM, Harry Putnam <rea...@newsguy.com> wrote: > In the following code I hoped to use `use Cwd abs_path;' to not > only produce the absolute path for a named directory but to die if it > could not... thereby getting around having to test the named dir with > -d ... to make sure it was really a directory in the fs. > > It does not have that effect when a bogus directory is passed in, and, > in fact, goes on to the chdir to find out its bogus. > > Why is that? > > ------- 8< snip ---------- 8< snip ---------- 8<snip ------- > > #!/usr/local/src/test/bin/perl > > use strict; > use warnings; > use Cwd 'abs_path'; > > my $tdir = shift; > > $tdir = abs_path($tdir) or die "Can't find <$tdir>: $!"; > > chdir $tdir or die "Can't chdir <$tdir>: $!"; > > print "Hello from $tdir\n";
It would not be very pleasant if most things die() on error because errors are normal and usually we don't want robust programs to crash when something innocent fails, nor do we want to have to wrap every error that we can recover from with eval blocks (i.e., similar to try...catch in other languages) to handle errors. In general the documentation for a function or module should be consulted to learn of how it handles errors. Most often die() implies that something in the program state is invalid and the program cannot continue. This typically implies an "impossible" or "exceptional" situation. When the simple fact that an operation failed is sufficient it is often good enough to return an error code (e.g., undef or an empty list, or perhaps a two-item list with an error as the second item) and let the caller decide if this is a serious error or not. Often the $! variable is set to an appropriate error code that is more descriptive of the error. That works well for many errors. Often die() is called directly by you. When die() is called (and its string doesn't end in a newline) it reports the line where it was called as the source of the error. There is a module, Carp, that is often used to die() from within modules by pointing at your invocation of the module (e.g., where you called the module) as the source of the error instead of the module itself being wrong. Modules will generally tell you if they die() or use Carp, and often that behavior can be toggled on or off with flags. There is also an 'autodie' pragma that allows many system functions to die() automatically. chdir() is supported by it if you're interested in that. I do not believe that the intent of abs_path() is to tell you whether that path exists. It is merely there to resolve a relative path into an absolute path. You might do so in order to create a new file with that path, and then tell the user what that full path is so there is no ambiguity. If it were to die() when that path didn't exist it would be impossible to resolve non-existent paths, which would limit the function's usefulness. I hope that helps. Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/