Re: syntax differences and performance

2004-10-25 Thread Brian Gerard
And the clouds parted, and Adamiec, Larry said...
 Given the following lines:
 
 $MESSAGE .= PApplication Fee: B . $price .  Dollars U.S./B;
 
 $MESSAGE .= PApplication Fee: B$price Dollars U.S./B;
 
 
 Is there a performance issue (or any other issue) for using either of
 the above lines?  Does it make any difference which is used?
 
 Suppose it changed to the following:
 
 $MESSAGE .= PApplication Fee: B . $query-param('price') . 
 Dollars U.S./B;
 
 or
 
 $MESSAGE .= PApplication Fee: B$query-param('price') Dollars
 U.S./B;
 
 I have used the first two lines without any noticeable difference, but I
 didn't use any sort of debugging/timer tools either.  I am debating with
 myself about which line from the second set I should use.  In all
 instances the correct value of price was inserted into the variable
 $MESSAGE.
 
 Larry

Hi Larry-

The first thing I would suggest is to check out the Benchmark::Timer
module off of CPAN.  You can time just about any section of your code
and print a report at the end to help you see where your optimization
time would best be spent.  Let me know if you would like some example
code to see how to use it.

Using this and a simple (contrived) script, I ran some tests and it
looks like the inline versions (foo $var bar) might be slightly slower
than their append counterparts (foo  . $var .  bar), although the
timings sometimes drifted in the other direction based on how many trials
I ran (more trials == inline gets faster), a result I suspect can be
attributed to caching at some level.  Basically, there wasn't enough
deviation to be able to point to either one as significantly slower than
the other.  One of the heavier technical perldocs may have the answer, but
I did a quick scan through 'perldoc perltoc' and nothing jumped off the
page at me.  YMMV.

So I would suggest running *your*own* tests on some test data of your own
to get a better idea what will work best in *your* environment.  Also keep
in mind that unless your script does several thousand of these assignments
or your benchmarking shows that the majority of your script's runtime is
spent in these assignments, you probably won't see much of a difference
in performance by figuring out which one saves you 3 ms over the other. :)
I'm all for optimizing code just for the sake of good programming, but
if you're trying to ameliorate a performance problem, you want to look for
the part of your script where it's spending the most time.

HTH-
Brian


PS - $query-param('price') should be $query-param{'price'} - curly
 brackets for hashes, parens for subroutines.


  /~~\
 | Brian Gerard Dawn is nature's way of telling you to|
 | First initial + 'lists'  go to bed. And to just stay there until   |
 | at technobrat dot com  the evil yellow disk is gone again. |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: File Size Calculator

2004-08-10 Thread Brian Gerard
And the clouds parted, and SilverFox said...

 Hi all, I'm trying to writing a script that will allow a user to enter a 
 number and that number will be converted into KB,MB or GB depending on the 
 size of the number. Can someone point me in the right direction?
 
 Example: 
 user enter: 59443
 Script will output: 58M
 
 SilverFox
 

Here's a little chunk that should give you about what you're looking
for, up to Tebibytes (2**40 bytes).  Note that I used the binary
prefixes[1] (Kibi, Mebi, Gibi, Tebi) as opposed to the base-10 versions
(Kilo, Mega, Giga, Tera).  Feel free to change them if you're so
inclined.  :)

--- Begin Chunk ---

our %ByteCount = (
  B = 1,
KiB = 2**10,
MiB = 2**20,
GiB = 2**30,
TiB = 2**40
);


sub prettybyte {
my $bytes = shift;

foreach my $unit ( qw{ TiB GiB MiB KiB B } ) {
if ($bytes = $ByteCount{$unit}) {
return sprintf(%4.3f $unit, $bytes/$ByteCount{$unit});
}
}
}

--- End Chunk ---

HTH[2]-
Brian

[1] http://www.alcyone.com/max/reference/physics/binary.html
-anyone remember offhand the URL to the /. story on these, btw?

[2] I'm a little rushed at the moment, so I don't have time to fill
in any details of how it works.  Let me know if you want/need an
explanation and I'll be happy to provide one.  :)


  /~~\
 | Brian Gerard  Some drink at the fountain of|
 | First initial + 'lists'  knowledge...others just gargle.   |
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: File Size Calculator

2004-08-10 Thread Brian Gerard
And the clouds parted, and Brian Gerard said...
 
 [1] http://www.alcyone.com/max/reference/physics/binary.html
   -anyone remember offhand the URL to the /. story on these, btw?
 

