Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2024-04-16 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found