Every now and then, there is a discussion whether to shell out to do something, or do it all in Perl. Many purists argue that if it's possible to do it in Perl, you should do it in Perl. Portability and such. And specially if the functionality comes in a standard module.

I got bitten by that today. I was working on a few programs that, in combination with make create a floppy disk to install a small Linux system on a specialized box. Part of the process involves copying executables to the root file system to be. Simple, isn't? Just do

use File::Copy; umask 022 or die "Failed to set umask: $!"; copy $src => $dest or die "Failed to copy: $!";

Well, that was what I did. Later, I needed to copy a directory structure. According to the manual page of File::Copy, that module just deals with copying files, so I shelled out:

system cp => '-a', $src, $dest; die "..." if $?;

But back to File::Copy and copy. The floppy disk was created, I insert it in the box, reboot, and after some time, it fails. /sbin/loader is not executable.

It turns out that File::Copy doesn't respect execution bits. Urg. Out went File::Copy and back came shelling out to the UNIX toolkit.

Here's a program that shows the unwanted behaviour of File::Copy:

#!/usr/bin/perl use strict; use warnings; use File::Copy; umask 022 or die "umask: $!"; my $one = "/tmp/one"; my $two = "/tmp/two"; my $three = "/tmp/three"; foreach my $file ($one, $two, $three) { ! -f $file or unlink $file or die "unlink $file: $!" } # Create first file. open my $fh, "> $one" or die "one: $!"; close $fh; # Make it executable. chmod 0755 => $one or die "chmod: $!"; # Copy it with File::Copy. copy $one, $two or die "copy: $!"; # Copy using the UNIX toolkit. system cp => $one, $three; die "cp: ", $? >> 8 if $?; # And check. print "$one is executable\n" if -x $one; print "$two is executable\n" if -x $two; print "$three is executable\n" if -x $three; __END__ /tmp/one is executable /tmp/three is executable

Abigail


In reply to Pure Perl or the toolkit? by Abigail-II

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.