...never mind.  Found it.  (uncaught typo on my first google query... DOH!)

http://slashdot.org/articles/99/08/10/0259245.shtml


  /~~\
 | Brian Gerard  Give me liberty or give me something |
 | First initial + 'lists' of equal or lesser value   |
 | at technobrat dot com   from your glossy 32-page catalog.  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Favorite email parser?

2004-06-29 Thread Brian Gerard

Hi all-

I'm about to start a project that goes a little something like this...

Right now, alerts get automatically emailed to a central address which due
to the volume is largely ignored.  You know the drill: Ooo! It would be
great if we got notified when resource X is running low!  Then we could
anticipate the need for it and get ahead of the game.  Repeat for resources
A-Z, AA-ZZ, etc, etc and you get a mailbox that quickly becomes filled with
alerts that no one reads, largely due to the fact that the reading alone
would be a full time job.  We can receive anywhere from a couple of hundred
to a couple of thousand of these per day.

So I'm planning to set up the address to forward to a backline server, where
my Brilliantly Written(tm) perl script will crunch said alerts.  I'd like it
to chop them up into little bits and insert the bits into an
intelligently-structured database of some sort.  I'm planning on Postgres at
this point.

Then there will be another script, available to the proper people via an
internal website, where queries can be made against the database according to
a yet-to-be-designed web interface.  The main thing I want is a simplification
of the alert viewing.  So you might see 3245 instances of alert Y, which
could be drilled down into based on various criteria, rather than the first
page (out of 65) of 50 alerts all of the same type.  Statistical trending and
other such pie-chart-able things may show up in the future, but not for now.

So my questions are these:

1) Does this sound like a reasonable approach to the problem, given the
relatively low amount of detail I've provided?  Anyone dealt with this type of
thing before and have any gotchas for me?

2) From what I've read, Postgres beats MySQL performance-wise, which is why I
chose it, but IANADBA.  Any votes on a free DB to use for this?  Obviously, it
needs to have a perl interface or CPAN module available.  Regardless of which
DB you like, what's your favorite module for interacting with it?  ...and,
umm, if you could point me to any Getting Started docs, that would be just
lovely.  :)

3) Which module would you suggest for parsing email?  There are a few of them
out there, and I really don't have the time to try each one out until I find
the one I like the best.  :)

Answers to any or all of the above gratefully received!

aTdHvAaNnKcSe-
Brian


  /~~\
 | Brian Gerard  O'Reilly is to a system administrator|
 | First initial + 'lists'   as a shoulder length |
 | at technobrat dot com   latex glove is to a veterinarian.  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Favorite email parser?

2004-06-29 Thread Brian Gerard

Forgot to mention the platform.  This will be on a FreeBSD system, and
the emails will be in the standard flat file format (mbox).  Didn't want
anyone going too deep on the M$/Exchange side of the house.  :)


  /~~\
 | Brian GerardRed meat isn't bad for you. Fuzzy  |
 | First initial + 'lists'  blue-green meat is bad for you.   |
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: matching hypenated strings

2003-11-25 Thread Brian Gerard
And the clouds parted, and deb said...
 
 ## begin ##
 
 while ($name = DATA) {
   $name =~ /(\w*)\.*/;
   $name{$1}++;
   $name =~ /(\w+)/;
   print $ \n;
   }
 
 
 __DATA__
 tibor.test.net
 mars.test.net
 moon-bx-r.test.net
 moon-bs-d.test.net
 moon-bt-321.test.net
 
 ## end ##
 
 This works for hostnames without hyphens, but when there is a hyphen in the
 name, everything after the hyphen is ignored.  I've been trying things like
 $name =~ /[a-z]*\-*\-*/ with no luck.  The data coming into the expression 
 may or may not be fully qualified, so I can't just take everything to the left
 of .test.net, and the domain name may be different at times, anyway.
 
 So what I'm left with finding an expression that will match any alphanumeric,
 with 0 or more embedded dashes.  It sounds simple, but I can't seem to find
 it.  
 
 What am I missing? 

Two things:

1) The regex you're looking for is likely /[-\w]+/, which says match one
or more dashes or word characters.  This will slurp up everything up to
the first non-word, non-dash character.

2) You can probably simplify your script to

## begin ##

while (DATA) {
(print $ \n and $name{$}++) if /[-\w+]+/;
}

__DATA__
tibor.test.net
mars.test.net
moon-bx-r.test.net
moon-bs-d.test.net
moon-bt-321.test.net

