[19230] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1425 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 2 00:06:47 2001

Date: Wed, 1 Aug 2001 21:05:08 -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: <996725108-v10-i1425@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 1 Aug 2001     Volume: 10 Number: 1425

Today's topics:
    Re: <br> and <b> substitution <krahnj@acm.org>
    Re: challenging question <wyzelli@yahoo.com>
        Easy way to save and restore debugger state? <newspost@coppit.org>
        FAQ: How do I test whether two arrays or hashes are equ <faq@denver.pm.org>
    Re: how to find free disk space (Win32) <ahamm@sanderson.net.au>
        how to validate a regular expression.. (Sarah Lin)
    Re: how to validate a regular expression.. <bwalton@rochester.rr.com>
    Re: join lines ? <godzilla@stomp.stomp.tokyo>
        Passing subroutine as an argument to another subroutine <irfan@abstractedge.com>
    Re: Passing subroutine as an argument to another subrou (Eric Bohlman)
        password protact a file <jagman98@home.com>
    Re: password protact a file (Damian James)
        process id on vms (Nick Paszty)
    Re: read files into arrays... <ren@tivoli.com>
    Re: Script works on PWS but not IIS? <skoehler@upb.de>
    Re: Splitting a text list into smaller lists? (Damian James)
    Re: Splitting a text list into smaller lists? <jeff@vpservices.com>
    Re: Splitting a text list into smaller lists? <krahnj@acm.org>
    Re: Strange problem... (Randal L. Schwartz)
    Re: Strange problem... <godzilla@stomp.stomp.tokyo>
    Re: Strange problem... <godzilla@stomp.stomp.tokyo>
    Re: The perlish way to write this? <uri@sysarch.com>
    Re: The perlish way to write this? <jeff@vpservices.com>
    Re: The perlish way to write this? (Eric Bohlman)
    Re: unlocking a directory (David Efflandt)
    Re: Verify upload is completed <gbeymk@sgh.com.sg>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 02 Aug 2001 02:48:40 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: <br> and <b> substitution
Message-Id: <3B68C021.D1BE12B3@acm.org>

Steve Booth wrote:
> 
> I'm not sure if this is a good way of doing it but I'm trying to replace <b>
> and <br> tags with codes.  The codes are stored in %hash1 and the HTML file
> is stored in $toFormat.  I have tried the code below but it only works if
> the tag is the only thing on a line.
> i.e:
> <br>            will be converted
> <br><br>      will not be converted
> 
> $toFormat =~ s! (<(.+)>) ! $hash1{$2} || $1 !gexi;

If you are only converting <b> and <br> then:

$toFormat =~ s/<(br|b)>/$hash1{$1}/gi;
# OR
$toFormat =~ s/<(br?)>/$hash1{$1}/gi;


>   BEGIN {
>         %hash1 = (
>         'br' => '=!=br=?=!==br=?=',
>         'b' => '=!=b=?='
>         );
>     }



John
-- 
use Perl;
program
fulfillment


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

Date: Thu, 2 Aug 2001 09:10:07 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: challenging question
Message-Id: <Ek0a7.10$fw.726@vic.nntp.telstra.net>

"Ilya Martynov" <ilya@martynov.org> wrote in message
news:87ae1jv8hf.fsf@abra.ru...
>
> sub generate_random_nums {
>     my $num_rand = shift;
>
>     my @rands = map rand, (1 .. $num_rand);
>
>     my $sum = 0;
>     for my $rand (@rands) {
>         $sum += $rand;
>     }
>
>     my @arr = map $_/$sum, @rands;
>
>     return @arr;
> }

Nice Algorithm.

Rewritten more compactly and using some Perl defaults (just as an example to
OP and for fun)

sub generate_random_nums {
    my @rands = map rand, (1 .. shift);
    my $sum;
    for (@rands){$sum += $_};
    return map $_/$sum, @rands;
}

:)

Wyzelli
--
@x='074117115116032097110111116104101114032080101114108032104097099107101114
'=~/(...)/g;
print chr for @x;





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

Date: Wed, 01 Aug 2001 19:33:02 -0400
From: David Coppit <newspost@coppit.org>
Subject: Easy way to save and restore debugger state?
Message-Id: <3B6891AE.6080905@coppit.org>

