http://www.perlmonks.org?node_id=1021747

perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:

I have a program segment here that I call repeated.

Thing is, it dies randomly with a 'undef' in a line that, I don't see how it could be undef ...

###################################################################### +######### { package RecycleBin; use P; use Cwd; use PathCat; my $dirsep = &PathCat::_fs_path_sep; sub recycle ($) { state $cwd//= getcwd(); my $path = $_[0]; if (substr $path, 0, 1 ne $dirsep) { $path =~ s{^(?:./)*}{}; $path =~ s{(?:/.)*$}{}; $path = pathcat($cwd, $path); } my $dev; while (1) { my $dev = (stat $path)[0]; Pe "path=%s, dev=%s", $path, $dev; $dev && last; } my ($dh, $dl) = ($dev >> 8, $dev & 0xff); #line 251 in prog #Pe "file %s on dev %s,%s, \N{U+83}\n", $path, $dh, $dl; my $partname; { open(my $ph, "<", "/sys/dev/block/$dh:$dl/dm/name") || die "can't open volume name: $!\n"; $partname = <$ph>; chomp $partname; } #Pe "name %s \N{U+83};", $partname; my $mnt_pnt; { my $mh; open($mh, "<", "/proc/mounts") or die "Can't open mount table: $ +?\n"; my @table = <$mh>; my $qpn = quotemeta($partname); my @mps = grep m{$qpn}, @table; if (@mps < 1) { die "Cannot find $partname in mount table"; } $mnt_pnt = (split /\s+/, $mps[0])[1]; chomp $mnt_pnt; } my $recycle_bin; if (!-d ($recycle_bin = pathcat($mnt_pnt, ".recycle"))) { mkdir $recycle_bin, 1777 || die "Cannot create recycle bin under mount $mnt_pnt: $ +!"; chmod 1777, $recycle_bin || die "Cannot set correct mode:$!"; } $_ = $path; my @cmd = qw( cp -aflx --parents --strip-trailing-slashes ); push(@cmd, $_, $recycle_bin); my $status = system @cmd; $status >>= 8; $status &= 0xf; if ($! = $status) { Pe "Could not link %s into %s", $_, $recycle_bin; } else { unlink $_ or Pe "Could not finish move (unlink old file) of %s", + $_; } } ## end sub recycle ($) 1; }
The whole prog generates a bunch of files to be deleted -- so I move them into a recycle bin... output looks something like:
Read 12161 rpm names. Use 9 procs w/1353 items/process #pkgs=10161, #deletes=2000, total=12161 Recycling 2000 duplicates...path=/Share/suse/distribution/12.1/repo/os +s/suse/test2/libgtk-2_0-0-2.24.7-2.3.1.x86_64.rpm, dev=65032 Use of uninitialized value $dev in right bitshift (>>) at /home/law/bi +n/remove-oldver-rpms-in-dir.pl line 251.
---------------------------------------
Note that I put a print right before that statement -- I even put a friggin ('scue the language) loop that shouldn't exit except on defined($dev)! (Sometimes we get a bit crazy in debugging).

Yet despite the print claiming that dev = 65032 and despite, the program logic claiming that $dev has a value, the very next statement gets an undef warning.

Any ideas on how to approach this? -- the one it deletes is NOT the first 1 on the delete list... I.e. if I run this program successive times, it makes progress through the 'delete' list until it approaches 0. But, off hand, I'm stumped as to how 'dev' could be equal to undef! GRR....

FWIW, pathcat looks like this:

{ package PathCat; use warnings; use strict; use 5.12.0; our @ISA=qw(Exporter); our @EXPORT=qw{pathcat Set_sep($) Fs_path_sep() pathcat(@)}; use Exporter; our $_sep = '/'; sub Set_sep ($) { $_sep = $_[0]; } sub Fs_path_sep () { $_sep } sub _fs_path_sep() { $_sep } sub pathcat(@) { my $lval; shift while (! defined $_[0] ) and @_>=1; defined $_[0] ? ( @_ >= 1 ? $_[0] . ( ( length ($lval=&path_cat($_[1], @_[2 .. $#_ ])) and length $_[0] and $_[0] ne _fs_path_sep ) ? _fs_path_sep : '' ) . $lval : $_[0] ) : $_[1] // '' } sub path_cat(@) { goto &pathcat } #old name compat 1;};
(and P should be fetchable *somehow( off of cpan...)

It's not hugely important -- as it's a util I don't run very often, but the fact that it happens randomly is a bit disconcerting...