perlquestion
chexmix
Salutations, Monks -
<p>A few weeks ago I posted a <a href="http://www.perlmonks.org/?node_id=743445">node</a> about an issue with the map function I had encountered while attempting to make a script PerlCritic-compliant.
<p>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:
<c>
if ( /filename\s*=\s*(\S+)/x )
{
$file = basename($1);
}
if ( /go/ )
{
if ( exists $files{$file} )
{
push @PLACE, @batch;
delete $files{$file};
} @batch = ();
}
</c>
<p>... 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:
<readmore>
<c>
switch ($_)
{
case qr /filename\s*=\s*(\S+)/ { $file = basename($1);}
case qr /go/ { if (exists $files{$file})
{ push @PLACE, @batch; delete $files{$file}}}
} @batch = ();
</c>
<p>... note this is inside a foreach loop. Anyhoo, here is the error this is throwing:
<c>
fileparse(): need a valid pathname at ./script line xxx
</c>
<p>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:
<p><ol>
<li>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
<li>It works when the thing is constructed as two IFs, so why doesn't it work here?
</ol>
<p>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.
<p>Besides, all the PBP anti- and for stuff has been well hashed out here, I think. Just my 2c.
</readmore>