I wrote a wrapper sub for creating a new directory using make_path. When I pass this sub the octal mode value in different manners I am unable to get make_path happy. I do more than make_path but that is where my problem is so I am simplifying. I am unable to copy and paste so hand typed code follows (please pardon my typos):
#!/usr/bin/perl # # Many comments # use warnings; use strict; use lib'./'; use File::Path qw(make_path remove_tree); use Getopt::Std; use Data::Dumper; use POSIX; use constant ( FALSE => 0, TRUE => 1); sub usage { print STDERR "SetPermissions -d <directory path name> -m <mode i.e. +0777>\n"; } # Get any command line parameters. my %options = (); getopts("d:m:", \%options); # Get the directory path name. my $dirPathName = ""; if (defined($options{d})) { $dirPathName = $options{d}; } else { usage; die "ERROR: the directory path name must be supplied."; } # Get the directory mode. my $dirMode = ""; if (defined($options{m})) { $dirMode = $options{m}; } else { usage; die "ERROR: the directory mode must be supplied."; } ############################################# # Create a directory if it is not already there. # Parameters: # 1) String : The directory path name. # 2) String : The directory mode. # sub smoothMakeDir { my ($dirPathName, $mode) = @_; if ( ! -e $dirPathName ) { my $err; my $saveUmask = umask(); print STDERR "MODE: " . $mode . "\n"; my $usedMode = oct($Mode); umask(0); print STDERR "usedMode: " . $usedMode . "\n"; make_path( $dirPathName, { mode => $usedMode, error => \$err ) ); print STDERR Dumper($err); if ( @$err ) { print STDERR "$!\n"; print STDERR "ERROR: Failed to create the directory: " . $dirPat +hName . "\n"; umask($saveUmask); die "Failed to create a directory"; } umask($saveUmask); } return 0; } print STDERR "\n"; smoothMakeDir( $dirPathName . "1", $dirMode ); print STDERR "\n"; smoothMakeDir( $dirPathName . "2", 0777 ); print STDERR "\n"; smoothMakeDir( $dirPathName . "3", "0777" ); RESULTS: MODE: 0777 usedMode: 511 $VAR1 = []; MODE: 511 usedMode: 329 $VAR1 = []; MODE: 0777 usedMode: 511 $VAR1 = []; ls -al dr----x--t 2 myuserid mygroup 0 Oct 22 11:22 david1 drwxrwxrwx 2 myuserid mygroup 0 Oct 22 11:22 david2 dr----x--t 2 myuserid mygroup 0 Oct 22 11:22 david3
No matter what I try, I am unable to adjust the mode within the sub so that make_path works correctly. I want to pass a variable to the sub with the user's input but it "always" fails. Only the hard coded value works.
I just tried using chmod --> chmod($mode, $dirpathName); <-- while not setting a mode in the make_path call. I get the same results both ways.
It MUST be something in the pass by value to the sub because the print in the sub will print different values depending on what it was passed.
UPDATE:
Using --> $val = oct($val) if $val =~ /^0/; <-- from the oct() Perldoc page flipped the result! At least I can get a value passed by a variable to work correctly now. I still think that there should be a way to handle all value types passed in this "typeless" language!!!!
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |