http://qs1969.pair.com?node_id=753188

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

Salutations, Monks -

A few weeks ago I posted a node about an issue with the map function I had encountered while attempting to make a script PerlCritic-compliant.

I have hit another problem. In an effort to satisfy PerlCritic's complaint about undue complexity, I am attempting to turn the following nested if:

if ( /filename\s*=\s*(\S+)/x ) { $file = basename($1); } if ( /go/ ) { if ( exists $files{$file} ) { push @PLACE, @batch; delete $files{$file}; } @batch = (); }

... into a switch statement. I am doing this because in another instance, it knocked a couple of points off the complexity score. So I'm hoping for the same result here. Anyway, here is the current version of the replacement:

switch ($_) { case qr /filename\s*=\s*(\S+)/ { $file = basename($1);} case qr /go/ { if (exists $files{$file}) { push @PLACE, @batch; delete $files{$file}}} } @batch = ();

... note this is inside a foreach loop. Anyhoo, here is the error this is throwing:

fileparse(): need a valid pathname at ./script line xxx

Thing is, I think I understand what is happening here but not why: essentially, the call to basename() is not finding a good value in $1, which should have been returned from the match by the (\S+). So:

  1. I recall that it is not good to rely on these numbered variables -- what if there really is nothing there? e.g. what if the match was unsuccessful? BUT
  2. It works when the thing is constructed as two IFs, so why doesn't it work here?

I suspect it will sound overweening of me to say so, but I would prefer this not turn into a thread (IF it turns into a thread ... this could be a stupid question!) about the merits and/or drawbacks of PBP and why I should or should not be trying to satisfy the damned thing's complaints. Its use has been mandated, and I'm trying to use it as a learning tool. It's actually kind of fun - like a puzzle.

Besides, all the PBP anti- and for stuff has been well hashed out here, I think. Just my 2c.