Setting SIG{CHLD} to IGNORE in fork() changes result of system() call

2010-05-17 Thread Kelly Jones
These 3 lines of code: if (fork()) {sleep 10; exit(0);} $SIG{'CHLD'} = 'IGNORE'; exit(system("/usr/lib/nagios/plugins/check_ping -H google.com -w 500,20% -c 1000,40% 1> /tmp/stdout.txt 2> /tmp/stderr.txt; echo $? > /tmp/res.txt")); return "PING WARNING - Packet loss = 0%, RTA = 62.08 ms|rta=62.07

system() call in END() subroutine overrides script exit value

2010-05-17 Thread Kelly Jones
I did this in tcsh: > perl -le 'exit(2); sub END {system("date");}' ; echo $status Mon May 17 11:09:43 MDT 2010 0 In other words, the return value of the date command in an END subroutine overrides my desired exit value. How do I fix this? I want to tell Perl: if I explicitly do exit($foo), I wa

str2time returns bad result for timezone MST7MDT during daylight time

2010-04-01 Thread Kelly Jones
This code: perl -le 'use Date::Parse; for $tz ("MST", "MDT", "MST7MDT") {print str2time("04 Jul 2009 14:42:00 $tz");}' returns: 1246743720 1246740120 1246743720 In other words, str2time returns the same Unix time for MST and MST7MDT on July 4th. That's incorrect: on July 4th, MST7MDT should eq

Modeling FIFO financial transactions in Perl

2010-03-10 Thread Kelly Jones
How do I easily model first-in-first-out (FIFO) financial transactions in Perl? Example: % I buy 100 shares of XYZ for $8/share on Day 1, another 100 shares for $9/share on Day 2, and another 100 shares for $10/share on Day 3. % On Day 4, I sell 150 shares for $11/share. I calculate my profit

Setting dollarslash to 0777 doesn't work?

2009-07-15 Thread Kelly Jones
According to "man perlrun", the "-0777" option sets $/ to 0777 and slurps files whole. This works fine. However, when I did 'local $/="0777"' in a script, it usually worked, but sometimes failed and only slurped part of the file. Changing this to "local $/ = undef" worked fine. Why? -- We're j

Perl floating point addition oddness

2009-06-03 Thread Kelly Jones
perl -le 'printf("%f %f %f\n", 4294967295, 2147483647*2**32, 2147483647*2**32+4294967295)' 4294967295.00 9223372032559808512.00 9223372036854775808.00 Why? The answer is really 9223372036854775807 (one number lower), and it's obvious that adding 2 and 5 in the units column should yiel

Variable in for loop is not automatically local?

2009-05-02 Thread Kelly Jones
If I do a for loop in a subroutine, do I have to declare it private with my()? I always though for() did that for me, but I ran into a bug where code like this: sub foo {for $i (1..2) {print $i;}} affected my global $i (I think). Or was I imagining it? -- We're just a Bunch Of Regular Guys, a c

Functional completion

2009-04-21 Thread Kelly Jones
I want to do "function completion". If I have functions called where() and which(), I want whe() to call where(), whi() to call which(), and wh() to return something like "Ambigious: where() and which() both match wh()". What's the best/easiest way to do this? -- We're just a Bunch Of Regular Gu

Aliasing functions

2009-04-21 Thread Kelly Jones
I want foo() and bar() to do the same thing. One way to do this: sub foo {return bar(@_);} Is there a more clever way using \&bar and things like that? -- We're just a Bunch Of Regular Guys, a collective group that's trying to understand and assimilate technology. We feel that resistance to new

Text::Unidecode behaves differently on two machines

2009-04-11 Thread Kelly Jones
I "cpan Text::Unidecode" on 2 machines and then ran this code: use utf8; use Text::Unidecode; print unidecode("\x{5317}\x{4EB0}")."\n"; print unidecode("\xd0\x90\xd0\xbb")."\n"; print unidecode("\xe3\x82\xa2")."\n"; On both machines, the first line correctly prints "Bei Jing", the author's test c

UTF-8 to ASCII

2009-04-11 Thread Kelly Jones
I'm trying to convert UTF-8 to ASCII in Perl. Is there an easy way to do this? I tried Unicode::UTF8simple, but ended up w/ many ctrl-a's, which can't be right. I'm going for an extremely complete transliteration, so I want ETH (for example) to be converted to both "d" and "dh". In other words, m

Re: Replace string with list of strings via character changes

2009-04-11 Thread Kelly Jones
-Original Message- >> From: Kelly Jones [mailto:kelly.terry.jo...@gmail.com] >> Sent: Friday, April 10, 2009 13:33 >> To: beginners@perl.org >> Subject: Replace string with list of strings via character changes >> >> I want to replace all the o's in a

Replace string with list of strings via character changes