Is there any easy way to save breakpoints between runs of the perl 
debugger? For example, when debugging:

   echo 'test input on stdin' | perl -d testscript.pl

I'd like to remember a breakpoint inside a module that is require'd, 
since I have to step through HTML::Parser and a bunch of other modules 
before I eventually get to the line that I want to stop on.

Thanks,
David



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

Date: Thu, 02 Aug 2001 00:20:12 GMT
From: PerlFAQ Server <faq@denver.pm.org>
Subject: FAQ: How do I test whether two arrays or hashes are equal?
Message-Id: <011a7.60$l_m.170978304@news.frii.net>

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with every Standard Distribution of
Perl.

+
  How do I test whether two arrays or hashes are equal?

    The following code works for single-level arrays. It uses a stringwise
    comparison, and does not distinguish defined versus undefined empty
    strings. Modify if you have other needs.

        $are_equal = compare_arrays(\@frogs, \@toads);

        sub compare_arrays {
            my ($first, $second) = @_;
            no warnings;  # silence spurious -w undef complaints
            return 0 unless @$first == @$second;
            for (my $i = 0; $i < @$first; $i++) {
                return 0 if $first->[$i] ne $second->[$i];
            }
            return 1;
        }

    For multilevel structures, you may wish to use an approach more like
    this one. It uses the CPAN module FreezeThaw:

        use FreezeThaw qw(cmpStr);
        @a = @b = ( "this", "that", [ "more", "stuff" ] );

        printf "a and b contain %s arrays\n",
            cmpStr(\@a, \@b) == 0 
                ? "the same" 
                : "different";

    This approach also works for comparing hashes. Here we'll demonstrate
    two different answers:

        use FreezeThaw qw(cmpStr cmpStrHard);

        %a = %b = ( "this" => "that", "extra" => [ "more", "stuff" ] );
        $a{EXTRA} = \%b;
        $b{EXTRA} = \%a;                    

        printf "a and b contain %s hashes\n",
            cmpStr(\%a, \%b) == 0 ? "the same" : "different";

        printf "a and b contain %s hashes\n",
            cmpStrHard(\%a, \%b) == 0 ? "the same" : "different";

    The first reports that both those the hashes contain the same data,
    while the second reports that they do not. Which you prefer is left as
    an exercise to the reader.

- 

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Answers to questions about LOTS of stuff, mostly not related to
Perl, can be found by pointing your news client to

    news:news.answers

or to the many thousands of other useful Usenet news groups.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.

  AUTHOR AND COPYRIGHT

    Copyright (c) 1997-1999 Tom Christiansen and Nathan
    Torkington.  All rights reserved.

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

                                                           04.42
-- 
    This space intentionally left blank


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

Date: Thu, 2 Aug 2001 12:03:35 +1000
From: "Andrew Hamm" <ahamm@sanderson.net.au>
Subject: Re: how to find free disk space (Win32)
Message-Id: <3b68b559@news.iprimus.com.au>

