tbo has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that has to chown everything in a directory. I have preferred system("chown","-R","user:group","$dir")than the perl function chown (It seems easier for me because I have not to read the directory).
So, in general, what is the best way : using Perl functions or system?
Thanks.

Replies are listed 'Best First'.
Re: system or perl function ?
by strat (Canon) on Mar 28, 2002 at 12:51 UTC
    I nearly always prefer using the perl-functions because of the following reasons:
    • Better portability: sometimes external programs are not available under certain operating systems or are called slightly different
    • Easier to eval success, because you don't need to parse the output of a command, just ask for the returncode and perhaps an errormessage
    • Less dependence to environmentvariables like PATH, or at the other hand, to fixed pathes
    • In my eyes, it is a more homogenous program than lots of system calls
    • Security issues

    Reasons for system calls might be:

    • Performance: but that often doesn't matter
    • Better handling (e.g. sendmail)
    • There is no good perl interface to this program available

    But you have to decide what to use, for it's your code :-)

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: system or perl function ?
by RMGir (Prior) on Mar 28, 2002 at 12:47 UTC
    It depends :)

    In your case, the external program does exactly what you want, and you'd need more perl code to get the job done. So calling /bin/chown is fine.

    (BTW, you should probably hardcode the path to chown in your call, in case your path gets messed up, or someone has the chance to drop a hostile executable script named chown upstream of /bin in your path, for instance. I don't know what context this script runs in.)

    However, keep in mind that your script now depends on /bin/chown. I admit it's unlikely that the interface to chown will change, but it's something to keep in mind if you call external programs.

    Obviously, if you'd had to chown only certain files, then using the "perl perl" approach along with the File::Find module would probably be a better choice.
    --
    Mike

      ...and you'd need more perl code to get the job done.

      While it may take more code, using more doesn't have to involve a significant bloat (well, not visually ;)).

      use File::Find qw/ find /; my $uid = getpwnam($uname) or die "$uname: no such user"; my $gid = getpwnam($gname) or die "$gname: no such group"; find( sub { chown $uid, $gid => $_ }, @directories );

      Not bad for the added portability.

          --k.


Re: system or perl function ?
by Dog and Pony (Priest) on Mar 28, 2002 at 12:54 UTC
    There is usually more than one way of doing things, and one should choose the one that gets the job done easiest without compromising security. :)

    If Larry didn't want us to use system() he wouldn't have given it to us...

    Your way of doing it does indeed seem more appropriate for that particular job, however, there are a few things to think about:

    • That call may not be cross-platform if the program should be run somewhere else. On some platforms it may even give errors, while perls built-in functions should just return quietly on unsupported platforms.
    • Make sure noone can put something into $dir from an external source. It is all too easy to execute arbitrary command this way. Don't let it happen to you. :)

    So, in conclusion: the built-in functions has a lot of benefits to them, is the benefit of writing a little less code really bigger? I'm not so sure... A fast opendir, readdir, closedir really isn't that bad, is it? :)


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.