skip to Main Content

With this Perl and this machine:

$ perl -v
perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux-gnu-thread-multi

$ sb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:    22.04
Codename:   jammy

I ran this snippet of code:

    my $filename=   't/mod_Prototype/SecondModule.t';
    my $nofile  =   chmod 0777, $filenmame;
    printf "%5sNumber of file :: %s - %sn",'',$nofile,$filename;

And I checked if it changed the permissions:

ll t/mod_Prototype/
total 8
-rw-rw-r-- 1 lutz lutz 674 Mar 19 09:00 SecondModule.pm.t
-rw-rw-r-- 1 lutz lutz 674 Mar 19 10:05 SecondModule.t

Here are my questions:

  • Does anyone knows if this command is supported for Ubuntu?
  • And for which OS version it might this perl command work?
  • Or is this function is still a supported part of the Perl language?

I think a system call in perl would get around this,
but I am looking for a pure Perl or language solution.

I tried to create a script file.

I was expecting to make the file executable (aka change the mode of the file).

I like to automate script development and test the perl language set, that are used less frequently.
There are 215 known function/keywords in Perl, but 49% of these keywords are so special that they need to be experiemented to make sure they are still functioning correctly.

No error, no warning were produced, and the correct return value was receive ( 1 for one file in the list) but the command was not effective to change the mode of the file.

2

Answers


  1. Are you using strict? These variables are not the same variable:

    $filename
    $filenmame
    

    Also, check the return value of chmod, it should return the number of successfully changed files.

    Login or Signup to reply.
  2. Yes, chmod functions on Ubuntu. But you haven’t checked if the command succeeded. Typically this is done by checking if it returned a false value. In chmod‘s case it returns the number of files changed, but since you’re only changing one it will return 0 if it fails.

    my $nofile  =   chmod 0777, $filenmame or die "chmod 0777, $filename failed: $!"
    

    You can also do this automatically with autodie.

    Finally, turn on strict and warnings to catch errors like typos in variable names.

    use strict;
    use warnings;
    use autodie;
    
    my $nofile = chmod 0777, $filenmame;
    

    Oops, there’s a typo. $filenmame vs $filename.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search