Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

how to debug an 'undef' that cannot be?

by perl-diddler (Hermit)
on Mar 05, 2013 at 05:11 UTC ( #1021747=perlquestion: print w/ replies, xml ) Need Help??
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...

Comment on how to debug an 'undef' that cannot be?
Select or Download Code
Re: how to debug an 'undef' that cannot be?
by kielstirling (Scribe) on Mar 05, 2013 at 05:20 UTC
    my $dev; while (1) { ***** problem!! local scoped $dev. my $dev = (stat $path)[0]; }
      Crud!

      IT **WASN'T** in a loop before I ran into the problem. ....

      ok... gonna go back and see about reproducing this w/they above fixed...

      MAN -- I hate it when I add broken debugging to an already broken prog!

      So friggin' brilliant! ;-/

Re: how to debug an 'undef' that cannot be?
by AnomalousMonk (Monsignor) on Mar 05, 2013 at 07:27 UTC
    ... the fact that it happens randomly is a bit disconcerting...

    ... especially since, for the reason kielstirling has already pointed out, I don't see any way that  $dev could possibly be anything other than undefined at that point in the given code!

Re: how to debug an 'undef' that cannot be?
by greengaroo (Hermit) on Mar 05, 2013 at 15:02 UTC

    Just to re-phrase what my fellow monks already pointed out, your problem is that you have two different $dev variable. When you use "my" in front of a variable, its scope becomes the current block. In the main program it's not a problem but in a while loop like you did, or any other kind of blocks, the variable will cease to exists at the end of said block, and will be recreated for each iteration, and it will never alter the content of the original variable defined outside of this block.

    In clear: remove the "my" besides your $dev variable inside your while loop because it creates a separate variable, not related to the one you think you are using.

    Good luck!

    Testing never proves the absence of faults, it only shows their presence.
      *Bingo*

      Data: smugbatch-006-2.1.3.x86_64.rpm smugbatch-006-2.4.1.x86_64.rpm Error: cannot stat, already deleted?) path=/Share/suse/distribution/12.1/repo +/oss/suse/test2/smugbatch-6-2.1.3.x86_64.rpm, dev=(undef)

      of course 'V' == "006"

      *sigh*...thanks all!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1021747]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-07-24 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls