Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: solution wanted for break-on-spaces (w/specifics)

by hippo (Bishop)
on Oct 23, 2021 at 22:57 UTC ( #11137928=note: print w/replies, xml ) Need Help??


in reply to solution wanted for break-on-spaces (w/specifics)

Here are a few suggestions to make the code clearer and perhaps then garner more helpful answers:

  • use strict
  • use warnings
  • use Test::More instead of trying to roll your own testing framework
  • Avoid prototypes
  • Avoid localising $_
  • Avoid capture groups which you never use
  • Avoid P. It's fine in your own code but here it is unnecessary (or would be if you used Test::More) and is another barrier to help.
  • Pick a formatting scheme and stick to it. Random whitespace doesn't help.

In summary, help us to help you.


🦛

  • Comment on Re: solution wanted for break-on-spaces (w/specifics)

Replies are listed 'Best First'.
Re^2: solution wanted for break-on-spaces (w/specifics)
by perl-diddler (Chaplain) on Oct 26, 2021 at 17:43 UTC
    re strict/warnings -- they were their and got deleted as I deleted chunks of template-prefix code...*oops*.

    Test::More is what I use for testing not random development -- Test::More is a heavy-weight solution for testing a few example RE's against lines in a file.

    prototypes -- avoid? only when I need to avoid them to make it work. Most of my prototypes are documentary -- in that I put them on Class-methods where they aren't used, with the expectation that the "this" ptr doesn't count.

    localising $_ -- I localise it if I change it's value in a sub -- I don't want to create side effects. In code cleanup I'll often replace them with "my $var"s.

    capture groups -- don't think there were any such that I didn't use. I use (?:...) if I don't use the result.

    Avoid P? If I don't use it, who would? ;-)

    As for being able to 'help' me -- I'm beyond help, but anyone who tried to write a regex seemed to have no problem giving me clues about things that worked or things to try.

      Test::More is what I use for testing not random development -- Test::More is a heavy-weight solution for testing a few example RE's against lines in a file.

      Test::More is in Core so everyone has it and everyone who writes any significant amount of Perl has used it and is familiar with it. The same is not true of your hand-rolled testing framework so when I look at your example code I have to first analyse your testing framework not least because it might be responsible for the underlying problem your code exhibits.

      If Test::More is too "heavy-weight" for you then you can always use the ultra-light Test::Simple instead.

      prototypes -- avoid?

      Yes, avoid!

      localising $_ -- I localise it if I change it's value in a sub -- I don't want to create side effects. In code cleanup I'll often replace them with "my $var"s.
      capture groups -- don't think there were any such that I didn't use. I use (?:...) if I don't use the result.

      Here is your subroutine txt:

      sub txt($) { local $_=shift; my (undef, undef,$txt)=m{^\s*(\d+)\s+(\d+),(.*)}; $txt; }

      It unnecessarily localizes $_ and discards 2 capture groups. Instead it could be written thus:

      sub txt { shift =~ /^\s*\d+\s+\d+,(.*)/; return $1; }

      No need to mess with $_ or declare any lexical variables at all. No need for 3 capture groups when all you want is one. No need for prototypes either.

      Of course you are entirely free to ignore these suggestions but the harder you make it for others to read or run your code the less likely they are to want to unpick it all.


      🦛

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11137928]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2021-12-08 03:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (34 votes). Check out past polls.

    Notices?