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.


In reply to Climbing Mt. Perlcritic -- Switch statements by chexmix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.