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

Greetings Monks, I currently have a perl script that generates a new file every time it is run. After it has completed, I want it to change permissions on that file. What I'm currently using is
close $outfile; system 'chmod 777 $outfile';

However, this gives me an error every time I execute the script, and it does not change the permissions

chmod: missing operand after `777'

Any ideas as to what I'm doing wrong here? Any and all help is, as always, highly appreciated.

Replies are listed 'Best First'.
Re: Change permissions after file is created
by marto (Cardinal) on Jul 08, 2014 at 20:35 UTC
Re: Change permissions after file is created
by AppleFritter (Vicar) on Jul 08, 2014 at 20:27 UTC

    Guessing from the two lines of code you shared, $outfile most likely contains a filehandle, not a filename. Pass a filename to chmod instead, and all should be fine.

    EDIT: also, as boftx pointed out, single quotes will keep perl from interpolating the variable into your string, so $outfile is being interpreted by your shell instead -- and most likely evaluates to the empty string, hence the error message.

      Good catch on the filehandle. I overlooked that and just focused on a scalar inside single quotes.

      You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: Change permissions after file is created
by Laurent_R (Canon) on Jul 08, 2014 at 22:50 UTC

    1. Don't use system commands on filehandles, but on actual files (file names);

    2. Use the internal Perl chmod function rather than a system call;

    3. The internal system call requires an octal number for permissions. So try something like:

    chmod 0777, $outfile;
Re: Change permissions after file is created
by boftx (Deacon) on Jul 08, 2014 at 20:29 UTC

    Try using double quotes (") instead of single quotes (') so that $outfile is interpolated into the string. Beyond that, read the docs for system and switch to the multi-argument style to avoid some nasty security holes.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.