[18824] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 992 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat May 26 11:05:35 2001

Date: Sat, 26 May 2001 08:05:11 -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: <990889511-v10-i992@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Sat, 26 May 2001     Volume: 10 Number: 992

Today's topics:
    Re: Any d20 Gamers here? I'm building a GM aid... <bmstroh@cavtel.net>
    Re: Capturing STDERR in backticked cmd (in W98) <mnemotronic@mind\no-spam/spring.com>
    Re: CGI and printing vars <goldbb2@earthlink.net>
    Re: cgi.pm problems <goldbb2@earthlink.net>
    Re: Constant -vs- Variable File Handles? nobull@mail.com
    Re: dbi, fork and ipc-shareable - having problems <chenri@snet.net>
    Re: forking and variable references <goldbb2@earthlink.net>
    Re: How to delete dupicate elements in array? (Dave Bailey)
    Re: How to delete dupicate elements in array? <cadmcse@hotmail.com>
    Re: How to delete dupicate elements in array? (Dave Bailey)
    Re: How to delete dupicate elements in array? <juex@my-deja.com>
    Re: How to delete dupicate elements in array? nobull@mail.com
    Re: I *really* don't understand this error message... <goldbb2@earthlink.net>
    Re: if ($x in @a) equivalent in perl? <goldbb2@earthlink.net>
    Re: Invoking Perl script from a web page <goldbb2@earthlink.net>
    Re: Invoking Perl script from a web page (Dave Bailey)
    Re: Invoking Perl script from a web page <joe+usenet@sunstarsys.com>
    Re: Java in perl?? (Gwyn Judd)
    Re: Javascript or Perl ? <goldbb2@earthlink.net>
    Re: keys to variable names, or variable names to keys? <robin@aly.de>
    Re: Long names are doom ? <bowman@montana.com>
        NET::SMTP <Peter.Ruester@t-online.de>
        Perl DBI: help needed understanding errorhandling <charles@betman.demon.nl>
    Re: Perl syntax for multilevel hash nobull@mail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 26 May 2001 09:38:25 -0400
From: Brent Stroh <bmstroh@cavtel.net>
Subject: Re: Any d20 Gamers here? I'm building a GM aid...
Message-Id: <iacvgtc3i4gn7ienqm4u82ag7bevldqdri@4ax.com>

Benjamin Goldberg <goldbb2@earthlink.net> wrote:

>This has nothing to do with your code itself, but if you're looking to
>make a shell, you might want to consider the Term::Readline module.

I thought about it, but from what I've seen, ActiveState Perl on NT seems
to do it for me.  I'm not sure if it's Perl, or the NT command shell it's
running under.  That would be useful for the *nix folks, though.

I haven't exactly been buried in requests or feedback, so I'm focusing on
what I want for now.

Thanks for the suggestion, though.




------------------------------

Date: Sat, 26 May 2001 08:27:51 -0600
From: pt <mnemotronic@mind\no-spam/spring.com>
Subject: Re: Capturing STDERR in backticked cmd (in W98)
Message-Id: <3B0FBD67.5EF04248@mindspring.com>



Chris Stith wrote:

> pt <mnemotronic@mind\no-spam/spring.com> wrote:
> >     I'm trying to redirect STDERR to STDOUT so that I can capture both
> > STDERR and STDOUT output of a backticked command.  I've tried the
> > suggested:
>
> > @Results = `$cmd 2>&1` ;
>
> >     I don't understand how that would work under Windows ... The I/O
> > redirection looks like a unixy kinda thing.
>
> >     My idea is:
>
> > {
> >    local *STDERR = *STDOUT ;
> >    @Results = `$cmd` ;
> > }
>
> >     This works when "$cmd" names an EXE with params (i.e. "get blah.blah
> > *.??v"), but not when "$cmd" is a BATCH file which itself runs
> > executables (specifically, various cross-compiling tools from Tasking
>
> This is because the batch file is being interpreted by your
> command interpreter, which is then running _another_ program.
> Even if you grab the command interpreter's STDERR, you can't
> really coerce it into grabbing the STDERR of the program it is
> running for you.
>

    I thought that each program would "inherit" the file handles passed by
it's progenitor : COMMAND.COM would get the "fixed" STDERR, which it would
pass to the BATCH, which it would pass to the make / compiler / assembler /
linker tools.  It sounds like some program is ignoring the passed handle.


>
> > .... formerly BSO).  When running the BATCH I just get the STDOUT.
> > STDERR still goes to the console.
> >     Environment is W98SE running from a DOS window with ActiveState.
>
> I'm not sure there's a way to do this, short of replacing the
> batch files with full-fledged programs which capture the
> output and pass it on, or duplicating the actions of the
> batch files in your own program in the first place.
>

    That would probably lead to more maintenance than I want to do ... the
BATCH files are the "supported" method.

    I did find a utility called "rse" (redirect standard error) that comes
with PVCS that seems to do the job.  If I do something like:

@Out = `rse SOME_FILE.BAT`;

    I get everything.



>
> Chris
> --
> Ooweeooo I look just like Buddy Holly, and you're Mary Tyler Moore.
> I don't care what they say about us anyway. No I don't care about
> that! -- Weezer, "Buddy Holly"


Remove the obvious anti-spam to reply.


------------------------------

Date: Sat, 26 May 2001 09:32:12 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: CGI and printing vars
Message-Id: <3B0FB05C.CBD3761C@earthlink.net>

Godzilla! wrote:
> 
> Todd Smith wrote:
> 
> > when I use CGI, sometimes I need to print some of the variables back
> > out on the page. Or if I'm sending email to a few people based on
> > input, like a feedback form, I like to make the body of the email
> > all at once and store it in a variable for later. So, I wish I could
> > do this:
> 
> > use CGI;
> > $cgi = new CGI;
> 
> (snipped code)
> 
> As you know by now, there is a parallel article of mine
> to this one, offering one cure for what CGI.pm screws up.

And a fairly ugly cure it is, at that.

> There is another way to avoid these myriad problems
> presented by CGI.pm which only requires some skill;
> write your own program.

Aw, heck, why not rewrite the entire CGI.pm module youself!  One rewrite
for each and every cgi script you need, that's the ticket.

> Below my signature, as usual, you will find a test
> script which performs in seventy or so very easy
> lines, what you are using six-thousand-five-hundred
> plus lines of CGI.pm to do, unsuccessfully.

There's this concept called "code re-use" which you don't seem to grok.

> This test script contains one of my off-the-shelf read
> and parse routines which offers a minimal degree
> of security.

I would prefer to turn taint checks on as my means of getting a 'minimal
degree of security'.

> The remainder, is mostly printing.
> 
> Many advantages are quickly found with this method.
> You have direct access to your read and parse, an
> ability to parse input directly, create single
> key and value pairs or modify to hash style with
> single key and multiple values.

> You may also reduce
> security or bolster security quickly and easily.

Roll your own code is generally more likely to introducing security
holes than plug them.

And what does direct access to the read and parse give you, anyway? 
More chances to screw up, that's what.

> Lots of tricks can be included as needed along
> with many other nice options, all directly and
> quickly with no need to jump through CGI.pm hoops
> only to discover CGI.pm tripped you up somewhere.

More like, lots of tricks *are* needed to be included.

> You expressed a wish to easily concatenate variables
> in an easy fashion and, learned CGI.pm will not allow
> this, without lots of unwarranted coding.

No, he didn't.  He learned from another poster that that CGI.pm does
allow it, and fairly easily.

> Have a look:
> 
> my ($send) = "Name: $in{Name}<BR>Email: $in{Email}";
> 
> Easy concatenation with old fashion double quotes.
> You cannot do this with CGI.pm as you discovered.

*in = $cgi->Vars;
my ($send) = "Name: $in{Name}<BR>Email: $in{Email}";

Simply because you don't know how to do it doesn't mean that it can't be
done.

> This can be done when you write your own program.
> 
> Within my test script, you will note I split up
> some of my print lines and use indentation. Run my
> script and look at your document source. You will
> view a very neat and well organized document source.
> This is not easily done with CGI.pm methodolgy.

That depends on whether you choose to use the stuff for dynamic html
generation or not, and if so, how you do it.  Just because *you* can't
write stuff with CGI.pm neatly, doesn't mean others can't.

> My script also displays a method of controlling
> script event sequencing quickly and easily by
> use of sub-routine calls, near the top of the
> script where they are easy to change.

So tell me, how often is $script_name likely to change?  I would kinda
expect it to simply always be the name of the current script, as in $0
or somesuch, but maybe on your planet it might be different.

> For complex
> scripts, this is life saver, especially if you
> need to split up an html print, as with a chat
> script or other script which prints html, a little
> at time, sprinkled with processing code. You
> cannot do this with CGI.pm; it only allows certain
> event sequences for html and, those events are
> difficult to access and modify.

Would you mind showing me an example of a situation where CGI.pm
disallows some event sequence?

> No module overhead, no hoops through which to
> jump, no unwarranted coding, no annoyance, just
> a quick efficient short script which you may make
> do whatever you like, easily.

As compared to roll-your-own, where the only hoops to jump through are
typing it all in, then fixing up the typoes, then fixing up the security
holes, then rewriting for speed, then fixing more typoes and logic
errors and security holes, etc, etc.

> So here you have an easy seventy lines of code
> performing with success what CGI.pm could not
> do with thousands of lines of code, not without
> your having to ask how to make CGI.pm do what
> you want, right?

Just because you don't know how to do things doesn't mean others don't.
If you use a piece of code often enough, it becomes second nature.  For
you, it's second nature to try to use roll-your-own rather than a
module.  I'm sure that there are others who use CGI.pm often enough that
they can do what they want without thinking about it.

> Consider writing programs, not copying and pasting.

You mean that in writing that piece of code, you *wrote* that cgi
parameter parser, *created* it up from the top of your head, and didn't
copy and paste it, or type it in from memory from somewhere else? 
Pshaw, yeah right.

Anyway, here's my 26 line program.

#! perl -w
use strict;
use CGI qw(:standard :cgi-lib);
use HTML::Entities;
my $cgi = new CGI;
my %in = map encode_entities $_, $cgi->Vars;

my $send = "
	Name:  $in{Name} <BR>
	Email: $in{Email}";

my $script_name = $cgi->url(-relative);
print $cgi->header;
print $cgi->start_html {}, BGCOLOR => "#ffffff";
print (<<HtmlSnippet), $cgi->end_html;
	<FORM ACTION="$script_name" METHOD=post>
	<FONT FACE="arial rounded mt bold" SIZE=3>
	<INPUT TYPE=submit NAME=button VALUE="Test This">
	<P> Name:  <INPUT TYPE=text NAME=Name>
	<P> Email: <INPUT TYPE=text NAME=Email>
	</FONT>
	</FORM>
	<P><P>
	$send
HtmlSnippet
exit 1;

Mine is about 2/3 the size of yours, does the same stuff, and is more
easy to read, IMHO.

-- 
#! perl -- This took a heck of a lot of work to make.  I hope it works.
@p{split//,'justaperlhacker'}=map oct,qw(6b 71 83 1c1 43 2 3 5 79d 71db
7835 1ddf07 23a871 72656b63); print map {$e=1;for(split//){$e*=$_};pack
'V',$e} qw(ptk ppppprre pperrrlau ppppphc ppjsa r);


------------------------------

Date: Sat, 26 May 2001 07:30:43 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: cgi.pm problems
Message-Id: <3B0F93E3.29C0CE1C@earthlink.net>

´Z©Ò¤ô@!¡±o^ wrote:
> 
> i can't add "pragma=>'no-cache' into the header tag,otherwise it will
> put up the header information like normal text,but if i didn't put
> anything into the header attributes , it works just fine at all anyone
> give me a tip i'll be appreciated,thanks in advance
> 
> #!perl
> #script: time4.pl
> 
> use CGI ':standard';
> use POSIX 'strftime';
> 
> #print the HTTP header and the HTML document
> print header({-pragma=>'no-cache'}),#i suppose not to #put anything right
> here
>  start_html('A Virtual Clock'),
>  h1('A Virtual Clock');

Try it as:
print header( -pragma => 'no-cache' );
print start_html('A Virtual Clock');
print h1('A Virtual Clock');

[rest of script snipped]

When posting code that has a problem, you should always try to trim to
the absolute smallest smallest piece of code which demonstrates your
problem.

-- 
#! perl -- This took a heck of a lot of work to make.  I hope it works.
@p{split//,'justaperlhacker'}=map oct,qw(6b 71 83 1c1 43 2 3 5 79d 71db
7835 1ddf07 23a871 72656b63); print map {$e=1;for(split//){$e*=$_};pack
'V',$e} qw(ptk ppppprre pperrrlau ppppphc ppjsa r);


------------------------------

Date: 26 May 2001 15:26:14 +0100
From: nobull@mail.com
Subject: Re: Constant -vs- Variable File Handles?
Message-Id: <u9ae40dw55.fsf@wcl-l.bham.ac.uk>

"Robert" <pettyr_no_spam@hotmail.com> writes:

> <nobull@mail.com> wrote in message news:u9y9rl4gbz.fsf@wcl-l.bham.ac.uk...

> > open (my $FH, '<', '/etc/passwd')
> >    or die "Could not open /etc/passwd: $!";

> > [1] New in 5.6
> >
> 
> Still dragging my feet in 5.005.  Who has time to upgrade? I can't keep up
> with the deprecated
> stuff!

In 5.5:

open (my $FH = IO::Handle->new, '</etc/passwd')
    or die "Could not open /etc/passwd: $!";

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


------------------------------

Date: Sat, 26 May 2001 08:24:10 -0400
From: Richard Chen <chenri@snet.net>
Subject: Re: dbi, fork and ipc-shareable - having problems
Message-Id: <3B0FA06A.54AAF58@snet.net>

"Mr. Sunray" wrote:

> Hi all,
>
> Perl 5.6 on bughat 6.2.
>
> Ok - I thought I'd try and optimize a series of sql SELECT statements by
> forking each one and storing the results in a shared memory object.
>
> Here's the code - I'm reading a series of sql select statements from a
> file, fork'ing a child for each one, executing the 'fetch' within the
> child and trying to store the results in an array for later retrieval.
>
> The problem seems to be with the shared memory objects.  Either the
> array of results grows each time I run the program or else I get "Can't
> use an undefined value as an ARRAY reference at
> /usr/local/lib/perl5/site_perl/5.6.0/IPC/Shareable.pm line 446, <GEN1>
> chunk whatever".
>
> WARNING - be prepared to use 'ipcs' and 'ipcrm' if you want to play with
> this script!  You may have stray memory objects floating around
> afterwards.
>
> Be sure to run this/your program at least twice to make sure your shared
> memory objects aren't "collecting".
>
> #!/usr/local/bin/perl -w
>
> use strict;
> use DBI;
> use DBD::Oracle;
> use IPC::Shareable;
> use IO::File;
>
> use constant DB => 'myDB';
> use constant USER => 'user';
> use constant PASSWD => 'passwd';
> use constant DRIVER => 'Oracle';
>
> # File contains 4 sql SELECT statements.
> # You can use "select sysdate from dual" if you like, so long as it
> works.
> use constant SQLFILE => 'file.sql';
>
> my($dbh, $sth, @results, @allRecords);
> my $if = IO::File->new(SQLFILE) or die "Couldn't open file: $!\n";
>
> # I am not entirely sure what the proper settings for these should be.
> # I've tried various combinations with no (or bad) luck
> my $glue = 'data';
> my %options = (create => 'yes', exclusive => 0, mode => 0644)
> tie @allRecords, 'IPC::Shareable', $glue, { %options } or die "Tie
> failed: $!\n";
>
> $/ = "";  # We're going to read each 'select' statement as an input
> record
> while(my $query = <$if>){
>    chomp($query);
>
>    my $child = fork();
>    die "Can't fork: $!\n" unless defined $child;
>
>    if($child == 0){
>
>         # Not sure if I need 'InactiveDestroy' here or not
>         $dbh = DBI->connect(DB,USER,PASSWD,DRIVER,
>             {RaiseError=>1,PrintError=>1,InactiveDestroy=>1}
>         );
>
>         $sth = $dbh->prepare($query);
>         $sth->execute();
>         while( my $hash_ref = $sth->fetchrow_hashref() ){
>             push(@results, $hash_ref);
>         }
>
>         #+++++++++++++++++++++++++++++++++++++++++++++
>         # '@allRecords' should contain a reference to an array of hash
>         # reference for each select statement (thus, four total).
>         #+++++++++++++++++++++++++++++++++++++++++++++
>         push(@allRecords, \@results);
>
>         $sth->finish();
>         $dbh->disconnect();
>         exit 0;
>     }
> }
>
> close($if) or die "Unable to close input file: $!\n";
>
> #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> # I expect to see four array references here.  When I try this, I either
> get "Can't use
> # undefined value as an array reference" warnings from the
> 'Shareable.pm' module
> #  or the list keeps growing and growing each time I run the program.
> What do I need to do?
> #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> foreach my $ref(@allRecords){
>     print "Ref is: $ref\n";
> }

The problem is that your parent program is not waiting for the children
to finish and hence it will get undefined references if at that time the
child has not finished yet. You can add waitpid call in the parent to deal
with this.
Also, $dbh, $sth should be my variabled defined in child processes.

If you do these, your code will work fine.

HTH

Richard



------------------------------

Date: Sat, 26 May 2001 08:07:22 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: forking and variable references
Message-Id: <3B0F9C7A.5D3F2338@earthlink.net>

Randy Bey wrote:
> 
> On Thu, 10 May 2001 12:02:39 -0500, Randy Bey
> <randy.bey@rivernorthsys.com> wrote:
> 
> >OK so I am truly a dork.
> ><snip!>
> >Am I smoking dope? Is there no way to share memory between these two
> >disparate processes?
> 
> Answer, duh!
> 
> Thanks for all input, and the suggestion to look into IPC::Shareable,
> which didn't work on my system, but turned me onto Storable, and that
> little puppy allowed me to (easily) store off a hash to disk where any
> number of other processes could get at it.

If you are storing a hash to disk, have you considered using a DBM of
some sort instead?

-- 
#! perl -- This took a heck of a lot of work to make.  I hope it works.
@p{split//,'justaperlhacker'}=map oct,qw(6b 71 83 1c1 43 2 3 5 79d 71db
7835 1ddf07 23a871 72656b63); print map {$e=1;for(split//){$e*=$_};pack
'V',$e} qw(ptk ppppprre pperrrlau ppppphc ppjsa r);


------------------------------

Date: 26 May 2001 10:37:01 GMT
From: dave@sydney.daveb.net (Dave Bailey)
Subject: Re: How to delete dupicate elements in array?
Message-Id: <slrn9gunkb.fo.dave@sydney.daveb.net>

On Sat, 26 May 2001 09:37:58 GMT, vector <cadmcse@hotmail.com> wrote:
>suppose I have array @lists ,
>inside many elements are the same,
>How to delete the dupicate and
>keep each element in array is uniq & pirnt out??

Do the unique array elements have to be printed out in the order 
in which they appeared in the original (uncollapsed) array?  If
not, you can collapse the array into a hash and print the keys of
the hash (sorted if you wish):

my %h = map{$_=>0}@list;
@list = sort keys %h;
print "@list\n";

If you need to retain the original ordering, you can still use a
hash to keep track of what you've seen so far:

my %h;
my @a;
map{push(@a,$_) and $h{$_}=1 unless($h{$_})}@list;
print "@a\n";

--
Dave Bailey
davidb54@yahoo.com


------------------------------

Date: Sat, 26 May 2001 11:10:37 GMT
From: "vector" <cadmcse@hotmail.com>
Subject: Re: How to delete dupicate elements in array?
Message-Id: <NaMP6.43043$ko.541505@news1.frmt1.sfba.home.com>

I follow you and wrote the code like this :
#!/usr/bin/perl

 open(IN,"c:/temp/data.txt");
 while(<IN>){push @lists, [split];  }

    my %h = map{$_=>0}@lists;
 @lists = sort keys %h;
 print "@lists\n";

    close(IN);

####
But I get output is something like
ARRAY(0x1ab563c) ..

Can u show me where am I  wrong?


> >keep each element in array is uniq & pirnt out??
>
> Do the unique array elements have to be printed out in the order
> in which they appeared in the original (uncollapsed) array?  If
> not, you can collapse the array into a hash and print the keys of
> the hash (sorted if you wish):
>
> my %h = map{$_=>0}@list;
> @list = sort keys %h;
> print "@list\n";
>
> If you need to retain the original ordering, you can still use a
> hash to keep track of what you've seen so far:
>
> my %h;
> my @a;
> map{push(@a,$_) and $h{$_}=1 unless($h{$_})}@list;
> print "@a\n";
>
> --
> Dave Bailey
> davidb54@yahoo.com




------------------------------

Date: 26 May 2001 11:38:09 GMT
From: dave@sydney.daveb.net (Dave Bailey)
Subject: Re: How to delete dupicate elements in array?
Message-Id: <slrn9gur6v.fo.dave@sydney.daveb.net>

On Sat, 26 May 2001 11:10:37 GMT, vector <cadmcse@hotmail.com> wrote:
>I follow you and wrote the code like this :
>#!/usr/bin/perl
>
> open(IN,"c:/temp/data.txt");
> while(<IN>){push @lists, [split];  }
                           ^     ^
                           |     |
                           Remove these brackets.

The brackets make your @lists array into an array of references
to arrays, each of which contains a single whitespace-split line
from the input file.  

--
Dave Bailey
davidb54@yahoo.com


------------------------------

Date: Sat, 26 May 2001 07:34:34 -0700
From: "Jürgen Exner" <juex@my-deja.com>
Subject: Re: How to delete dupicate elements in array?
Message-Id: <3b0fbef0$1@news.microsoft.com>

"vector" <cadmcse@hotmail.com> wrote in message
news:WPKP6.43038$ko.541239@news1.frmt1.sfba.home.com...
> suppose I have array @lists ,
> inside many elements are the same,
> How to delete the dupicate and
> keep each element in array is uniq & pirnt out??

How about following the answer to "perldoc -q duplicate"?
(Please note the spelling: "perldoc -q dupicate" won't give you any results)

jue




------------------------------

Date: 26 May 2001 15:46:04 +0100
From: nobull@mail.com
Subject: Re: How to delete dupicate elements in array?
Message-Id: <u94ru8dv83.fsf@wcl-l.bham.ac.uk>

"vector" <cadmcse@hotmail.com> writes:

[ snip FAQ: "How can I remove duplicate elements from a list or
array?" ]

I know someone else has already shown you the solution to this
particular problem but he forgot to mention you could have saved
everyone's time by reading the FAQ.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


------------------------------

Date: Sat, 26 May 2001 07:42:20 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: I *really* don't understand this error message...
Message-Id: <3B0F969C.A0A2CECF@earthlink.net>

Koos Pol wrote:
> 
> Can someone please explain why I get this error on the following code?
>     print F foo();          #  gives "Hello world!"

Try
	print { F } foo();

-- 
#! perl -- This took a heck of a lot of work to make.  I hope it works.
@p{split//,'justaperlhacker'}=map oct,qw(6b 71 83 1c1 43 2 3 5 79d 71db
7835 1ddf07 23a871 72656b63); print map {$e=1;for(split//){$e*=$_};pack
'V',$e} qw(ptk ppppprre pperrrlau ppppphc ppjsa r);


------------------------------

Date: Sat, 26 May 2001 10:43:58 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: if ($x in @a) equivalent in perl?
Message-Id: <3B0FC12E.7090BB7A@earthlink.net>

Ala Qumsieh wrote:
[snip]
> At least the original results showed the same ratios between the
> different methods. This basically confirms that foreach() is indeed
> faster than grep() even in the case where both have to traverse the
> entire array. I am not sure where the difference is, but it's probably
> that grep() constructs a copy of the list it is passed before actually
> 'grepping', while the foreach using pointers that simply starts at the
> beginning of the array, and moves down.

That can't be it, since if you change $_ while doing a grep, it does
change the original array (with sometimes unintuitive results).  From
this one can conclude that a copy of the original is *not* made.

-- 
#! perl -- This took a heck of a lot of work to make.  I hope it works.
@p{split//,'justaperlhacker'}=map oct,qw(6b 71 83 1c1 43 2 3 5 79d 71db
7835 1ddf07 23a871 72656b63); print map {$e=1;for(split//){$e*=$_};pack
'V',$e} qw(ptk ppppprre pperrrlau ppppphc ppjsa r);


------------------------------

Date: Sat, 26 May 2001 06:31:11 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Invoking Perl script from a web page
Message-Id: <3B0F85EF.8080262D@earthlink.net>

Uri Guttman wrote:
[snip]
> well, if that is the whole script, you could just do:
> 
>         $/ = undef ;
>         print <IMAGE> ;
> 
> or even faster:
> 
>         read( IMAGE, $buf, -s IMAGE ) ;
>         print $buf ;

I've a question which is OT for the thread...

Suppose I open a file from a mounted system whose reads are very slow,
for example the following with a slow network link:

	open( FILE, "/afs/something.jp/foo/blah.gif" );

(With me being in the US).  If I then do:

	read( FILE, $buf, -s FILE );

Am I garunteed to get all of the file?  What if the filesystem presents
a few bytes of data, then blocks for a few seconds transfering more,
then continues?  Is there any chance that read() will block until *some*
data is there, and return it, even if that amount is less than the
amount I asked for?

If so, I might want to do:

for( $buf = '', $want = -s FILE; $want; $want -= $got ) {
	$got = read( FILE, $buf, $want, length $buf ) or die "$!";
}

This should read chunks as big as I can get, trying again if I didn't
get everything.

But is it *needed* for read()?  What about for sysread()?

-- 
#! perl -- This took a heck of a lot of work to make.  I hope it works.
@p{split//,'justaperlhacker'}=map oct,qw(6b 71 83 1c1 43 2 3 5 79d 71db
7835 1ddf07 23a871 72656b63); print map {$e=1;for(split//){$e*=$_};pack
'V',$e} qw(ptk ppppprre pperrrlau ppppphc ppjsa r);


------------------------------

Date: 26 May 2001 11:31:38 GMT
From: dave@sydney.daveb.net (Dave Bailey)
Subject: Re: Invoking Perl script from a web page
Message-Id: <slrn9guqqn.fo.dave@sydney.daveb.net>

On Sat, 26 May 2001 06:31:11 -0400, Benjamin Goldberg 
<goldbb2@earthlink.net> wrote:
[...]
>I've a question which is OT for the thread...
>
>Suppose I open a file from a mounted system whose reads are very slow,
>for example the following with a slow network link:
>
>	open( FILE, "/afs/something.jp/foo/blah.gif" );
>
>(With me being in the US).  If I then do:
>
>	read( FILE, $buf, -s FILE );
>
>Am I garunteed to get all of the file?  [...] Is there any chance 
>that read() will block until *some* data is there, and return it, 
>even if that amount is less than the amount I asked for?

POSIX allows read(2), which underlies Perl read() and sysread(),
to return a short byte count without setting errno if it is 
interrupted after reading some data.  I think this means you could
end up with just part of the file whether you use read() *or*
sysread(), so in order to be sure what you've got, you have to 
check the return value from whichever function you choose to use.

--
Dave Bailey
davidb54@yahoo.com


------------------------------

Date: 26 May 2001 10:13:43 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Invoking Perl script from a web page
Message-Id: <m3itioqju0.fsf@mumonkan.sunstarsys.com>

Benjamin Goldberg <goldbb2@earthlink.net> writes:

> Suppose I open a file from a mounted system whose reads are very slow,
> for example the following with a slow network link:
> 
> 	open( FILE, "/afs/something.jp/foo/blah.gif" );
> 
> (With me being in the US).  If I then do:
> 
> 	read( FILE, $buf, -s FILE );
> 
> Am I garunteed to get all of the file?  

No- but you are guaranteed that an error occurred if perl's 
read() is short in this situation.

> What if the filesystem presents a few bytes of data, then blocks for a
> few seconds transfering more, then continues?  Is there any chance
> that read() will block until *some* data is there, and return it, even
> if that amount is less than the amount I asked for?

I don't think so, but as perl's read() is usually a single call to
stdio's fread(3), the manpage for fread should provide the answer. Mine 
says fread should continue until either the buffer is full, eof, or 
error.

> 
> If so, I might want to do:
> 
> for( $buf = '', $want = -s FILE; $want; $want -= $got ) {
> 	$got = read( FILE, $buf, $want, length $buf ) or die "$!";

Better:

  my $want = -s FILE;
  my $data;
  $want == read (FILE, $data, $want) or die "partial read error: $!";


> 
> This should read chunks as big as I can get, trying again if I didn't
> get everything.
> 
> But is it *needed* for read()?  What about for sysread()?

No for read(); yes for sysread().

sysread is perl's interface to read(2), which may return a short
read without an error occurring. read(2) returns -1 on error, which
sysread returns as an undef.  Hence the loop might look like

  my ($want, $data) = (-s FILE, "");
  1 while sysread( FILE, $data, $want, length $data );
  die "partial read error: $!" unless $want == length $data;

HTH.

-- 
Joe Schaefer           "Imagination is more important than knowledge."
                                               --Albert Einstein


------------------------------

Date: Sat, 26 May 2001 12:52:32 GMT
From: tjla@guvfybir.qlaqaf.bet (Gwyn Judd)
Subject: Re: Java in perl??
Message-Id: <slrn9gvdlo.bv8.tjla@thislove.dyndns.org>

"Mein Lufkissenfahrzeug ist voller Aale"
said GOGAR (angenent@kabelfoon.nl) in 
<9elm5n$2jcs$1@news.kabelfoon.nl>:
>Anybody knows how to run a java applet in perl instead of a browser?

Just the same way a browser would run it. You would need to embed an
applet container in your script somehow. It might be a lot easier to
just use the system() function to run the applet_viewer program and get
that to run it.

-- 
Gwyn Judd (print `echo 'tjla@guvfybir.qlaqaf.bet' | rot13`)
I am a leader by default, only because nature does not allow a vacuum.
-Bishop Desmond Tutu


------------------------------

Date: Sat, 26 May 2001 07:15:58 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Javascript or Perl ?
Message-Id: <3B0F906E.740F6A0B@earthlink.net>

Chris Stith wrote:
> P.P.P.P.P.P.S. Good day now, and aren't you glad there are no more
>                postscripts? ;-)

Reminds me of an old knock-knock...

Knock-knock.  Who's there?  Bannana.  Bannana who?
Knock-knock.  Who's there?  Bannana.  Bannana who?
Knock-knock.  Who's there?  Bannana.  Bannana who?
Knock-knock.  Who's there?  Orange.  Orange who?
Orange you glad I didn't say bannana?

-- 
#! perl -- This took a heck of a lot of work to make.  I hope it works.
@p{split//,'justaperlhacker'}=map oct,qw(6b 71 83 1c1 43 2 3 5 79d 71db
7835 1ddf07 23a871 72656b63); print map {$e=1;for(split//){$e*=$_};pack
'V',$e} qw(ptk ppppprre pperrrlau ppppphc ppjsa r);


------------------------------

Date: Sat, 26 May 2001 13:59:18 +0200
From: "Robin Aly" <robin@aly.de>
Subject: Re: keys to variable names, or variable names to keys?
Message-Id: <9eo6f8$kau$03$1@news.t-online.com>


Hello,

"Charles K. Clarkson" <c_clarkson@hotmail.com> schrieb im Newsbeitrag
news:B74211237D100F9E.51220C9111C47329.D9FC3F4B1198DB8F@lp.airnews.net...
>
>     I don't play with CGI much, though. Do some browsers ignore
> MAXLENGTH?
I think most common browsers will check on this, but if you depend on the
data being in correct size, and there is someone who invokes your cgi with
i.e. a java-application he/she will/could perfectly ignore these settings.

Bye
Robin





------------------------------

Date: Sat, 26 May 2001 08:16:38 -0600
From: "bowman" <bowman@montana.com>
Subject: Re: Long names are doom ?
Message-Id: <ISOP6.11789$md1.1576@newsfeed.slurp.net>


"00001111" <00001111@aol.com> wrote in message
news:3B0F6809.B9EE02A9@aol.com...
>
>   Anybody use variables/names longer than 31 character
> and finds it really useful ?

very infrequently, and usually when working with gtk in C. This is getting
to be a moot point, but for a big part of computing history, linkers and
related tools imposed an arbitrary length on names. While C might be happy
with my_wonderful_function_1 and my_wonderful_function_2, the linker would
only look at the first 6 or 8 chars.

of course, if you want to write a novel, there is alway COBOL.....




------------------------------

Date: Sat, 26 May 2001 16:15:06 +0200
From: Peter Ruester <Peter.Ruester@t-online.de>
Subject: NET::SMTP
Message-Id: <3B0FBA69.34BC3E72@t-online.de>

Hi,

I would like to send a Email with an attached file.
Is it possible with the module NET::SMTP
A sample is very welcome.

Thanks for all suggestions in advance,
Peter





------------------------------

Date: Sat, 26 May 2001 12:14:43 +0200
From: "Charles Betman" <charles@betman.demon.nl>
Subject: Perl DBI: help needed understanding errorhandling
Message-Id: <990871988.4675.0.pluto.d4ee0b73@news.demon.nl>


Hi,

Learning how to programme the Perl DBi I try to connect to a local mySQL
database (on my PC, with Apache installed) ....

- - - - -

my %attr = (
 PrintError => 0,
 RaiseError => 1
);
my $dbh = DBI->connect($test_dsn, $test_user,$test_password, \%attr)
 or die "Kan geen verbinding maken met de database!";

- - - - -

No problem if I submit the correct parameters: a connection is made
successfully and the script continues ....

When I deliberately use incorrect parameters, trying to understand some
errorhandling, the scripts doesn't return from de DBI->connect-statement. It
doens't make any difference which values I give to PrintError and
RaiseError.

Can somebody explain to me what I do wrong?
Do I have to initialize the DBI in some way?

Thanx in advance for any support or good advise ....
--

Met vriendelijke groet,
Charles Betman
charles@betman.demon.nl
http://www.betman.nl/





------------------------------

Date: 26 May 2001 15:36:53 +0100
From: nobull@mail.com
Subject: Re: Perl syntax for multilevel hash
Message-Id: <u97kz4dvne.fsf@wcl-l.bham.ac.uk>

chadbour@wwa.com (James Weisberg) writes:

> 1.    my($a, $b, $c) = order();
> 2. $hash{$a}{$b}{$c}++;

> What I'm wondering is simply if there is a syntactical way
> to combine lines 1 and 2 into one statement.

No, there is no builtin way.   There are other ways of doing it that
generalise to any number of keys but they are not as simple as what
you are already doing for a fixed number of keys.

BTW: You could simply join the 3 values into a single delimited
string using the implicit join:

$hash{order()}++;

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


------------------------------

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 992
**************************************


home help back first fref pref prev next nref lref last post