## end ##

Its output is :
ksh$ ./mchname
tibor 
mars 
moon-bx-r 
moon-bs-d 
moon-bt-321 
ksh$

HTH-
Brian



  /~~\
 | Brian Gerard A child of five could understand  |
 | First initial + 'lists'  this! Fetch me a child of five.   |
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: matching hypenated strings

2003-11-25 Thread Brian Gerard
And the clouds parted, and Rob Dixon said...
 
 That's a misuse of 'and' Brian. It says that the hash element
 should be incremented only if the print succeeds, which isn't
 what was intended. If you mean a code block then use a code
 block.

Mea Culpa.  You're absolutely correct.  Thanks for the correction.  :)


  /~~\
 | Brian GerardCrime does not pay... as well as politics. |
 | First initial + 'lists'|
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: regexp matching- real simple!

2003-11-13 Thread Brian Gerard
And the clouds parted, and Nandita Mullapudi said...

 This has to be really easy- but I cant get it to work:
 I want my script to carry out a loop only if the number stored in 
 $diff is positive,
 so i say
 if ($diff =~m/^[-\d+]/) {blah blah}
 but it won't work. why?
 TIA
 -Nandita

Hi Nandita,

The problem is that you're trying to match a character class instead of the
pattern you think you're matching.

Your pattern: ^[-\d+]
Which means : At the beginning of the line, match a single dash, digit, or
plus sign.

In other words, it will match '-23 fnords', '42', '--Socrates', '++$foo', and
a slew of other things you never indended.  :)  A character class matches a
single character, no more.

What you probably want is the ($diff == abs($diff)) solution already proposed,
since you've stated that $diff should be a number.

That being said, the pattern you were trying for would probably be: \A-\d+\Z
Which says: At the beginning of the string, match a single dash, followed by
one or more digits, followed by the end of the string.

HTH-
Brian

  /~~\
 | Brian Gerard  I don't suffer from insanity.|
 | First initial + 'lists'I enjoy every minute of it. |
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: regexp matching- real simple!

2003-11-13 Thread Brian Gerard
And the clouds parted, and Jeff 'japhy' Pinyan said...
 
 Well, your regex is matching either a digit, a -, or a + as the first
 character in $diff.  If you wanted to match a POSITIVE number, you'd be
 better with /^+?\d/, but WHY are you using a regex for a MATH problem?
 
   if ($diff  0) { ... }
 

Bah.  That's the trouble with starting a reply, getting otherwise occupied
before completing it, and then sending it in late.

What he said.  :)


  /~~\
 | Brian Gerard  We now return to our regularly   |
 | First initial + 'lists' scheduled flame-throwing.  |
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Why is parsing your own form data a bad idea?

2003-11-05 Thread Brian Gerard
And the clouds parted, and Dan Anderson said...
 
 This seems kind of silly.  Can anyone explain to me why this is?
 

Beats me.  I've been rolling my own cgi-handlers since perl4 with no
discernable ill effects.  :)  Let me know if you want some sample code.

Brian

  /~~\
 | Brian GerardI'm sorry, are the voices  |
 | First initial + 'lists' in my head bothering you?  |
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Training in the Middle and Far East

2003-11-04 Thread Brian Gerard
And the clouds parted, and Rajeev Prasad said...
 
 computer science is no science at all. It is only layers of information. It 
 is physics and mathematics which makes this number crunching plastic do its 
 job.  that is science. rest of the stuff as Jerry said it is a BIGENNER's 
 list. so it will be like the way it is.
 

Hi Rajeev-

I'm afraid I must disagree.  Science can be defined (in this sense) as
knowledge or a system of knowledge covering general truths or the
operation of general laws especially as obtained and tested through
scientific method[1], and lest I be accused of circular reasoning, the
scientific method referred to above would be principles and procedures for
the systematic pursuit of knowledge involving the recognition and formulation
of a problem, the collection of data through observation and experiment, and
the formulation and testing of hypotheses.[2]

It is true that mathematics and physics (as well as chemistry) are integral
parts (excuse the pun) of computer science, just as mathematics can be said
to be an integral part of quantum mechanics.  They are prerequisite bases of
knowledge without which the composite science would be well nigh impossible
to discuss, much less work in.  This does not mean that the composite
science is not a science in its own right.  Computer scientists propose
theorems and design experiments to test said theorems.  Their theories
predict behaviors resulting from given conditions and can be demonstrated to
be valid or not.