John Stumbles wrote in message ...
>Is there a module? Or some fairly portable way to do this? On windoze
>even grepping `dir` for 'bytes free' fails if the disk is empty :-(
>
>My hack (below) is to create an empty file, repeat the `dir` then
>delete the file - oh dear!
>
There is surely (no definitely) a win function for this, but I haven't been
into that particular programming world for years. However, access would
either be through the Win32 API modules, and there may be a specific call
available or you can fudge one up. So don't give up. It's out there
somewhere. And when you work it out, please add your code (or pass it on to
the authors of) Filesys::DiskFree as suggested by Paul Johnston.

--
Lamarr: My mind is a raging torrent, flooded with rivulets of thought
    cascading into a waterfall of creative alternatives.
Taggart: Gol-darn it, Mr. Lamarr, you use your tongue purdier than a
    twenty dollar who.re.
Lamarr: Sh!t - kicker...
    -- Blazing Saddles





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

Date: 1 Aug 2001 17:04:21 -0700
From: sarahlin@yahoo.com (Sarah Lin)
Subject: how to validate a regular expression..
Message-Id: <a4ab66b9.0108011604.3814cee0@posting.google.com>

Hi there. how do i validate a regular expression that entered by the user?
perl will die if "(q1" is entered, and that's the thing i try to avoid..
any PM for this? any help?
thanks.

-sarah


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

Date: Thu, 02 Aug 2001 01:49:50 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: how to validate a regular expression..
Message-Id: <3B68AA59.287EBDCA@rochester.rr.com>

Sarah Lin wrote:
> 
> Hi there. how do i validate a regular expression that entered by the user?
> perl will die if "(q1" is entered, and that's the thing i try to avoid..
> any PM for this? any help?
 ...
> -sarah
You could use eval to evaluate it and test for any errors that
resulted.  perldoc -f eval.
-- 
Bob Walton


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

Date: Wed, 01 Aug 2001 18:15:45 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: join lines ?
Message-Id: <3B68A9C1.1E5DE7D1@stomp.stomp.tokyo>

Yves Orton ignorantly and hatefully trolled:
 
> Godzilla! wrote:
> > Tim wrote:
> > > Godzilla! wrote:
> > > > Tim wrote:

> #!perl


Your code produces incorrect results.


Godzilla!


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

Date: Wed, 01 Aug 2001 23:26:47 GMT
From: "Irfan Baig" <irfan@abstractedge.com>
Subject: Passing subroutine as an argument to another subroutine.
Message-Id: <20010801.192219.1037127828.17158@irf.local>

I want to create a subroutine (XX) with two arguments - a 'source', and
another subroutine (YY). As XX scans the source, it selects pieces of
data from it. As each piece of data is selected, it will then be passed
as an argument to YY and processed through YY.

For example, if XX is a sub that reads down a directory tree for
filenames. The 'source' argument is the top folder path, and then the YY sub
is  rename(). This should effectively rename files in the tree according
to some set of rules. *However* the rename sub can be replaced by the
caller with, for example, unlink(), at the caller's will.

Is this possible, and if so, how do I go about writing the code for this?


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

Date: 2 Aug 2001 03:41:56 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: Passing subroutine as an argument to another subroutine.
Message-Id: <9kai64$mm7$2@bob.news.rcn.net>

Irfan Baig <irfan@abstractedge.com> wrote:
> I want to create a subroutine (XX) with two arguments - a 'source', and
> another subroutine (YY). As XX scans the source, it selects pieces of
> data from it. As each piece of data is selected, it will then be passed
> as an argument to YY and processed through YY.

> For example, if XX is a sub that reads down a directory tree for
> filenames. The 'source' argument is the top folder path, and then the YY sub
> is  rename(). This should effectively rename files in the tree according
> to some set of rules. *However* the rename sub can be replaced by the
> caller with, for example, unlink(), at the caller's will.

> Is this possible, and if so, how do I go about writing the code for this?

You pass the second argument as a code reference.  See perlref for
details.  Note that you can't make direct code references to built-in
functions like rename() or unlink(), but you can create a reference to an 
anonymous sub that calls either of them.

The basic logic is like:

sub process_tree {
  my ($source,$code)=@_;
  ...
  $code->($oldfile,$newfile);
}

process_tree('/stuff', sub {rename($_[0], $_[1])});
process_tree('/morestuff', sub {unlink($_[0])});




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

Date: Thu, 02 Aug 2001 00:16:19 GMT
From: "Jag Man" <jagman98@home.com>
Subject: password protact a file
Message-Id: <nZ0a7.45119$oh1.15575802@news2.rdc2.tx.home.com>

How can it be done from perl, any built in function?   similar to "vi -x".

TIA!




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

Date: 2 Aug 2001 04:03:29 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: password protact a file
Message-Id: <slrn9mhk7j.e1.damian@puma.qimr.edu.au>

Jag Man chose Thu, 02 Aug 2001 00:16:19 GMT to say this:
>How can it be done from perl, any built in function?   similar to "vi -x".
>

The direct equivalent to `vi -x` would be crypt(). See `perldoc -f crypt`.

Cheers,
Damian
-- 
@:=grep!($;+=m!$/|#!),split//,<DATA>;@;=0..$#:;while(@;){for($;=@;;--$;;)
{@;[$;,$:]=@;[$:,$;]if($:=rand$;+$|)!=$;}push@|,shift@;if$;[0]==@|;select
$,,$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
Just another Perl Hacker # rev 3.1 -- a JAPH in progress, I guess...


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

Date: 1 Aug 2001 15:34:09 -0700
From: paszty@xoma.com (Nick Paszty)
Subject: process id on vms
Message-Id: <14ce1c21.0108011434.7df0431@posting.google.com>

hello.

we're running vms 7.1 on an alphaserver 800 5/333 and perl 5.005_03
built for VMS_AXP.  i am running the following test code from a telnet
session and also from an xwindows decterm using eXcursions.  here is
the code.

#!lardat4disk:[perl]
# tests the sas call from perl using a com file called perl2sas.com;

my$sascode="dsets.sas";
chomp($sysparm);
print "sas code is: $sascode\n";

my$protocol="hukt257";
chomp($protocol);
print "protocol is: $protocol\n";

my$sysparm="$protocol|$$";
chomp($sysparm);
print "system parameter is: $sysparm\n";

system ("\@lardat3disk:[csws.apache.cgi-bin]perl2sas $sascode $sysparm
$$");

print "end of test perl";

exit 0;

the problem that i see is that the $$ process id variable value does
not change from one run to the next.  on unix, every time i run a
perscript, it has a unique process number.

any thoughts on how to find a unique process number for the script? i
am eventually heading towards running this as a cgi script and with
multiple users, i need to maintain 'state' using the process id.


thanks,

nick


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

Date: 01 Aug 2001 15:49:51 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: read files into arrays...
Message-Id: <m3hevrijyo.fsf@dhcp9-161.support.tivoli.com>

On Wed, 01 Aug 2001, Tassilo.Parseval@post.rwth-aachen.de wrote:

> Nathan Randle wrote:
> 
>>is there a way of reading specific lines from a text file into an
>>array
>>
>>Eg. read lines 10 - 30 of data.dat
>>
> 
> Yes.
> 
>>In perl form obviously. I used that just to illustrate what i mean.
>>
> 
> Use an array-slice for that:
> 
> open FILE, "data.dat" or die "Error: $!";
> my @array = (<FILE>)[9 .. 29];

Egads!  That reads the entire file into memory just to grab 20 lines
of it.  Here is an alternative that does not:

open my $datafh, "<", "data.dat" or die "Could not open data.dat, $!";
while(<$datafh>) {
  next if $. < 10;
  push @array, $_;
  last if $. == 30;
}
close $datafh;

-- 
Ren Maddox
ren@tivoli.com


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

Date: Thu, 2 Aug 2001 01:38:25 +0200
From: "Sven Köhler" <skoehler@upb.de>
Subject: Re: Script works on PWS but not IIS?
Message-Id: <9ka3s4$n0l$01$1@news.t-online.com>

your thoughts are right because pws=iis

but you forgot one thing ! you perhaps dont' have ntfs on your machine and
ntfs makes things complecated ...

the admin of the iis-server must grant the IUSR_xxx write permissions on the
file ...

"Alex" <samara_biz@hotmail.com> schrieb im Newsbeitrag
news:c7d9d63c.0108010534.50dae317@posting.google.com...
> Hi,
>
> I am trying to install Matt Wright's guestbook script on a website on
> a IIS webserver. When I try to run it I get the following error
> message:
>
> ----------
> CGI Error
> The specified CGI application misbehaved by not returning a complete
> set of HTTP headers. The headers it did return are:
>
> Can't Open e:/Inetpub/wwwroot/Edtech/iei/guestbook/guestbook.html:
> Permission denied
> ----------
>
> I only have ftp access to the IIS webserver. At first I thought that
> the problem is in write permission on guestbook.html. I have a PWS
> webserver installed on my local machine. When I install the script
> there, it runs just fine! It doesn't even matter whether
> guestbook.html has write permission enabled or not. I tried really
> hard to model the same problem, but I couldn't.
>
> I'm very confused... Why does the script run on PWS, but wouldn't run
> on IIS?
>
> Any help is very appreciated. I searched for the solution to this
> problem for hours, but can't seem to come up with anything on my own,
> that's why I'm posting this here.
>
> Thanks again,
>
> Alex




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

Date: 2 Aug 2001 01:05:05 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: Splitting a text list into smaller lists?
Message-Id: <slrn9mh9p2.7le.damian@puma.qimr.edu.au>

ctverlane@NOSPAM.blackdahlia.zzn.com chose 1 Aug 2001 20:26:52 GMT to say this:
>
>I apologize in advance for the cluelessness of this question, but I'm
>desperate, so here goes.
>
>I'm an Oracle dba.  I have a long list of users (25000) of them, in a
>delimited text file.  What I want to do is use Perl to take the long list and
>divide it up into 500 lists of 50 users each.  So the task breaks down to 1)
>dividing the list into groups of 50, using the delimiter to show where each
>username ends, and then 2) writing each list to a separate file.
>
>Here is my halting attempt at it:
>
>#!/usr/bin/perl  -w
>

It's always a good idea to include the 'strict' pragma:

use strict;

># Read the file of 25000 users from stdin:
>while ($a = <STDIN>) {
>

This reads ONE LINE of input.

Hopefully the variable name is something other that '$a', as this has a
special meaning in perl (see `perldoc -f sort`). A 'my' is needed to work
with 'strict', and this also limits the scope of the variable to the while
block.

while (my $line = <STDIN> ) {

># Parse the line, using the numeral 4 as the delimiter.
>   @a = split( /4/, $a, 50);

This splits the one line of input on '4', with a max of 50 matches, into
the array @a. If there were fewer than 50 matches on the first line, you
are only assigning that number of matches, not 50. To work with strict, you
would also need the 'my':

	my @names = split( /4/, $line, 50 );

>   
>   foreach $a (@a) {
>        print "$a";
>
> }
>}

OK. If you really were using '$a' in both places, you have now clobbered it.
This loop re-uses the unscoped '$a'. I would expect this to cause problems.
You can avoid using a new variable here altogether by 

	print for @names;

(See `perldoc perlvar` for info about Perl's special variables -- you are
interested in $_ here.)
or (at the very least) use a different variable name:

	foreach my $name (@names) {
		print $name;
	};

Note that your while loop is only reading a line at a time. You are
splitting each line and taking the first 50 or less elements, then printing
those. If you really want to generate lists with exactly 50 members, you
need a different mechanism. It might look like this (warning untested code
follows):

#!/usr/local/bin/perl -w
use strict;

$/ = '4'; ### see perldoc perlvar
my @names;
my $size = 50;

while (my $name = <>) {
    chomp $name;
    $name =~ s/^\s+//g;
    push @names, $name;
    if ( @names == $size or not $name) {
        my $file = 'names' . ($.-@names) . '-' . ($.-1);
        pop @names unless $name;
        open  OUTFILE, ">$file" or die "Can't write $file: $!";
        print OUTFILE join("\n", @names), "\n";
        close OUTFILE;
        @names = ();
    };
};

__END__

I'm sure other regulars could show you how to do this with a one liner :-).

Cheers,
Damian
-- 
@:=grep!($;+=m!$/|#!),split//,<DATA>;@;=0..$#:;while(@;){for($;=@;;--$;;)
{@;[$;,$:]=@;[$:,$;]if($:=rand$;+$|)!=$;}push@|,shift@;if$;[0]==@|;select
$,,$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
Just another Perl Hacker # rev 3.1 -- a JAPH in progress, I guess...


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

Date: Wed, 01 Aug 2001 19:16:49 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: Splitting a text list into smaller lists?
Message-Id: <3B68B811.D9706F2C@vpservices.com>

ctverlane@NOSPAM.blackdahlia.zzn.com wrote:
> 
> I'm an Oracle dba.  I have a long list of users (25000) of them, in a
> delimited text file.  What I want to do is use Perl to take the long list and
> divide it up into 500 lists of 50 users each.

You can certainly continue on with the method you're using and I'm sure
someone will have suggestions on it for you.  An alternative is to use
AnyData or DBD::AnyData which handle delimited files as databases. 
Here's your script using the AnyData tied hash interface:

use AnyData;
my $col_names = "comma separated list of your column names";
my $flags = { sep_char = $yourDelim, col_names => $col_names };
my $db_1 = adTie( 'CSV', $yourOldFile, 'r', $flags );
my $db_2 = adTie( 'CSV', $yourNewFile_number_1, 'o', $flags );
my($rowcount,$filecount);
while (my $row = each %$db_1) {
    my $user_name = $row->{user_name} || next;
    if ( $rowcount++ == 50 ) { 
        $rowcount = 0;
        $filecount++:
        my $next_file_name = $base_file_name . $filecount . $file_ext;
        $db_2 = adTie( 'CSV', $next_file_name, 'o', $flags );
    }
    $db_2->{ $user_name } = { user_name => $user_name };
}
__END__

When the variable $db_2 gets tied to a new file name every 50 users, it
automatically closes (and releases the lock on) the old file and opens
(and locks) a new file for writing.  This script also ensures that you
are only ever dealing with a single row at a time -- it reads through
the source file a line at a time and inserts into the target file
immediately without building any kind of array or hash holding the whole
table.  If you want to add more fields in addition to user_name, just
grab that value from the $row hash.  This also takes care of any file
access problems -- it will die with an error if any of the file
operations aren't possible.

-- 
Jeff



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

Date: Thu, 02 Aug 2001 03:20:58 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Splitting a text list into smaller lists?
Message-Id: <3B68C7B2.897DA9E9@acm.org>

ctverlane@NOSPAM.blackdahlia.zzn.com wrote:
> 
> I apologize in advance for the cluelessness of this question, but I'm
> desperate, so here goes.
> 
> I'm an Oracle dba.  I have a long list of users (25000) of them, in a
> delimited text file.  What I want to do is use Perl to take the long list and
> divide it up into 500 lists of 50 users each.  So the task breaks down to 1)
> dividing the list into groups of 50, using the delimiter to show where each
> username ends, and then 2) writing each list to a separate file.
> 
> Here is my halting attempt at it:
> 
> #!/usr/bin/perl  -w
> 
> # Read the file of 25000 users from stdin:
> while ($a = <STDIN>) {
> 
> # Parse the line, using the numeral 4 as the delimiter.
>    @a = split( /4/, $a, 50);
> 
>    foreach $a (@a) {
>         print "$a";
> 
>  }
> }
> 
> exit;


#!/usr/bin/perl -w
use strict;

my $file = 'userfile01';
my @in;
# Read the file of 25000 users from stdin:
while ( <> ) {

    # Parse the line, using the numeral 4 as the delimiter.
    push @in, split /4/;
   
    while ( @in >= 50 ) {
        open OUT, "> $file" or warn "Cannot write to $file: $!" and
next;
        print splice( @in, 0, 50 );
        $file++;
        close OUT;
        }
    }


> [snip]



John
-- 
use Perl;
program
fulfillment


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

Date: 01 Aug 2001 15:37:07 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Strange problem...
Message-Id: <m1k80no19o.fsf@halfdome.holdit.com>

>>>>> "Chris" == Chris Stith <mischief@velma.motion.net> writes:

Chris> I don't think the OP rolled this. I've seen this exact same code,
Chris> right down to the same variables. I'm not sure from where it comes,
Chris> but it must be coming from a published source.

It's been cargo-culted everywhere.  I suspect it came from cgi-lib.pl
or something like that, lifted and inserted into all of Matt Wright's
code, which became the defacto standard scriptlib for the
dead-camel-Perl generation.  And that became the source of the
dangerous underpowered memes that must be eradicated, one by one.

-- 
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: Wed, 01 Aug 2001 16:37:58 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Strange problem...
Message-Id: <3B6892D6.D0F5BD72@stomp.stomp.tokyo>

Randal L. Schwartz wrote:
 
> >>>>> "Chris" == Chris Stith <mischief@velma.motion.net> wrote:
 
> Chris> I don't think the OP rolled this. I've seen this exact same code,
> Chris> right down to the same variables. I'm not sure from where it comes,
> Chris> but it must be coming from a published source.
 
> It's been cargo-culted everywhere.  I suspect it came from cgi-lib.pl
> or something like that, lifted and inserted into all of Matt Wright's
> code, which became the defacto standard scriptlib for the
> dead-camel-Perl generation.  And that became the source of the
> dangerous underpowered memes that must be eradicated, one by one.

Ahem, CGI.pm is today's Cargo Cult hot item.
 
Other than one variable being deliberately changed to create
an error, this read and parse routine under discussion is a
very common take-off on Steve Brenner's cgi-lib series, dating
back to the early nineties. I don't know the exact year Brenner
developed this method to deal with form actions. Might date
back a decade or slightly more. Brenner is the de facto father
of read and parse routines. Lincoln Stein includes some of
Brenner's code and credits him, within CGI.pm itself.

Brenner's method is quite powerful and very efficient. It can
be an excellent method if accessorized for security and parsing
of hostile html style tags. Brenner's cgi-lib series contains
quite sufficient security for most applications.

The mother of form parsing is Brigitte Jellinek. Her work in
developing read and parse routines also dates back to the
early nineties. Ms. Jellinek's methods closely parallel those
of Brenner. Unfortunately, Brigitte Jellinek has never been given
any earned credit for her contributions to the development
of robust read and parse routines. Her work is actually more
robust and efficient than Brenner's, however, she is female.

Both of these people developed methodologies upon which Stein
based quite a bit of his CGI.pm module. Brenner is credited.
Jellinek is not although there are hints of her methodology
within Stein's module.

Using CGI.pm as I have stated many times, is just fine. Trick
is knowing when to use it and, when not to use it. As you know,
I posted documentation many times showing CGI.pm to be average
thirteen times slower than a robust Brenner / Jillinek method.

This knowledge, this decision ability on which to use, in part,
sorts us programmers from what I consider to be stereotypical
Perl 5 Cargo Cultists.


Godzilla!


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

Date: Wed, 01 Aug 2001 18:06:35 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Strange problem...
Message-Id: <3B68A79B.BDAFF90A@stomp.stomp.tokyo>

Godzilla! wrote:
 
> Randal L. Schwartz wrote:
> > >>>>> Chris Stith wrote:

(snipped)
 
> > Chris> I don't think the OP rolled this. I've seen this exact same code,

> > ...I suspect it came from cgi-lib.pl


> ...Brenner is the de facto father of read and parse routines.

> The mother of form parsing is Brigitte Jellinek.


Earliest copyright work I have on file for Brenner
and Jellinek, are both dated late 1993. A presumption
can safely be made they have both been working in this
specific field of Perl for a fair decade. Both antedate
CGI.pm by two to three years.


Godzilla!


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

Date: Thu, 02 Aug 2001 00:26:30 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: The perlish way to write this?
Message-Id: <x7r8uvz4qx.fsf@home.sysarch.com>

>>>>> "B" == BCC  <a@b.c> writes:

  B> sub imageButton {
  B>   my ($href, $image, $alt, $border, $align) = @_;
  B>   $alt    = "" if !$alt;

the ||= op is a common idiom for assigning defaults. it's only weakness
is if you had a 0 in there and wanted to keep it.


  B>   $border = "0" if !$border;
  B>   $align  = "abscenter" if !$align;

	$border ||= '0' ;
	$align ||= 'abscenter ;



  B>   print "<a href=$href><img src=$image align=$align border=$border
  B> alt=\'$alt\'></a>";


yech! learn about perl's great variant quoting operators. either qq or a
here doc would be much nicer there. you don't need to escape ' in a
normal double quoted string. also you need quotes around all the tag
values for legal html

for html i almost always use here docs:

	print <<HTML ;
<a href="$href"><img src="$image" align="$align" border="$border"
alt="$alt"></a>
HTML

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs  --------------------------  http://jobs.perl.org


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

Date: Wed, 01 Aug 2001 17:38:45 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: The perlish way to write this?
Message-Id: <3B68A115.CDFAE27B@vpservices.com>

BCC wrote:
> 
> Hi,
> 
> Is this the best way to write this sub?  If not what would be an
> improvement?
> 
> sub imageButton {
>   my ($href, $image, $alt, $border, $align) = @_;
>   $alt    = "" if !$alt;
>   $border = "0" if !$border;
>   $align  = "abscenter" if !$align;
> 
>   print "<a href=$href><img src=$image align=$align border=$border
> alt=\'$alt\'></a>";
> }

sub imageButton {
   my ($href, $image, $alt, $border, $align) = @_;
   $alt    ||= '';
   $border ||= 0;
   $align  ||= 'abscenter';
   print <<EOH;
       <a href="$href">
           <img src="$image" align="$align" border="$border" alt="$alt">
       </a>
EOH
}

The or-equal (||=) is useful for situations like this where you don't
care about the difference between an undefined and a false value.  This
is also better HTML than yours because you don't know what is going to
go inside the attributes and it may well be something that requires
quote marks.  

Depending on preference, you might also want to re-write the entire
thing using CGI.pm because that would (IIRC) take care of the undefs for
you and would be even more legible in terms of mapping variables to
attributes.  I believe it would also take care of the case that you
haven't covered where one of the attributes contained an embedded quote
mark that would mess up the attribute delimiters.

> <a@b.c>

So *you're* the guy that prevents me from signing up for all those
online thingies.  They keep telling me that email address is already
registered.

-- 
Jeff



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

Date: 2 Aug 2001 03:34:47 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: The perlish way to write this?
Message-Id: <9kahon$mm7$1@bob.news.rcn.net>

BCC <a@b.c> wrote:
> Hi,

> Is this the best way to write this sub?  If not what would be an
> improvement?

> sub imageButton {
>   my ($href, $image, $alt, $border, $align) = @_;
>   $alt    = "" if !$alt;
>   $border = "0" if !$border;
>   $align  = "abscenter" if !$align;

>   print "<a href=$href><img src=$image align=$align border=$border
> alt=\'$alt\'></a>";
> }

You've already gotten several replies about using the ||= idiom.  My
suggestion is that when you've got a subroutine that takes several
optional parameters, you make them keyword parameters rather than
positional parameters.  Perl's magical autoconversion between lists and
hashes makes it extremely easy to implement keyword parameters:

sub imageButton {
  my ($href, $image)=splice(@_,0,2);
  my %opts=(alt=>"", border=>0, align=>'abscenter', @_);

# when you initialize an array from a list and there are duplicate keys,
# the last one takes precedence

  print "<a href=$href><img src=$image align=$opts{align} 
border=$opts{border} alt='$opts{alt}'></a>";
}

Now you could call it like:

imageButton('home','house.gif');  #uses default for all options
imageButton('home','house.gif',alt=>'Home');
imageButton('home','house.gif',alt=>'Home',border=>1);



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

Date: Thu, 2 Aug 2001 00:27:16 +0000 (UTC)
From: see-sig@from.invalid (David Efflandt)
Subject: Re: unlocking a directory
Message-Id: <slrn9mh7j3.4rl.see-sig@typhoon.xnet.com>

On 1 Aug 2001 10:22:10 -0700, James Schandua <js4864@txmail.sbc.com> wrote:
> I have an issue as follows:
> 
> I have had the Unix admin use directory level security to lock the
> directory in which my webpage files are held. When the user enters a
> username and password, I need the information to validate through a
> SQL Server 7 DB. How can I send the username and password to the DB,
> have them validate, and then let the users see the rest of the site
> only after logging in.

The easiest way to password protect web resources is using web server
authentication.  If you want to do the authentication yourself, you would
have to use CGI (or some sort of CGI wrapper) to serve ALL protected data.  
Either way, there is nothing Perl specific about this and it would
probably do you some good to read the docs and FAQ for your webserver.

If you had to have Unix admin lock a directory, (instead of yourself), I
am just curious how you propose to unlock it?  Your CGI would have to be
running as a user who has search permission (x) on the dir to access it at
all, read permission (r) to do a directory listing, and write permission
to create/delete files there.  This has nothing to do with Perl either.

-- 
David Efflandt  (Reply-To is valid)  http://www.de-srv.com/
http://www.autox.chicago.il.us/  http://www.berniesfloral.net/
http://cgi-help.virtualave.net/  http://hammer.prohosting.com/~cgi-wiz/


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

Date: Thu, 02 Aug 2001 10:21:35 +0800
From: YMK <gbeymk@sgh.com.sg>
Subject: Re: Verify upload is completed
Message-Id: <3B68B92F.6BFE9DFD@sgh.com.sg>

Instead of verifying file size, I am checking whether renaming of file
is successful.
If it is successful, it will start to process the file.

"B. Caligari" wrote:
> 
> "YMK" <gbeymk@sgh.com.sg> wrote in message
> news:3B651517.CA841516@sgh.com.sg...
> > Hi !
> >
> > I am doing the following:
> > 1. upload or copy a file to a "raw_file" folder in Linux
> > 2. a script to 1st verify the file size every 5 seconds
> > if there is no change in file size, assume the upload is completed
> > process the file and remove it from "raw_file" folder
> 
> <snip snip>
> 
> >
> > Is there anyway to verify whether upload or copy is completed ?
> 
> <snip snip>
> 
> You are right in observing that NT shows the 'full size'
> of the file even though it has not been fully copied over.
> 
> Assuming that if the file doesn't change in size
> is also assuming that there were no network errors,
> etc.  How about uploading the file to a temporary
> filename, and renaming it to the original filename
> from the uploading side once the transfer is complete?
> 
> B


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

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


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