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

Hi Monks, I'm writing a perl script to download a file from a url. The download part is ok but I need to change the permission of the file. I used chmod u+x <filename>, but every time I run my script I'm having this error: Bareword "u" not allowed while "strict subs" in use at download.pl line 40. Can you help me with this?</>

Here's my sample code

my $filename = "sample.pdf"; chmod u+x $filename;

Replies are listed 'Best First'.
Re: How to use chmod u +x in perl
by hdb (Monsignor) on Nov 25, 2013 at 08:34 UTC

    From the documentation of the Perl command chmod:

    The first element of the list must be the numeric mode, which should probably be an octal number, and which definitely should not be a string of octal digits: 0644 is okay, but "0644" is not.

    No mention of being able to specify "u+x" or the like.

    Also, having not used Unix for a while, what is the reason to mark a pdf file as executable?

      "what is the reason to mark a pdf file as executable?"

      Good question. Perhaps it carries a PAYLOAD (TROJAN). ;) ;)

      Seriously. To the OP; There's precious little reason to add the x bit to a document. In fact, it could potentially cause you grief. If exposed to the wild (it's within your web space, or those other than yourself have access), it could be used in malicious ways.

      Perhaps you would find one of the following perms more suitable
      0644
      0444
      In fact, unless you actually need to alter the contents of the file. 0444 would be your best choice.

      --Chris

      #!/usr/bin/perl -Tw
      use Perl::Always or die;
      my $perl_version = (5.12.5);
      print $perl_version;
Re: How to use chmod u +x in perl
by Corion (Patriarch) on Nov 25, 2013 at 08:19 UTC

    Have you looked at what the documentation about chmod has to say? The examples show the various allowed usages of chmod. Note that chmod takes a string. Perl sees u+x as the numeric addition of the strings "u" and "x", and thankfully you are using strict so it prohibits using barewords as strings.

Re: How to use chmod u +x in perl
by GrandFather (Saint) on Nov 25, 2013 at 08:18 UTC

    You need to use qx() or `` (back ticks) to execute a command line from within Perl (there are other ways as well, but lets keep it simple for now):

    my $filename = "sample.pdf"; qx(chmod u+x '$filename');

    The single quotes around the file name are only needed if you have unquoted spaces or other special characters in the file name, but do no harm.

    See Quote and Quote like Operators.

    True laziness is hard work
Re: How to use chmod u +x in perl
by daxim (Curate) on Nov 25, 2013 at 10:57 UTC
    Best solution:
    use File::chmod qw(chmod); chmod('u+x', 'sample.pdf');
Re: How to use chmod u +x in perl
by Anonymous Monk on Nov 25, 2013 at 09:45 UTC
    Basically, you need to stat() the file to get its mode and then do a binary OR with 0100 (which I think is u+x) and then chmod $mode, $filename;
    my $mode = (stat($filename))[2]; my $new_mode = $mode | 0100; chmod $mode, $filename);
      A few typos crept into that answer, but I'm sure you'll spot them...
Re: How to use chmod u +x in perl
by taint (Chaplain) on Nov 25, 2013 at 14:14 UTC

    Because TMTOWTDI. May I suggest you can also

    qx(chmod 0705 '$filename');
    or
    qx(chmod 0755 '$filename');
    These would circumvent any possibility of Perl's misunderstanding the "u+x". If that actually applies.

    --Chris

    #!/usr/bin/perl -Tw
    use Perl::Always or die;
    my $perl_version = (5.12.5);
    print $perl_version;

      Just as what GrandFather was suggesting, this fails badly if $filename has a ' in the name. IF you'd choose a system call instead of the builtin chmod, then at least do it the safe way:

      system "chmod", "u+x", $filename;

      If someone cares to set the x bit on a PDF document, it is likely that the document name is littered with unexpected characters.

      In the end I'd advice NOT to use system or qx{} to do a simple chmod. Not even to give a fast and simple workaround. I think it is bad advice.


      Enjoy, Have FUN! H.Merijn