perl problem - another 'die' issue.

two things.

  1. why doesn't the 'die' message get displayed - "Error: release log directory creation failed..."?
  2. why does the script name and line number get displayed despite the inclusion of a '\n'. apparently adding a newline prevents this from happening.
if (! -d "$logdir") {
    use File::Path;
    mkpath "$logdir" || die ("ERROR: release log directory creation failed - $logdir: $!\n");
    print "\nINFO: Directory created - $logdir\n";
}

the result:

mkdir /build/cm_test_sharee: Permission denied at ./crm.prl line 122

If you look at the error more carefully, it is generated by mkpath(), not yours.

From the File::Path manpage:

As you don't trap the error with eval, the program terminates with the error as expected.

i've now changed the code and it works fine.

if (! -d "$logdir") {
    use File::Path;
    eval { mkpath($logdir) };
    if ($@) {
        print "\nERROR: release log directory creation failed - $logdir: $!\n";
        exit;
    }

i've just started learrning perl and i'm slowly discovering that it makes things harder and longer. in unix i would have done this in one line.

[ -d ${LOGDIR} ] || mkdir -p ${LOGDIR} || { echo "\nERROR: release log directory creation failed - ${LOGDIR}"; exit 1; }

thank you cbkihong.

Not quite.

Don't you think the perl one you wrote looks a bit more descriptive? Using a module, you have to accept that different modules have different behaviour. That is normal. This is just like the '-c' switch of one command and that of another command means totally different things, and you must accept that.

Learning a new thing, you cannot always expect your knowledge of another environment can translate well to it. Perl is a full-blown programming language, and programs naturally need some structure so that no matter you are writing a 30-line or 30000-line program it will still be a manageable piece.

i agree. despite my complaining i think the perl code looks and somehow feels better.

i think this is the source of my complaint. i've been writing so many unix shell scripts, it's clouded my acceptance of perl and the fact that it's bound to be different.