2009-04-10 Thread Kelly Jones
I want to replace all the o's in a string with x's or y's and all the a's in a string with u's or v's. Example: given string "foobar", the output would be this list of strings fxxbur (change both o's to x, and the a to u) fxxbvr (both o's to x, a to y) fxybur (first o to x, second to y, and the a

Re: Calling subroutine every second using alarm fails

2009-04-01 Thread Kelly Jones
>> Original Message >> Subject: Calling subroutine every second using alarm fails >> From: Kelly Jones >> Date: Wed, April 01, 2009 5:48 am >> To: beginners@perl.org >> >> >> I want a script that constantly accepts user input, b

Calling subroutine every second using alarm fails

2009-04-01 Thread Kelly Jones
I want a script that constantly accepts user input, but runs a subroutine every second to do other work. My attempt: $|=1; $SIG{'ALRM'}= "\&alarm_sub"; &alarm_sub; while (<>) {print "You typed: $_\n";} sub alarm_sub {print "ALARM!\n"; alarm 1; return;} fails miserably. What's the right way to do

Use of my on left side breaks STDIN on right side?

2009-03-29 Thread Kelly Jones
> perl -le '$x=; print $x' hello <- I TYPED THIS IN AND HIT RETURN hello > perl -le 'my($x)=; print $x' hello <- I TYPED THIS IN AND HIT RETURN [no answer, hangs forever] Why? -- We're just a Bunch Of Regular Guys, a collective group that's trying to understand and assimilate technology. We fee

Small paying project: Perl DNS server

2008-11-22 Thread Kelly Jones
I'm trying to write a Perl DNS server that answers queries (A, NS, TXT, CNAME, LOC, MX, etc-- pretty much anything in http://en.wikipedia.org/wiki/List_of_DNS_record_types) based on the time of day, current weather in Paris, number of users on my server, the IP address of the requestor, a hash of t

Quoting hash keys changes things sometimes

2008-11-15 Thread Kelly Jones
Consider: perl -le '$hash{"foo-bar"} = 1; print $hash{foo-bar}' [no result] perl -le '$hash{"foobar"} = 1; print $hash{foobar}' 1 I sort of understand this: in the first script, Perl treats foo-bar as a subtraction, and sets $hash{0} to 1. In the second one it assumes you just left off some quot

Perl equivalent of PHP globals?

2008-10-24 Thread Kelly Jones
How can I see all the local, global, etc Perl variables defined at a given point in my program? -- We're just a Bunch Of Regular Guys, a collective group that's trying to understand and assimilate technology. We feel that resistance to new ideas and technology is unwise and ultimately futile. --

Use of dollar signs before subroutine definition

2008-10-24 Thread Kelly Jones
I saw something like this in a mimedefang tutorial: sub foo () {return 1;} By trial and error, I discovered this means: foo takes EXACTLY four arguments, no less, no more. Where can I learn more about this syntax/feature? -- We're just a Bunch Of Regular Guys, a collective group that's try

Percentile of inserted element

2008-10-11 Thread Kelly Jones
I want an efficient subroutine that inserts $elt into @x and returns the percentile of $elt in @x. For example, if @x is (2,2,3,3,3,4,5,6,7,8) and $elt is 3, the subroutine returns .363636... and @x is now (2,2,3,3,3,3,4,5,6,7,8). Why .363636...?: % After insertion, @x has 11 elements. $elt (3)

Unix command-line tools to edit SharePoint site?

2008-05-24 Thread Kelly Jones
I begrudgingly use a Windows SharePoint server at a customer's request. I'd like to automate (command-line) updating and creating documents, lists, etc. Is there a Unix tool that does this? I know SharePoint has an "API", which basically spoofs the GET/POST calls that your browser would make(?).

Setting arbitrary-depth hash from file

2008-04-13 Thread Kelly Jones
I have a file with this in it: a.b = 10 c.d.e = 11 f.g.h.i.j.k = 12 Based on that, I want to set: $HASH{a}{b} = 10; $HASH{c}{d}{e} = 11; $HASH{f}{g}{h}{i}{j}{k} = 12; This is easy using eval(), but is there a better way? I realize that entries like "a.b = 10" and "a.b.c = 13" would conflict, s

Arbitrary mathematical relations, not just hashes

2008-04-06 Thread Kelly Jones
Many programming languages (including Perl, Ruby, and PHP) support hashes: $color['apple'] = 'red'; $color['ruby'] = 'red'; $type['apple'] = 'fruit'; $type['ruby'] = 'gem'; This quickly lets me find the color or type of a given item. In this sense, color() and type() are like mathematical funct

Filling out HTML form w/ hidden and pre-selected fields

2007-08-10 Thread Kelly Jones
I've scraped an HTML page and gotten back a string that looks like this: [... bunch of stuff I don't care about ...] bar some text here boing [... bunch of stuff I don't care about ...] Is there a Perl module that will take this string and return a hash representing the current (default) for

system() command with a time limit

2006-12-24 Thread Kelly Jones
I want to use system() (or `command`) to run an external command from my Perl script. However, if the external command takes more than 30 seconds (for example) to run, I want to kill it, and move on with the rest of my Perl script. How do I do this? -- We're just a Bunch Of Regular Guys, a collec

How do I tell perl -w: "I really want to use this var just once"

2006-12-18 Thread Kelly Jones
perl -w dings me if I use a variable just once: Name "main::foo" used only once: possible typo... even if I'm magically defining/using $foo somewhere else. Is there any way to tag a variable to tell the -w option that I'm intentionally using that variable just once, and not to warn me about it?

Perl equivalent of PHP extract()?

2006-12-17 Thread Kelly Jones
Inside a subroutine, I want to use this hash: %hash = ("apple" => "red", "pear" => "green", "[EMAIL PROTECTED]" => "yellow"); to set my($apple)="red", my($pear)="green", my($lemon)="yellow" [getting rid of the junk '@' and '!' chars in the key] This is approximately what PHP's extract() does. T

Minimal DNS answer using Net::DNS

2006-11-25 Thread Kelly Jones
I've used xinetd to set up a test nameserver on port 1024. Here's the Net::DNS Perl I'm using to say (falsely) that news.yahoo.com resolves to 10.1.2.3 with a TTL of 1 day: $res = Net::DNS::Packet->new(); $rr = Net::DNS::RR->new("news.yahoo.com. 86400 A 10.1.2.3"); $res->push(answer => $rr); prin