[19406] in Perl-Users-Digest
Perl-Users Digest, Issue: 1601 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 24 00:05:29 2001
Date: Thu, 23 Aug 2001 21:05:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <998625906-v10-i1601@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 23 Aug 2001 Volume: 10 Number: 1601
Today's topics:
- HELP! Accidentally saved Perl script with WORDPAD... <vemba72@youknowwhattodo.hotmail.com>
Re: dynamic menu (efficiency) four12and8up@unspam-me.yahoo.com
Re: email this page script <projectobjects@earthlink.net>
Re: Error in CGI Application: Can't open perl script"d <ash@turnernewmedia.com.au>
Re: i need to count words in perl <ren@tivoli.com>
Re: join <gnarinn@hotmail.com>
Re: local exec via cgi <gnarinn@hotmail.com>
NFA vs DFA <noway@nohow.com>
Re: NFA vs DFA (Mark Jason Dominus)
Re: NFA vs DFA (Tim Hammerquist)
Re: Openning a file <joe+usenet@sunstarsys.com>
Re: Openning a file (Tassilo v. Parseval)
Re: Openning a file (Tad McClellan)
Re: Performance : Shell X Perl (Mark Jason Dominus)
quick(?) programming question for perl newbie (Nathan McDannold)
Re: quick(?) programming question for perl newbie <Jon.Ericson@jpl.nasa.gov>
Re: quick(?) programming question for perl newbie <krahnj@acm.org>
Re: quick(?) programming question for perl newbie <davidhilseenews@yahoo.com>
Re: State of Parrot <dan@tuatha.sidhe.org>
sysread problem on socket <news@althepal#nospam#.com>
Re: VIER/NEUN problem <goldbb2@earthlink.net>
Re: Why is $i so popular? (Randal L. Schwartz)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 23 Aug 2001 23:38:14 -0400
From: "Jon 'The Mensch'" <vemba72@youknowwhattodo.hotmail.com>
Subject: - HELP! Accidentally saved Perl script with WORDPAD...
Message-Id: <N_jh7.1205$ok3.362523@weber.videotron.net>
This is probably in every Perl FAQ on the planet, but since I can't seem to
find one...
I just accidentally modified a .CGI perl script with WORDPAD instead of my
usual NOTEPAD. Naturally, the file no longer works, probably because of the
added carriage returns WP adds (I guess).
My question is... short of going over every line in NOTEPAD manually (forget
it, there are zillions of lines) what are my options to get out of this
predicament?
Thanks!
Jon
------------------------------
Date: Thu, 23 Aug 2001 22:31:54 -0000
From: four12and8up@unspam-me.yahoo.com
Subject: Re: dynamic menu (efficiency)
Message-Id: <tob12qee2m8da@corp.supernews.com>
Logan Shaw <logan@cs.utexas.edu> tapped:
..lots of good stuff..
Wow! Well said, thanks!
The reason I was putting the matches into a numbered hash is (and I should have mentioned it) I'm allowing the use of arrow keys for cursor positioning, so I
could type in five letters and then backspace (without deleting anything) and
change something.
I like how you layed out the stack; It would be easier to use/truncate:
@match = $match[0]..$match[n]; #truncate back to position n on the line.
With the data structure you set up--I'm going to have to study it for awhile
--I could set up something like that whenever the original list changes
(Texas secedes? ;-) and use it. That'll keep me thinking for awhile.
Hey, thanks for the help, it's appreciated.
------------------------------
Date: Fri, 24 Aug 2001 03:29:52 GMT
From: "Dale Henderson" <projectobjects@earthlink.net>
Subject: Re: email this page script
Message-Id: <QSjh7.6599$XY4.664828@newsread2.prod.itd.earthlink.net>
For a static html page, you can use Mail::Sendmail. In fact in the example
in the pod shows how to send html.
For a static page, you would simply open the file and assign to the $text
array.
dale
"Class Spokesman" <galligat@tcd.ie> wrote in message
news:3B850966.7D1FC124@tcd.ie...
> Hay
> I'm looking for a freeware perl script that can email a page
> Does anyone where i can find one thanks
>
>
> Tom
>
>
------------------------------
Date: Fri, 24 Aug 2001 12:03:24 +1000
From: Ash Turner <ash@turnernewmedia.com.au>
Subject: Re: Error in CGI Application: Can't open perl script"d:\myfolder\myfolder\: No such file or directory.
Message-Id: <B7ABF30C.DF5%ash@turnernewmedia.com.au>
iHi Thanks,
This just made my server crash *grin*, but didn't work.
Ash
n article 3B84A7F0.ACF51DA3@sxb.bsf.alcatel.fr, Philippe PERRIN at
philippe.perrin@sxb.bsf.alcatel.fr wrote on 23/8/01 4:51 PM:
> Ash Turner wrote:
>> App Mapping the correct path c:\perl\bin\perl.exe %s %s
>
> I don't know it it can help, but my mapping is :
> ...perl.exe "%1" %*
------------------------------
Date: 23 Aug 2001 16:33:54 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: i need to count words in perl
Message-Id: <m3wv3utq9p.fsf@dhcp9-161.support.tivoli.com>
On 22 Aug 2001, abigail@foad.org wrote:
> Ren Maddox (ren@tivoli.com) wrote on MMCMXIII September MCMXCIII in
> <URL:news:m34rr09bku.fsf@dhcp9-161.support.tivoli.com>:
>:}
>:} print $text =~ /^((?:\S+\s+){0,30})/g;
>
>
> That would fail if text contained thirty words exactly, with no
> whitespace following the last word.
True.
print $text =~ /^((?:\S+\s+){0,29}\S*)/g;
[snip examples of complexity of task]
> Perhaps finding words from a text isn't as trivial as most people
> think it is.
Unless you just use a brain-dead definition of "words", as I have
done. :)
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: Thu, 23 Aug 2001 23:08:12 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: join
Message-Id: <998608092.21797677827999.gnarinn@hotmail.com>
In article <9m3982$7ub@news.or.intel.com>,
Just in <justin.devanandan.allegakoen@intel.com> wrote:
(note: i missed the start of the thread, so some context may be missing)
>Let me elaborate, a full line ends with a ;
>
>The following is therefore split on to two lines :-
>NAME * * * * * 6130 1-9999 10%
> >0.5% N8 CHECKSETUP ;
>
>So it should actually read:-
>NAME * * * * * 6130 1-9999 10%
>>0.5% N8 CHECKSETUP;
you cannot assume that every newsreader will show linesplits the
same way, but i get your meaning.
>
>Printing this out is fine using :-
>$Line = $_;
>s/\n// if((length($Line) < 60) && !(Line =~ /;$/));
please show actual code. use cut and paste.
i assume you mean that you print $line or $_ at this point
>However when I try sticking the joined current line into an array, like:-
>@Array = split(" ", $Line);
what do you mean joined? i do not see any join. just because you
print a variable without a \n does not join it to anything, if that
is what you think.
>
>I don't get anything returned from $Array[9] onwards.
sounds reasonable to me, as there was no join
>Printing the length of $Line after all that suggests that it wasn't even
>joined.
maybe it was not joined after all
>
>I'm not trying to modify $_ so I'm a bit stumped as to why $Array[9] eq ""
>I've tried $NewLine = $PreviousLine . $CurrentLine and variations
>of join " ", $CurrentLine, $NextLine but it results in the same empty
>$Array[9].
show us some actual code for this
gnari
------------------------------
Date: Fri, 24 Aug 2001 01:42:49 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: local exec via cgi
Message-Id: <998617369.611515391152352.gnarinn@hotmail.com>
In article <9m3n1b$sv0$1@slb5.atl.mindspring.net>,
William Alexander Segraves <wsegrave@mindspring.com> wrote:
>"gnari" <gnarinn@hotmail.com> wrote in message
>news:998073498.838356931228191.gnarinn@hotmail.com...
>> In article <129rntscc67ecs5cm8hehf82cpet5618fi@4ax.com>,
>> GunneR <ds@ss.com> wrote:
>> >I know this is very insecure, and im not sure if its even perl
>> >related, but...
>> >
>> >Im trying to write a cgi script that executes local code (via system,
>> >exec, etc) on the system that visits the page.
>>
>***snipped***
>
>TinyWeb (free) or MicroWeb (shareware) can be used to provide a "local"
>server on MS Windows.
or for that matter, a full blown webserver can be installed, with all
bells and whistles. i thought the OP wanted the cgi to run on remote server.
gnari
------------------------------
Date: Thu, 23 Aug 2001 23:44:24 GMT
From: "Buck Turgidson" <noway@nohow.com>
Subject: NFA vs DFA
Message-Id: <szgh7.169188$J37.41529198@typhoon.southeast.rr.com>
I am reading Friedl's "Mastering Regular Expressions", in fact I took
it to the beach with me, which my wife didn't especially appreciate,
but that's another story.
Anyway, he says that to test if your regex engine is NFA, (which I
understand Perl is), one can enter the folloiwng, and it should either
terminate immediately (DFA), or hang (NFA).
However, this command terminates immediately (DOS), and I expected it
to hang. Any insights from anyone?
perl -ne "print if /X(.+)+X/" xx.txt
C:\temp>type xx.txt
=XX============================================
------------------------------
Date: Fri, 24 Aug 2001 02:19:53 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: NFA vs DFA
Message-Id: <3b85b9c9.703c$10e@news.op.net>
In article <szgh7.169188$J37.41529198@typhoon.southeast.rr.com>,
Buck Turgidson <noway@nohow.com> wrote:
>I am reading Friedl's "Mastering Regular Expressions", in fact I took
>it to the beach with me, which my wife didn't especially appreciate,
>but that's another story.
>
>Anyway, he says that to test if your regex engine is NFA, (which I
>understand Perl is), one can enter the folloiwng, and it should either
>terminate immediately (DFA), or hang (NFA).
>
>However, this command terminates immediately (DOS), and I expected it
>to hang. Any insights from anyone?
Friedl has oversimplified. A DFA will terminate quickly, but so will
an NFA with a clever optimization. Perl is using a clever optimization.
Recent versions of Perl (since at least 5.6.0) figure out that there
must be two X's in the target string, and that the second X must be at
least two characters past the first X. The target string has no such
second X, so the matcher reports failure without ever even running the
main part of the regex engine.
Thank you, Ilya!
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: Fri, 24 Aug 2001 03:40:29 GMT
From: tim@vegeta.ath.cx (Tim Hammerquist)
Subject: Re: NFA vs DFA
Message-Id: <slrn9objrb.sl.tim@vegeta.ath.cx>
Me parece que Buck Turgidson <noway@nohow.com> dijo:
> I am reading Friedl's "Mastering Regular Expressions", in fact I took
> it to the beach with me, which my wife didn't especially appreciate,
> but that's another story.
>
> Anyway, he says that to test if your regex engine is NFA, (which I
> understand Perl is), one can enter the folloiwng, and it should either
> terminate immediately (DFA), or hang (NFA).
>
> However, this command terminates immediately (DOS), and I expected it
> to hang. Any insights from anyone?
>
> perl -ne "print if /X(.+)+X/" xx.txt
>
> C:\temp>type xx.txt
> =XX============================================
"Mastering Regular Expressions", p.161
The match should fail. If it does so immediately, the engine is
a DFA[*]. If it takes more than a few seconds it's an NFA...
Footnote: [*] Of course, it could also be an NFA with an
optimization I have not forseen.
I believe the disclaimer in the footnote applies here.
HTH
--
We all know linux is great...it does infinite loops in 5 seconds.
-- Linux Torvalds
------------------------------
Date: 23 Aug 2001 18:34:49 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Openning a file
Message-Id: <m34rqycsmu.fsf@mumonkan.sunstarsys.com>
"cp" <cpryce@pryce.net> writes:
> "Gary E. Ansok" <ansok@alumni.caltech.edu> wrote in message
[...]
> > How would you describe this?
> >
> > my $x = qw/a b c/;
> >
> > According to perlop, qw// generates "a real list".
> >
> > I know, you'll probably tell me it's an implied comma-operator.
> > But that's not how the documentation describes it. ;-)
> >
>
> no. It's an implied split. $x gets the value of three, the number of
> elements in the list.
Only in pre-5.6 perl's; where, as the warning might suggest,
this is considered a bug. qw// now actually compiles a
"true list" of constants:
From perl 5.6's perldelta:
Improved "qw//" operator
The "qw//" operator is now evaluated at compile time into
a true list instead of being replaced with a run time call
to "split()". This removes the confusing misbehaviour of
"qw//" in scalar context, which had inherited that
behaviour from split().
Thus:
$foo = ($bar) = qw(a b c); print "$foo|$bar\n";
now correctly prints "3|a", instead of "2|a".
--
Joe Schaefer "Not everything that counts can be counted, and not everything
that can be counted counts."
--Albert Einstein
------------------------------
Date: Thu, 23 Aug 2001 22:38:40 GMT
From: tassilo.parseval@post.rwth-aachen.de (Tassilo v. Parseval)
Subject: Re: Openning a file
Message-Id: <3b85838c.3097690@news.rwth-aachen.de>
On Thu, 23 Aug 2001 17:04:15 -0500, "cp" <cpryce@pryce.net> wrote:
>
>"Gary E. Ansok" <ansok@alumni.caltech.edu> wrote in message
>news:9m3u13$9d7@gap.cco.caltech.edu...
>> In article <slrn9oaolc.6m2.abigail@alexandra.xs4all.nl>,
>> Abigail <abigail@foad.org> wrote:
>> >Please write down 500 times (in blood):
>> >
>> > THERE ARE NO LISTS IN SCALAR CONTEXT.
>>
>> How would you describe this?
>>
>> my $x = qw/a b c/;
As for what Abigail said about no lists in scalar context, I admit it
is the first time I would not instantly agree with her. I do, however,
understand what I confused in my post which she was referring to. But
in my understanding there can be lists in scalar context.
This list versus array thing fortunately never produced problems
within my programs. Other things (difference between undef, 0, () in
different contextes, when to use 'if defined $var' and when to use 'if
$var') is a much bigger pain for me. I think this is something that
really deserved an article in one of the FAQs.
Tassilo
------------------------------
Date: Thu, 23 Aug 2001 18:23:52 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Openning a file
Message-Id: <slrn9ob0jn.8ec.tadmc@tadmc26.august.net>
SpeedCancer <gmandesigns@hotmail.com> wrote:
>Abigail <abigail@foad.org> wrote in message
>news:slrn9oaolc.6m2.abigail@alexandra.xs4all.nl...
>> Tassilo von Parseval (Tassilo.Parseval@post.rwth-aachen.de) wrote on
>> MMCMXIII September MCMXCIII in
><URL:news:3B842C47.2000408@post.rwth-aachen.de>:
>> @@ SpeedCancer wrote:
>> @@
>> @@ > Is it possible to open and close a file in a sub routine?
>> @@ > Im using this code but it does'nt seem to work
>> @@ >
>> @@ > &openfile (ARGUMENTLIST)
>> @@ >
>> @@ > sub openfile
>> @@ > use strict;
>> @@
Note the syntax error there...
>my $filename = ("file.txt"); #does'nt load the file into the Scalar context.
Nobody said that it did that.
It does however, load the file*name* in scalar context.
A "file" is a concept separate from a "filename".
>it simply tells the FH to open file.txt into the @text array.
No it doesn't.
It stores the 8-characters shown above as the value of the
$filename variable. That is all that it does.
The open() line is what associates a filehandle with a filename.
The <FH> line is what would (if the filehandle had actually been
open()ed successfully) put the file's contents into the @text array.
>And if you
>will try my code without the sub routine it works.
It cannot possibly work, for reasons I pointed out earlier.
Your code reads from the FH filehandle, but you never open()ed
a filehandle with that name.
>Try putting this into
>your interpreter
>You will of course actually have to have file.txt just create one.
>
>#!/usr/bin/perl -w
>use strict;
>my $filename = ("file.txt"); #Tells FH the name of the file to open.
>open FH, "<$filename" or die "Could not open file: $!\n";
>my @text = <FH>; #creates an array out of the opened file
>print @text; #prints the file
>
>Ureka It works! now that we all know it works.
Now you are using the same filehandle in both the open() and the
readline(). You earlier code did NOT work, in fact, it wouldn't
even compile!
>maybe someone can tell me how
>to put it in a sub.
Maybe someone can learn all about subroutines themselves rather
than ask hundreds (thousands?) of other people to read it to them.
perldoc perlsub
>And no offense to anyone
I feel reasonably sure that you are not being sincere there.
>but you should work on reading
>your code.
I read my code just fine. I even read _your_ code just fine.
Please point out where I have misread your code.
I have no idea what you are talking about (because you did not
quote where I did whatever it is you are talking about).
>Especially Tad McClellan
If you want me to see your flame, then you should put it in a
followup to one of my postings (complete with correct References
header), else I am not likely to see it, as your articles hit 2
of my general-purpose score rules.
Flaming is not nearly so satisfying when the target never sees
the article.
>from Texas
You added that to differentiate me from the other Tad McClellans?
I doubt that. I again surmise that here you have dipped down into
my .sig for some ad-hominem ammunition. A rather non-intellectual
approach to discourse. So long.
*plonk*
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 24 Aug 2001 02:27:58 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Performance : Shell X Perl
Message-Id: <3b85bbad.705a$275@news.op.net>
In article <3B84CDD1.9ADE800B@ce.gatech.edu>,
Robert Sherman <rsherman@ce.gatech.edu> wrote:
>in general, the shell is better for smaller tasks, and for portability
>(provided you use sh, as it is the most consistent across the board)
I'd strongly disagree with that. When you write an sh script, you
have to call out to a lot of external utilities like eval, cut, sort,
ls, ps, and so on. These may not be present everywhere, and if they
are their argument formats and output may vary from platform to platform.
In my experience it's a lot easier to write a portable Perl program
than a portable shell script. In 1995 you might have had to worry
that the target system didn't have Perl, but these days Perl is almost
everywhere.
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: 23 Aug 2001 16:58:20 -0700
From: nmcdannold@yahoo.com (Nathan McDannold)
Subject: quick(?) programming question for perl newbie
Message-Id: <58f70619.0108231558.68a10d0f@posting.google.com>
Hello,
I needed to write some quick and dirty code to control a ftp session.
I've never done anything like this, and I chose Perl as a new language
to learn for this project. I hacked my way through, and got a working
script in a few hours(nice language you've got here).
Now, the last thing I'd like to do is to integrate a little subroutine
I have in some other C code I have, and am not sure how I should do
this. The subroutine opens a binary file of a given name, extracts
some bytes, flips the byte order (the file was created on a unix
machine (big-endian), and is being read in on a PC (little-endian)),
and returns an integer read from the file.
Here is the C code:
int getheaderval(char *filename){
char *headerbuf;
headerbuf=(char*)calloc(2200,sizeof(char));
ifstream inFile(filename, ios::in | ios::nocreate | ios::binary);
if (!inFile) printf("File not found");
inFile.read(headerbuf, 2200);
inFile.close();
//Read value from buffer:
int intval;
short shortval;
memcpy((char *)&shortval,&headerbuf[2194],2);
//Switch byte order:
shortval=(unsigned short)ntohs(shortval);
intval = (int)shortval;
free(headerbuf);
return intval;
}
Would this be difficult to do in Perl? Where should I look? Would it
be easier to link to the C code? I'd apprciate any thoughts/help.
Sorry if this is a totally obvious question or if this isn't the right
newsgroup.
-Nathan
------------------------------
Date: 24 Aug 2001 00:28:57 +0000
From: Jon Ericson <Jon.Ericson@jpl.nasa.gov>
Subject: Re: quick(?) programming question for perl newbie
Message-Id: <86zo8qi9me.fsf@jon_ericson.jpl.nasa.gov>
nmcdannold@yahoo.com (Nathan McDannold) writes:
> Now, the last thing I'd like to do is to integrate a little subroutine
> I have in some other C code I have, and am not sure how I should do
> this. The subroutine opens a binary file of a given name, extracts
> some bytes, flips the byte order (the file was created on a unix
> machine (big-endian), and is being read in on a PC (little-endian)),
> and returns an integer read from the file.
<Snip of C code>
> Would this be difficult to do in Perl?
No
> Where should I look?
perlopentut
perldoc -f pack
perldoc -f unpack
Possible http://search.cpan.org
> Would it be easier to link to the C code?
Probably not. If you do decide to use C within Perl, may I suggest
Inline from CPAN?
Jon
------------------------------
Date: Fri, 24 Aug 2001 01:17:22 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: quick(?) programming question for perl newbie
Message-Id: <3B85AB8F.63489817@acm.org>
Nathan McDannold wrote:
>
> Hello,
Hi,
> I needed to write some quick and dirty code to control a ftp session.
> I've never done anything like this, and I chose Perl as a new language
> to learn for this project. I hacked my way through, and got a working
> script in a few hours(nice language you've got here).
>
> Now, the last thing I'd like to do is to integrate a little subroutine
> I have in some other C code I have, and am not sure how I should do
> this. The subroutine opens a binary file of a given name, extracts
> some bytes, flips the byte order (the file was created on a unix
> machine (big-endian), and is being read in on a PC (little-endian)),
> and returns an integer read from the file.
>
> Here is the C code:
C++ code
> int getheaderval(char *filename){
>
> char *headerbuf;
> headerbuf=(char*)calloc(2200,sizeof(char));
>
> ifstream inFile(filename, ios::in | ios::nocreate | ios::binary);
> if (!inFile) printf("File not found");
>
> inFile.read(headerbuf, 2200);
> inFile.close();
>
> //Read value from buffer:
> int intval;
> short shortval;
> memcpy((char *)&shortval,&headerbuf[2194],2);
>
> //Switch byte order:
> shortval=(unsigned short)ntohs(shortval);
>
> intval = (int)shortval;
> free(headerbuf);
> return intval;
> }
>
> Would this be difficult to do in Perl? Where should I look? Would it
> be easier to link to the C code? I'd apprciate any thoughts/help.
> Sorry if this is a totally obvious question or if this isn't the right
> newsgroup.
sub getheaderval {
my $filename = shift || return;
open IN, $filename or die "Cannot open $filename: $!";
binmode IN;
my $shortval;
read( IN, $shortval, 2, 2194 ) == 2 or die "Cannot read from
$filename: $!";
close IN;
return unpack 'n', pack 'S', $shortval;
}
John
--
use Perl;
program
fulfillment
------------------------------
Date: Fri, 24 Aug 2001 01:31:02 GMT
From: "David Hilsee" <davidhilseenews@yahoo.com>
Subject: Re: quick(?) programming question for perl newbie
Message-Id: <q7ih7.10757$hT4.2835262@news1.rdc1.md.home.com>
"John W. Krahn" <krahnj@acm.org> wrote in message
news:3B85AB8F.63489817@acm.org...
> Nathan McDannold wrote:
> >
> > Hello,
>
> Hi,
>
> > I needed to write some quick and dirty code to control a ftp session.
> > I've never done anything like this, and I chose Perl as a new language
> > to learn for this project. I hacked my way through, and got a working
> > script in a few hours(nice language you've got here).
> >
> > Now, the last thing I'd like to do is to integrate a little subroutine
> > I have in some other C code I have, and am not sure how I should do
> > this. The subroutine opens a binary file of a given name, extracts
> > some bytes, flips the byte order (the file was created on a unix
> > machine (big-endian), and is being read in on a PC (little-endian)),
> > and returns an integer read from the file.
> >
> > Here is the C code:
>
> C++ code
>
> > int getheaderval(char *filename){
> >
> > char *headerbuf;
> > headerbuf=(char*)calloc(2200,sizeof(char));
> >
> > ifstream inFile(filename, ios::in | ios::nocreate |
ios::binary);
> > if (!inFile) printf("File not found");
> >
> > inFile.read(headerbuf, 2200);
> > inFile.close();
> >
> > //Read value from buffer:
> > int intval;
> > short shortval;
> > memcpy((char *)&shortval,&headerbuf[2194],2);
> >
> > //Switch byte order:
> > shortval=(unsigned short)ntohs(shortval);
> >
> > intval = (int)shortval;
> > free(headerbuf);
> > return intval;
> > }
> >
> > Would this be difficult to do in Perl? Where should I look? Would it
> > be easier to link to the C code? I'd apprciate any thoughts/help.
> > Sorry if this is a totally obvious question or if this isn't the right
> > newsgroup.
>
>
> sub getheaderval {
> my $filename = shift || return;
> open IN, $filename or die "Cannot open $filename: $!";
> binmode IN;
> my $shortval;
> read( IN, $shortval, 2, 2194 ) == 2 or die "Cannot read from
> $filename: $!";
> close IN;
> return unpack 'n', pack 'S', $shortval;
> }
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
Of course, that is the approach that should have been taken in the original
snippet of C++ code.
David Hilsee
------------------------------
Date: Fri, 24 Aug 2001 02:12:48 GMT
From: Dan Sugalski <dan@tuatha.sidhe.org>
Subject: Re: State of Parrot
Message-Id: <AKih7.277545$v5.27945336@news1.rdc1.ct.home.com>
Hessu <qvyht@removejippii.fi> wrote:
> Polly still alive and nacking?
Yup, doing just fine. Working (albeit early stage) source soon.
Announcements to be made to use.perl.org, I expect.
Dan
------------------------------
Date: Fri, 24 Aug 2001 00:32:03 GMT
From: Alex Hart <news@althepal#nospam#.com>
Subject: sysread problem on socket
Message-Id: <7ghh7.268$YN4.244242@typhoon2.gnilink.net>
I have a POP client that I wrote by hand (well, I use Socket.pm) and its
has worked perfectly until recently. I found one server that it won't
work with and I don't understand what is happening. Hopefully, one of
you enlightened souls will understand the problem.
The basic problem is that the output from the server is getting broken
up. When I do a sysread on the filehandle, it doesn't read the whole
line. I need to do another sysread to pull in the rest of the line. I
wait plenty of time and I read in plenty of bytes. I have tried changing
both with no help. Unless I read the buffer again, I can't get the whole
line. I have tried using the select command in all sorts of ways, with
no effect.
The following is a stripped down version of the code which will connect
to the server, send the username and password(USER and PASS), check the
number of messages (STAT), then quit. It works on most servers, but
fails on at least one. The second sysread (commented near the end)
makes it work on all.
If anyone understands what is going on here, please let me know. Thanks.
#!/usr/bin/perl -w
use strict;
my $server = ""; # server name
my $user = ""; # user name
my $pass = ''; # user password
my @lines;
use Socket;
my $CRLF = "\r\n";
my $protocol = (getprotobyname('tcp'))[2];
socket(POP, AF_INET, SOCK_STREAM, $protocol);
my $server_socket = sockaddr_in (110, inet_aton($server));
connect(POP, $server_socket);
my $old_selected = select(POP);
$| = 1;
select($old_selected);
select(undef, undef, undef, .75);
my $first_response;
sysread(POP, $first_response, 1024);
print "server: $first_response";
my @acts = ("USER $user$CRLF","PASS
$pass$CRLF","STAT$CRLF","QUIT$CRLF");
foreach (@acts) {
print "client: $_";
print POP $_;
my $tmp;
sysread(POP, $_, 1024);
# sysread(POP, $tmp, 1024); # these 2 lines will make it work
# $_ .= $tmp;
print "server: $_\n";
unless (/^\+OK/) {
print "server didn't respond properly.\n";
exit;
}
}
------------------------------
Date: Thu, 23 Aug 2001 23:21:04 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: VIER/NEUN problem
Message-Id: <3B85C820.A522A873@earthlink.net>
Craig Berry wrote:
>
[snip]
> my @squares = map { sprintf '%04d', $_ ** 2 } 1 .. 99;
Wouldn't your code run slightly faster if you replaced 1 .. 99 with
32 .. 99? After all, we do know that the first digit is non-zero, and 32 is the lowest number which when squared produces a 4 digit number.
Plus, we could then get rid of the sprintf.
Also, the loop that does most of the work runs @squares * @squares times, and I think it could be sped up some:
#!/usr/bin/perl -w
use strict;
my @squares = map { $_ * $_ } 32 .. 99;
my @vier = grep /\A(.)((?!\1).)((?!\1|\2).)((?!\1|\2|\3).)\z/, @squares;
my @neun = grep /\A(.)((?!\1).)((?!\1|\2).)\1\z/, @squares;
(my $vierneun = q/\A
(.) # V
((?!\1) .) # I
((?!\1|\2) .) # E
((?!\1|\2|\3) .) # R
((?!\1|\2|\3|\4) .) # N
\3 # E
((?!\1|\2|\3|\4|\5) .) # U
\5 # N
\z/) =~ s/#.*\n|\s+//g;
# A string with /o is the same speed as a precompiled re with /o, and
# it's more portable. See benchmarks down below
# I wish I could do q//x, for this and for [s]printf, [un]pack, etc.
my %vierMap;
my %neunMap;
foreach my $vier (@vier) {
foreach my $neun (@neun) {
if ("$vier$neun" =~ /$vierneun/o) {
push @{$vierMap{$vier}}, $neun;
push @{$neunMap{$neun}}, $vier;
}
}
}
while (my ($vier, $neunsr) = each %vierMap) {
if (@$neunsr == 1 && @{$neunMap{$neunsr->[0]}} == 1) {
print "VIER = $vier\nNEUN = $neunsr->[0]\n";
exit;
}
}
warn "Hey, we should never get here!\n";
__END__
Here's my benchmarking of regexes:
#!/usr/bin/perl
use Benchmark qw(cmpthese);
@str = (qw(foo bar baz qux quux)) x 1000;
$re_s = q [ba];
$re_c = qr[ba];
cmpthese( -10, {
"s" => eval 'sub{scalar grep /$re_s/, @str}',
"so"=> eval 'sub{scalar grep /$re_s/o, @str}',
"i" => eval "sub{scalar grep /$re_s/, \@str}",
"c" => eval 'sub{scalar grep /$re_c/, @str}',
"co"=> eval 'sub{scalar grep /$re_c/o, @str}',
} );
__END__
Here's the results:
Benchmark: running c, co, i, s, so, each for at least 10 CPU seconds...
c: 11 wallclock secs (10.52 usr + 0.02 sys = 10.54 CPU) @ 114.90/s (n=1211)
co: 11 wallclock secs (10.51 usr + 0.03 sys = 10.54 CPU) @ 138.90/s (n=1464)
i: 11 wallclock secs (10.57 usr + 0.01 sys = 10.58 CPU) @ 144.99/s (n=1534)
s: 11 wallclock secs (10.32 usr + 0.20 sys = 10.52 CPU) @ 114.16/s (n=1201)
so: 11 wallclock secs (10.50 usr + 0.02 sys = 10.52 CPU) @ 139.16/s (n=1464)
Rate s c co so i
s 114/s -- -1% -18% -18% -21%
c 115/s 1% -- -17% -17% -21%
co 139/s 22% 21% -- -0% -4%
so 139/s 22% 21% 0% -- -4%
i 145/s 27% 26% 4% 4% --
Putting the expression in directly [using interpolation before the code
is compiled] of course runs fastest.
With the /o flag, a string and a compiled r.e. are equal in speed.
Without the /o flag, a string and a compiled r.e. are *almost* equal.
ISTM that precompiled regexes are only likely to help if we're
looping through a list of res to apply to a string, in something like:
while(<>) LOOP: { for my $re (@res) {print, next LOOP if /$re/} }
--
I'm not a programmer but I play one on TV...
------------------------------
Date: 23 Aug 2001 15:47:19 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Why is $i so popular?
Message-Id: <m1bsl62y2w.fsf@halfdome.holdit.com>
>>>>> "Ross" == Ross Presser <rpresser@NOSPAMimtek.com.invalid> writes:
Ross> It seems to me that early versions of FORTRAN didn't have a way
Ross> to declare types; if you used a name beginning with I through N,
Ross> it was an integer, if not, it wasn't. The default was the only
Ross> choice.
Ross> I may be wrong about this, of course.
My memory mirrors your meme. I think it was FortranIV that I first
learned from a book, although I never hacked it. Fortran77 may have
introduced the type declaration to override the mandatory
"convention".
print "Just another Perl hacker,";
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
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.
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 V10 Issue 1601
***************************************