Big O notation, analysis of algorithms, cryptology, information theory,
pattern recognition, graph theory, computational biology; all of these are
examples of tools, branches, and applications of a real, definable, rigorous
science.  Try reading through one or two volumes of Knuth's[3] The Art of
Computer Programming and try to tell me that computer science isn't a real
science.  ;)

Sorry, I know far too many brilliant CS researchers to let this comment go
by unanswered.  :)

Respectfully,
Brian


[1] http://www.m-w.com/cgi-bin/dictionary?book=Dictionaryva=science
Definition 3a.

[2] http://www.m-w.com/cgi-bin/dictionary?book=Dictionaryva=scientific+method

[3] http://www-cs-staff.stanford.edu/~knuth/index.html


  /~~\
 | Brian Gerard Any sufficiently advanced technology is   |
 | First initial + 'lists'   indistinguishable from a perl script.|
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Training in the Middle and Far East

2003-11-04 Thread Brian Gerard
And the clouds parted, and Rajeev Prasad said...

SNIP!

 And the clouds parted, and Rajeev Prasad said...
 
  computer science is no science at all. It is only layers of information. 
  It is physics and mathematics which makes this number crunching plastic
  do its job.  that is science. rest of the stuff as Jerry said it is a 
  BIGENNER's list. so it will be like the way it is.
 
 
 I'm afraid I must disagree.  Science can be defined (in this sense) as
 knowledge or a system of knowledge covering general truths or the
 operation of general laws especially as obtained and tested through
 scientific method[1], and lest I be accused of circular reasoning, the
 scientific method referred to above would be principles and procedures for
 the systematic pursuit of knowledge involving the recognition and 
 formulation of a problem, the collection of data through observation and
 experiment, and the formulation and testing of hypotheses.[2]
 
SNIP!

 [1] http://www.m-w.com/cgi-bin/dictionary?book=Dictionaryva=science
 Definition 3a.
 
 [2] 
 http://www.m-w.com/cgi-bin/dictionary?book=Dictionaryva=scientific+method

Hi again Rajeev-

I think perhaps we have a basic disagreement on the definition of science.
I have quoted what I accept as a rigorous definition (with cites from a well
known authority on the English language, Merriam-Webster).  Regardless of
whether you have corresponding citations, I would be interested to know what
you consider science to be.  Clearly, you rate mathematics, physics, and
chemistry as sciences (I would agree).  Examples aside, what is a good
definition of science as far as you are concerned?

Brian


  /~~\
 | Brian Gerard   The moon is covered with the|
 | First initial + 'lists'   results of astronomical odds.|
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: What is my name

2003-10-31 Thread Brian Gerard
And the clouds parted, and Kevin Pfeiffer said...
  
  Here's a line that will give you exactly that.
  ($PROGNAME = $0) =~ s|(\.?\.?(/[^/]+)*/)?([^/]+)|$3|;
 
 I have two questions...
 
 I came up with this earlier today:
 my ($program) = $0 =~ /([^\/]+)$/;
 
 Doesn't it do the same? (Question 1)

