Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Best bet would be to stick with the same conventions Perl uses, this way you can maintain a perlish feel to your code making use of 'and' and 'or' so on and so forth. So returning 0, an empty string, or undef are all valid canidates.

Also consider using negative error codes for non fatal errors. I.e all data was present, but a field had some invalid element. This may not be a reason to 'die', but to send it through an extended scrubbing routine.

Just a comment on style, try to segregate lines which do functionally different things. This way it is clear when reading the code that lines grouped together are doing something similar. I have included your code below and spaced it out along what I mean. Also try to not "hardcode" values in your scripts, as this can lead to numerous possibilities for typos. This is just my personal opinion and YMMV, and offered as constructive criticism.

#!/usr/bin/perl my $dir = "/tmp/store"; my $sleep = '1'; opendir DH, $dir or die "Opendir $dir: $!\n"; while (1) { for ( grep !/^\.+$/, readdir(DH) ) { my $file = join('/', $dir, $_); # split $_ on _ and return the second element # this avoids a unnecessary temp array my $mobile = ( split /_/ )[1]; # Is the extra whitespace cleanup really necessary? $mobile =~ s/(?:^\s+|\s+$)//g; # drop spaces from front and back $mobile =~ s/\s+/ /g; # sanitize the rest my $old_sep = $/; # being paranoid, save it for later undef $/; open FH, $file or die "open $file: $!\n"; chomp( my $data = <FH> ); close FH; $/ = $old_sep; # restore it, in case other funcs expect the defa +ult # im not sure what the next few lines are doing, as they are # working on $data or $_, almost randomly. my @fields = split; my $appcode = $fields[0]; # this is never used again? my $message = $data; s/^\s+//, s/\s+$//, s/\s\s+/ / for $data; # should this be $mess +age? sendtolive($mobile, $message); # Sends data through socket conne +ction unlink("$file"); # remove file once read } # END for ( grep readdir(DH) ) closedir(DH); sleep $sleep; } # END while 1

use perl;


In reply to Re: Re: Re: Re: Re: network socket send loop is very cpu intensive by l2kashe
in thread network socket send loop is very cpu intensive by Anonymous Monk

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 browsing the Monastery: (4)
As of 2024-04-19 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found