[31160] in Perl-Users-Digest
Perl-Users Digest, Issue: 2405 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat May 9 21:09:45 2009
Date: Sat, 9 May 2009 18:09:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 9 May 2009 Volume: 11 Number: 2405
Today's topics:
Block requests from open proxies <noreply@gunnar.cc>
Re: Console output on Win32? <benkasminbullock@gmail.com>
Re: Console output on Win32? <nospam-abuse@ilyaz.org>
Re: Proposal: Image::EXIF::DateTime::Parser <nospam-abuse@ilyaz.org>
Re: use associative array to calculate average & SD <tadmc@seesig.invalid>
Re: use associative array to calculate average & SD <ela@yantai.org>
Re: use associative array to calculate average & SD <tadmc@seesig.invalid>
Re: use associative array to calculate average & SD derykus@gmail.com
Re: writing get_script() <frank@example.invalid>
Re: writing get_script() <jurgenex@hotmail.com>
Re: writing get_script() <jurgenex@hotmail.com>
Re: writing get_script() <jurgenex@hotmail.com>
Re: writing get_script() <jurgenex@hotmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 10 May 2009 01:56:19 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Block requests from open proxies
Message-Id: <76mjhpF1d8k4eU1@mid.individual.net>
[ a similar message was posted to the "Webmaster General" board at
www.webmasterworld.com ]
I wrote a Perl module that can be used to block HTTP requests from open
proxies. The intended purpose is to prevent bogus form submissions.
http://gunnar.cc/cgi-bin/cvsweb.cgi/misc/HTTP/ProxyTest.pm?cvsroot=gunnar
Before the request is further processed, the module grabs REMOTE_ADDR
and does the following:
* Takes a few ports that are commonly used by open proxies and lets Nmap
determine which of those ports are open at REMOTE_ADDR.
* Attempts to use each of the open ports as a proxy, and if any of the
requests would succeed, sends a 403 error and exits.
Once the module has been configured and installed, and assuming that
Perl is the programming language, all I need to do is inserting these
two lines in the beginning of the form processing script:
use HTTP::ProxyTest;
proxytest();
I would appreciate your comments on both the approach as such and the
chosen solution.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sat, 9 May 2009 08:29:08 +0000 (UTC)
From: Ben Bullock <benkasminbullock@gmail.com>
Subject: Re: Console output on Win32?
Message-Id: <gu3esk$ng3$1@news.motzarella.org>
On Fri, 08 May 2009 01:42:35 +0000, Ilya Zakharevich wrote:
> Is it possible to use `binmode($fh, SOMETHING)' in Win32 to get "the
> expected console-like behaviour" on the handle? Something other API?
>
> I mean something like what I find on:
> http://blogs.msdn.com/junfeng/archive/2004/02/25/79621.aspx
>
> (the semantic is to get full unicode text on the console, and text
> converted to the current codepage if the output is redirected to a
> pipe/file; cp65001 [=UTF-8] not excluded).
I'm sorry but I'm not sure I understand exactly your question.
You can find the code page on your system using Win32::Codepage (on
CPAN), and set your binmode to the appropriate encoding.
------------------------------
Date: Sat, 09 May 2009 09:54:34 GMT
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Console output on Win32?
Message-Id: <slrnh0akmq.era.nospam-abuse@chorin.math.berkeley.edu>
On 2009-05-08, Ben Morrow <ben@morrow.me.uk> wrote:
> If you were interested in doing so, writing a :win32console or
>:via(Win32::Console) PerlIO layer wouldn't be hard. You would need some
> XS to call the W versions of the console functions, of course.
Given that I see Windows a few times in a year, I doubt this is a
viable option. Of course, a pre-cooked solution would be much appreciated...
Thanks,
Ilya
------------------------------
Date: Sat, 09 May 2009 09:59:47 GMT
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Proposal: Image::EXIF::DateTime::Parser
Message-Id: <slrnh0al0j.era.nospam-abuse@chorin.math.berkeley.edu>
On 2009-05-08, Marcin Owsiany <marcin@owsiany.pl> wrote:
>| This module provides a parser for "DateTime" strings as defined in
>| Exchangeable image file format for digital still cameras:
>| Exif Version 2.2
>| Section 4.6.4 "TIFF Revision 6.0 Attribute information"
>| Subsection D. "Other Tags", DateTime
Why use a separate module? Should not just ExifTool be fixed to
transparently translate to a uniform format?
(Of course, there must be kept some fields to access the "raw", low-level,
strings, but the "high-level" fields should better not depend on the
flavor of firmware...)
Or did I confuse what this module is about?
Thanks,
Ilya
------------------------------
Date: Sat, 9 May 2009 07:45:56 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: use associative array to calculate average & SD
Message-Id: <slrnh0auo4.mof.tadmc@tadmc30.sbcglobal.net>
[ Please learn the proper way of composing followup messages.
Have you seen the Posting Guidelines that are posted here frequently?
]
ela <ela@yantai.org> wrote:
There should be an attribution line here stating who is being quoted.
(like I have above, indicating that I am quoting something
that "ela" said.)
> Have you looked at the docs, eg. perldoc perldsc,
> perldoc perllol, etc? This should help jumpstart
> some code you can write on your own. Post your
> code here if you run into snags or want alternative
> solutions.
There should be some sort of quoting character here so that readers
can tell the difference between text that is quoted and text that
is new.
(like the ">" above, indicating that I am quoting those 5 lines.
> --
> Charles DeRykus
You should not quote .sigs.
> Thanks for your reference again. But neither of them talks about how to
> create variables dynamically.
You should stop wanting to create variables
dynamically (ie. symbolic references).
You can accomplish what you want without it.
> Let me illustrate:
>
> $var1, $var2, ..., $varn
Instead of having N separate variables, have a single hash
(associative array) with N keys:
$hash{var1} = calc_var1();
$hash{var2} = calc_var2();
...
$hash{varn} = calc_varn();
> $array[var1] #impossible because var1 is a string
Hashes _are_ indexed by strings.
So if you want something that is indexed by strings, you should be
using a hash. Then
$hash{var1}
will work just fine.
> or $array->[var1] #possible but in fact previously it's $var1 but not var1
>
> what i have to get is to somehow "de-reference" (I wish this's the correct
> term)
There are no references in anything I've suggested above, just
plain old hash data structures.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Sat, 9 May 2009 21:05:36 +0800
From: "ela" <ela@yantai.org>
Subject: Re: use associative array to calculate average & SD
Message-Id: <gu3v30$tmk$1@ijustice.itsc.cuhk.edu.hk>
"Tad J McClellan" <tadmc@seesig.invalid> wrote in message
news:slrnh0auo4.mof.tadmc@tadmc30.sbcglobal.net...
>
> Instead of having N separate variables, have a single hash
> (associative array) with N keys:
>
> $hash{var1} = calc_var1();
> $hash{var2} = calc_var2();
> ...
> $hash{varn} = calc_varn();
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
In this sense, I can't create a function to handle the problem generically.
In other words, every time I have to write the codes (or lines) explicitly.
Although I have to declare "n" variables, it's not good to do the "n" things
again & again (e.g. take average, calculate SD and whatever downstream
operations). I wish to use for loop with a function to achieve this.
------------------------------
Date: Sat, 9 May 2009 08:55:07 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: use associative array to calculate average & SD
Message-Id: <slrnh0b2pr.ndu.tadmc@tadmc30.sbcglobal.net>
ela <ela@yantai.org> wrote:
>
> "Tad J McClellan" <tadmc@seesig.invalid> wrote in message
> news:slrnh0auo4.mof.tadmc@tadmc30.sbcglobal.net...
>>
>> Instead of having N separate variables, have a single hash
>> (associative array) with N keys:
>>
>> $hash{var1} = calc_var1();
>> $hash{var2} = calc_var2();
>> ...
>> $hash{varn} = calc_varn();
You have provided an attribution and quoting characters in your followup.
Thank you.
>> --
>> Tad McClellan
>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
But you are still quoting .sigs.
Please stop doing that.
> In this sense, I can't create a function to handle the problem generically.
Probably because we cannot divine what "the problem" is.
If you post a short and complete program *that we can run* that
illustrates the problem, then we can almost for sure provide
a solution...
> In other words, every time I have to write the codes (or lines) explicitly.
> Although I have to declare "n" variables, it's not good to do the "n" things
> again & again (e.g. take average, calculate SD and whatever downstream
> operations). I wish to use for loop with a function to achieve this.
Now I am begining to think that you are looking for a "dispatch table".
Let us fix N at 2 (avg and SD) so we have something concrete to work with.
# UNTESTED
my %dispatch = (
avg => \&compute_average,
sd => \&compute_sd,
);
my @results; # LoH, 1 array element for each data set,
# each hash has 2 keys: "avg" and "sd"
foreach my $dataset ( @data ) {
my %h; # holds all statistics for one data set
foreach my $statistic ( keys %dispatch ) {
$h{$statistic} = $dispatch{$statistic}->($dataset);
}
push @results, \%h;
}
foreach my $result ( @results ) {
display_result($result);
}
If you then need to change N to 3, you'll need to add only one line
to the dispatch table:
median => \&calculate_median,
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Sat, 9 May 2009 10:30:17 -0700 (PDT)
From: derykus@gmail.com
Subject: Re: use associative array to calculate average & SD
Message-Id: <bb58cb6b-1476-494a-ac20-10cbf0f7a814@d38g2000prn.googlegroups.com>
On May 8, 10:54=A0pm, "ela" <e...@yantai.org> wrote:
> ...
>
> Thanks for your reference again. But neither of them talks about how to
> create variables dynamically. Let me illustrate:
>
> $var1, $var2, ..., $varn
>
> $array[var1] #impossible because var1 is a string
>
> or $array->[var1] #possible but in fact previously it's $var1 but not var=
1
>
> what i have to get is to somehow "de-reference" (I wish this's the correc=
t
> term) the variables 1 to n to get their names and then use these names to
> hash the associative arrays.
Here's some background about why it's usually a bad
idea to use that approach:
perldoc -q "variable as a variable name"
--
Charles DeRykus
------------------------------
Date: Sat, 9 May 2009 01:38:26 -0700
From: Franken Sense <frank@example.invalid>
Subject: Re: writing get_script()
Message-Id: <9pf3n5eoruoh.n4qbfr1usklq.dlg@40tude.net>
In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:
[snipped and re-ordered]
>>> True. However there is no reason why not to build a binary tree in Perl.
>>
>>Is there a module for it?
>
> A quick search for 'Binary Tree" on CPAN returns several hundred
> results, the very first on being "Tree::Binary" with many more
> interesting modules on the same and the next page.
my $tree = Tree::Binary->new( 'root' );
my $left = Tree::Binary->new( 'left' );
$tree->left( $left );
my $right = Tree::Binary->new( 'left' );
$tree->right( $right );
my $right_child = $tree->right;
$tree->right( undef ); # Unset the right child.
my @nodes = $tree->traverse( $tree->POST_ORDER );
my $traversal = $tree->traverse( $tree->IN_ORDER );
while ( my $node = $traversal->() ) {
# Do something with $node here
}
Well, this is the cpan for Tree::Binary. I'll need a little time just to
look at it.
I had larger ambitions for my activestate install earlier in the moon. I
saw the gorgeous, full moon rise on an Isotopes game where we slugged the
Zephyrs into a 16-8 loss. We can provide a player to cover Manny and still
pull out a big win on lutheran night.
Anyways, I wanted to download *all* of the modules containing 'Tree'.
Screenshot here: http://lomas-assault.net/usenet/z27.jpg . Fishing for
hints.
>>> And even more: "[...]and then use C to
>>> insert the data into a binary tree."
>>
>>The task is motivated by §11-12 in _C Unleashed_, wherein Heathfield uses a
>>binary tree to remove duplicate lines in a text.
>
> Oh, well, aehmmmm, unless you want to do that as a learning excercise
> there is a _MUCH_ better method in Perl, see 'perldoc -q duplicate':
> "How can I remove duplicate elements from a list or array?"
> in particular the very last sentence.
Would it delete the first or the non-first entry?
Found in C:\Perl\lib\pod\perlfaq4.pod
How can I remove duplicate elements from a list or array?
(contributed by brian d foy)
Use a hash. When you think the words "unique" or "duplicated", think
"hash keys".
If you don't care about the order of the elements, you could just
create
the hash then extract the keys. It's not important how you create that
hash: just that you use "keys" to get the unique elements.
my %hash = map { $_, 1 } @array;
# or a hash slice: @hash{ @array } = ();
# or a foreach: $hash{$_} = 1 foreach ( @array );
my @unique = keys %hash;
You can also go through each element and skip the ones you've seen
before. Use a hash to keep track. The first time the loop sees an
element, that element has no key in %Seen. The "next" statement creates
the key and immediately uses its value, which is "undef", so the loop
continues to the "push" and increments the value for that key. The next
time the loop sees that same element, its key exists in the hash *and*
the value for that key is true (since it's not 0 or undef), so the next
skips that iteration and the loop goes to the next element.
my @unique = ();
my %seen = ();
foreach my $elem ( @array )
{
next if $seen{ $elem }++;
push @unique, $elem;
}
You can write this more briefly using a grep, which does the same
thing.
my %seen = ();
my @unique = grep { ! $seen{ $_ }++ } @array;
I'll need to do a little digesting here, too.
>
>>That turns out to be
>>pretty straightforwardto implement, and I was able to do that following the
>>development in K&R § 6 and the solns in the clc wiki.
>
> In Perl you just put each line into a hash (as keys) and the semantics
> of a hash will automatically eliminate all duplicates.
>
I'm getting closer to getting my head around this. How would you
disambiguate "hash" in perl as opposed to "hash table" in C? (It sounds
like a shady deal on the Raperbahn, nicht?)
--
Frank
[Roger Ailes, Fox News Founder, Chairman and CEO, and former
Nixon-Reagan-Bush strategist, is] a cynical Republican ideologue with no
regard for fairness and balance.
~~ Al Franken,
------------------------------
Date: Sat, 09 May 2009 04:15:11 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: writing get_script()
Message-Id: <q4pa05ttvjhl2h54nm0i7at1dh84s5jv9o@4ax.com>
Franken Sense <frank@example.invalid> wrote:
>I think I can answer your question if you can tell me why this is giving me
>numbers instead of words:
> my @s = split /\s+/, $_;
You are splitting on white space ...
> # print fields
> print $s[0];
... and printing the first 'word', which in your data is e.g.
'44:005:017' ...
> my $outline = join(' ', (1..$#s));
...and then you are merging the natural numbers from 1 to some value n
...
> print "$outline\n";
... and printing them.
Nowhere in this code do you ever print any of the other text. Where do
you think it should be printed?
jue
------------------------------
Date: Sat, 09 May 2009 04:32:03 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: writing get_script()
Message-Id: <cfpa05lglan2jpgmiug5s8atfgukvme04g@4ax.com>
Franken Sense <frank@example.invalid> wrote:
>In Dread Ink, the Grave Hand of Uri Guttman Did Inscribe:
>>>>>>> "FS" == Franken Sense <frank@example.invalid> writes:
>> FS> my $outline = join(' ', (1..$#s));
>>
>> i don't see any word data in that code. what do you think 1 .. $#s will
>> do? do you know what $#s does? (these are for you to answer). how would
>> you think that data has anything to do with the word data you have in
>> @s?
>
>What I want it to do is join the first through the ultimate words in s.
If you don't know what $#s means, then maybe you could ask? If you don't
know what (1..$#s) means, then maybe you could ask? Using code fragments
that you picked up somewhere without knowing their meaning an throwing
them together rarely produces useful code.
$@s is the highest index in the array @s, i.e. a number.
(1..$#s) is the list of numbers from 1 to the highest index of @s.
Nowhere does it relate to the content of @s.
What you want is maybe
@s[1..$#s]
which is a slice of the array @s, containing the elements from index 1
to the highest index. However I would probably use shift() instead to
remove the first element from an array.
Another note:
You are splitting the whole string into individual words only to join
them again later. Because the only value you really want separated out
as an individual value is the 't44:005:0171' it makes much more sense
to only split off that one single value by limiting split() to only 2
fields.
my ($numbers, $text) = split /\s+/, $_, 2;
jue
------------------------------
Date: Sat, 09 May 2009 04:55:44 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: writing get_script()
Message-Id: <6jqa05docvoh7rt9qmku086qruqkgokr75@4ax.com>
Franken Sense <frank@example.invalid> wrote:
>In Dread Ink, the Grave Hand of Jürgen Exner Did Inscribe:
>> Oh, well, aehmmmm, unless you want to do that as a learning excercise
>> there is a _MUCH_ better method in Perl, see 'perldoc -q duplicate':
>> "How can I remove duplicate elements from a list or array?"
>> in particular the very last sentence.
Sorry, please ignore this, my version of the perlfaq is outdated, I
really need to upgrade. This particular answer has been rewritten, such
that now ...
>Found in C:\Perl\lib\pod\perlfaq4.pod
> How can I remove duplicate elements from a list or array?
> (contributed by brian d foy)
>
> Use a hash. When you think the words "unique" or "duplicated", think
> "hash keys".
... the key sentence has been moved to the top.
>Would it delete the first or the non-first entry?
Why would that matter? The items are identical, therefore you cannot
distinguish them anyway.
If you really have a list where the sequence is important, then use one
of the other methods that are listed in that FAQ for preserving the
sequence. If you keep the first of the last element typically depends
upon if you loop through the list from the top or from the bottom.
>> In Perl you just put each line into a hash (as keys) and the semantics
>> of a hash will automatically eliminate all duplicates.
>
>I'm getting closer to getting my head around this. How would you
>disambiguate "hash" in perl as opposed to "hash table" in C? (It sounds
>like a shady deal on the Raperbahn, nicht?)
Not absolutely sure what you mean by 'disambiguate' in this context,
therefore my answer may be off the mark.
A hash in Perl is a buildin standard data structure which maps strings
to scalars and allows access to every elements in O(1) time. Internally
it is implemented using hash tables, but Joe Programmer normally doesn't
have to concern himself with that detail. For him a hash is an array
where the indices are arbitrary strings instead of (natural) numbers.
C as a programming language does not have hash tables. It is the
programmers task to implement the abstract data structure 'hash table'
himself using pointers and arrays and chunks of memory.
jue
------------------------------
Date: Sat, 09 May 2009 05:03:58 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: writing get_script()
Message-Id: <v6sa05lq2r80cd2j2jt70dj903r9kvq1dm@4ax.com>
Jürgen Exner <jurgenex@hotmail.com> wrote:
>$@s is the highest index in the array @s, i.e. a number.
Arrrg, make that
$#s is the highest index in the array @s, i.e. a number.
of course.
jue
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 2405
***************************************