Sure looks that way, yes.  :)  I realized after you posted your (much,
_much_) simpler regex that the reason mine was so convoluted was that
the script I was using it in originally needed to capture the path the
script was invoked with as well as the script name.  For just the script
name your solution should perform admirably.

 If I don't escape the slash in the char class -- i.e. /([^\/]+)$/ -- I get
 this error:
 Unmatched [ in regex; marked by -- HERE in m/([ -- HERE ^/ at ./test-0
 line 7.
 
 This makes no sense to me (since this is Perl and not sed or something)...
 (implied Question 2)

The reason is that you're using '/' as the delimiter for the m// operator,
and '/' isn't automatically escaped in a character class (it's not a
metacharacter).  I got away with it because I was using '|' as the delimiter
in my substitution.

What I mean is this.  Talking about the m// and s/// operators, you can
use just about anything in place of the '/'s. (We won't discuss using the
'x' option to m// and s///, for the sake of brevity and simplicity.)

m/pattern/  #is the same as
m;pattern;  #which is the same as
m|pattern|

and
s/pattern/replacement/  #is equivalent to
s#pattern#replacement#
...or even...
s{pattern}{replacement}
...but I digress.  :)

So to avoid what the Camel Book refers to as 'leaning toothpick syndrome'
when matching unix pathnames, I use a delimiter other than '/' so that I
don't have to always type '\/' when I want to match a slash in the path.

As for why it wouldn't lose its specialness inside of a character class,
that only applies to regex metacharacters ('.', '*', '+', etc).  When perl
looks for the pattern to match it looks for everything up to the first
matching delimiter before trying to compile it.  So when you said
m/([^/]+)$/
perl tried to use
([^
as its regex, which isn't valid.  That's why it was complaining about an
unmatched [.  It only saw one.  :)

HTH-
Brian


  /~~\
 | Brian Gerard  I am Pooh of Borg. Bother is|
 | First initial + 'lists'futile; hunny will be assimilated. |
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: initialising a list of variables

2003-10-30 Thread Brian Gerard
And the clouds parted, and Rob Dixon said...

 Gary Stainburn wrote:
 
SNIP!
 
 Hi Gary.
 
 Just one more offering:
 
$_ = '' foreach my ($fred, $ginger);
 
 HTH,
 
 Rob

...and in the spirit of TMTOWTDI...

map {$_=} my ($fred, $ginger);

:)

  /~~\
 | Brian Gerard   Do not taunt Happy Fun Ball.|
 | First initial + 'lists'|
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: What is my name

2003-10-30 Thread Brian Gerard
And the clouds parted, and [EMAIL PROTECTED] said...
 Hi,
 
 I have a little problem. I have script test.pl and inside this script I want to 
 know what is my name (I mean this test.pl.
 
 test.pl script
 ---
 
 my $this_scripts_name_is = who_am_I();
 
 sub who_am_I
 {
   my $my_name = ; # what I have to put to this  to get test.pl scripts 
 name
 
   return $my_name;
 }
 

I ran into exactly this issue, but I wanted to just get the actual script
name.  $0 returns the entire path the script was invoked as.  ie - $0 could
be ../../foo/bar/blarch/scriptname.pl, but I just wanted scriptname.pl.

Here's a line that will give you exactly that.
($PROGNAME = $0) =~ s|(\.?\.?(/[^/]+)*/)?([^/]+)|$3|;

...or, to put it in the above terms:

my $this_scripts_name_is = who_am_I();

sub who_am_I
{
my $PROGNAME;
($PROGNAME = $0) =~ s|(\.?\.?(/[^/]+)*/)?([^/]+)|$3|;

return $PROGNAME;
}

HTH-
Brian

  /~~\
 | Brian Gerard  Overflow on /dev/null;   |
 | First initial + 'lists'please empty the bit bucket.|
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: script to test a file.

2003-10-29 Thread Brian Gerard
And the clouds parted, and Wiggins d Anconia said...
 
 
  Rick Bragg wrote:
   Hi,
   
   I want to write a script that will test the contents of a file.
   
   The file being tested will pass only if it contains nothing more
 than an 
   ip address on one line.  Does anyone have a sample of a simple regex to 
   accomplish this?
  
  /\d+\.\d+\.\d+\.\d+\n?/s
  
 
 Beware of insufficient regexes in regex clothing.  The above will
 certainly find one or more digits followed by a dot, followed by one or
 more digits followed by a dot, etc.  But it does *NOT* match an IP
 address...
 
 /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
 
 Is at least closer, but it is still not sufficient, as IP addresses are
 bounded, I believe, at 255 so 336.47.894.0, will match the above, but is
 not a valid IP address.
 
 How strict do you need to be, how sure are you about the data in the files?
 

Good point.

At it's most basic, to match any valid IPv4 address alone on a line, I would
use
/\A((1?\d{1,2}|2([0-4]\d|5[0-5]))\.){3}(1?\d{1,2}|2([0-4]\d|5[0-5]))\z/

Which will match a string containing only a dotted quad from 0.0.0.0 to
255.255.255.255, inclusive.  Note that I'm assuming the string in
question contains the _entire_ contents of the file.

A couple of things to note, though:
* A valid IP is actually any 32 bit integer from 0 to 4294967295 (try
  putting 3639555427 in Mozilla (or supposedly any other well-behaved
  browser... in other words, _not_ IE)... it will take you to Google
  because that number is actually 216.239.53.99 - the address for
  www.google.com - in base-10).
* This pattern doesn't exclude RFC1918 reserved addresses (10/8,
  172.16/12, and 192.168/16), or multicasts.
* It doesn't take netmasks into account.  1.2.3.255/16 is a valid
  host address, while 1.2.3.255/24 is not (it's a broadcast).

Taking these sorts of things into account could make for a bit hairier
solution.  ;)  Caveat user.



  /~~\
 | Brian GerardI'm writing an unauthorized autobiography. |
 | First initial + 'lists'|
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]