[22248] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4469 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jan 26 18:05:42 2003

Date: Sun, 26 Jan 2003 15:05:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 26 Jan 2003     Volume: 10 Number: 4469

Today's topics:
        Apache::SpeedLimit <Pop@goesthe.net>
    Re: Changing the context of a code block <eric.anderson@cordata.net>
    Re: How to make standalone perlscript <Ingo_Wiarda@web.de>
        mod_perl instalation error (Daniel Gooderidge)
    Re: Possible dead links in perlfaq (Anno Siegel)
        removing a "-" <rick@shleprock.net>
    Re: removing a "-" <tassilo.parseval@post.rwth-aachen.de>
    Re: removing a "-" <eric.ehlers@btopenworld.com.nospam>
    Re: removing a "-" <rick@shleprock.net>
    Re: Reverse Inheritance? <bart.lateur@pandora.be>
    Re: Reverse Inheritance? (Anno Siegel)
        totally newbie question (Mattias)
    Re: totally newbie question <tassilo.parseval@post.rwth-aachen.de>
    Re: totally newbie question (Tad McClellan)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 26 Jan 2003 21:43:11 GMT
From: "Papa Oohmawmaw" <Pop@goesthe.net>
Subject: Apache::SpeedLimit
Message-Id: <PDYY9.43787$rM2.39106@rwcrnsc53>

I am trying to install Apache::Speedlimit. Below is the code of what I did.

I installed the code below into the /etc/httpd/conf/httpd.conf file.

<Location />
   PerlAccessHandler Apache::SpeedLimit
   PerlSetVar        SpeedLimit   20   # max 20 accesses/minute
   PerlSetVar        SpeedSamples  5   # 5 hits before doing statistics
   PerlSetVar        SpeedForgive 30   # amnesty after 30 minutes
  </Location>

I have the code below (not complete in order to shorten post). What do I
name it? Where do I put this code? How do I install it? How do I get it to
work? How do I insure that it works all the time? Is there a book, article,
etc. that I can reference? There are no other files that I have. Do I need
others? I am at a loss as to what to do, but if it works it's the answer to
what I need. Thanks for any help.

package Apache::SpeedLimit;
 # file: Apache/SpeedLimit.pm

 use strict;
 use Apache::Constants qw(:common);
 use Apache::Log ();
 use IPC::Shareable ();
 use vars qw(%DB);

 sub handler {
     my $r = shift;
     return DECLINED unless $r->is_main;  # don't handle sub-requests

     my $speed_limit = $r->dir_config('SpeedLimit') || 10; # Accesses per
minute
     my $samples = $r->dir_config('SpeedSamples')   || 10; # Sampling
threshold (hits)
     my $forgive = $r->dir_config('SpeedForgive')   || 20; # Forgive after
this period (minutes)
     my $expire = $r->dir_config('SpeedExpire')   || 40; # Expire unused
                           # records from memory after this period (minutes)







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

Date: Sun, 26 Jan 2003 17:28:36 -0500
From: "Eric Anderson" <eric.anderson@cordata.net>
Subject: Re: Changing the context of a code block
Message-Id: <pan.2003.01.26.22.28.34.385605@cordata.net>

On Sun, 26 Jan 2003 18:42:45 +0000, Uri Guttman wrote:

>>>>>> "EA" == Eric Anderson <eric.anderson@cordata.net> writes:
> 
>   EA> On Sun, 26 Jan 2003 04:21:32 +0000, Uri Guttman wrote:
>   >> that makes little sense. you still haven't given a big picture and a
>   >> reason why you want it. manipulating an object definition can be easy
>   >> or hard depending on the manipulation needed. so you have to be much
>   >> more specific and give the proper picture. your internal
>   >> understanding is useless to others without properly communicated
>   >> requirements.
> 
>   EA> The manipulation I want to do is fairly simple. I want a interface
>   EA> that allows me to specify object names, their attributes, and EA>
>   their methods.  Then my interface will mess with the symbol table EA> to
>   create an object as specified.
> 
> objects don't have names. objects don't live in symbols tables. you can
> create classes on the fly and bless objects into them. given what you say
> there i think you still have an XY problem. you are specifying a design
> need but not a requirements spec. why do you need to design such on the
> fly things? there are many ways to do it. look at class::classless for
> one. each object has its own unique class and can be created anytime.

OK, let me try to give you an idea of what I am after. Perl has a very
simple form of expressing an object. It is just a package with some
functions. Once a variable is blessed with that package name it becomes an
object.

Because of this a chunck of your code tends to be just dealing with the
details of an object, not the programming logic. There are several classes
on CPAN (mostly in Class::*) that expand how a object is defined in perl
by creating functions that act as keywords. When the functions are
called, the modules manipulate the symbol table to create the object. A
good example of this is Class::Contract.

I like this idea, but as it currently is I cannot easily combine different
ideas. For example I may want to use Class::Accessor which provides
automatic accessor generation and Class::Contract to provide contract
programming. But if I want to use both of them together (or some other
module that manipulates the object model such as class persistance) then I
can't because they were not designed to be used together.

Basically in some problem domains I may want to be able to add certain
keywords, but in other problem domains I want to add different keywords. I
would also want to combine different extensions of the class defination and
have them work together.

So what I am working on is a framework for allowing this. I am developing
a pluggable system for allowing new keywords to be added. The developer
could use what class extensions they wanted and combine them in any way it
made sense. So for example I could define an object similar to the
following:

persistant
public class 'Person' {

    get
    set
    private attribute 'name';

    private attribute 'birth_date';

    public method 'get_age' {
        # Reture the age of the person based on today's date
    }
}

In the above example I added syntax to make it easier to specify the
object. This is what Class::Contract and several other Class::* modules
do. Except they are limited in the fact that you are limited to only the
new keywords they has defined, and you can't combine it with other Class::*
modules for the most part. So if you want allow for richer syntax in
defining object, or reduce the amount of keywords (but still leave some)
then you have to change the Class::* module.

What I am doing is providing a pluggable framework so that the amount of
syntax or keywords can be controlled by what plugins are being used. For
example, in the above example I could have a module that provided the
"private/public/protected" keywords, another that provides the
"class/attribute/method" keywords, another that provides the "get/set"
keywords for accessor generation, and still another that provide the
object persistance. If a project was not using the object persistance,
then you simply do not use the object persistance plugin. This reduces
your "keywords".

But to get all these plugins will have to cooperate in manipulating the
object model to accomplish what they want, and because the plugin writer
does not neccessarly know much about the symbol table I want to create a
interface to the object model that they use so they can focus on the
manipulation needed and not the details of how to carry out the
manipulation in perl.

The final thing I want to do is provide a pluggable syntax. In the above
example we have perl code with added keywords implemented by functions.
But if you notice there are some issues such as putting the object,
attribute, and method names in quotes (so they are passed into the
function as a string). This makes the above example a bit hard to read and
error prone. If the syntax is also pluggable, and the keyword plugins
simply specify their keywords and info about those keywords then the
framework could allow alternate syntax that might make more sense. For
example:

public persistant class Person {

    private attribute name (get/set);

    private attribute birth_date;

    public method get_age {
        # Reture the age of the person based on today's date
    }
}

could be an alternate syntax. This cannot be executed directly as perl,
but the framework would translate it into the previous example before
execution. In addition to the semi-perl syntax above there could be other
syntax. For example in data objects you could even use a XML type syntax:

<object>
    <attribute name="name">
        <private>
        <get>
	<set>
    </attribute>
    <attribute name="birth_date">
	<private>
	<validation less-than="time()">
    </attribute>

    <method name="get_age">
        <public>
    </method>
</object>

The XML syntax would be ideal for pure data objects (object that are
nothing but attributes with getter/setter methods.

OK, you wanted to know what I was up to. Now that you know that, what does
this all have to do with my original question. Well, the object will be
contructed at runtime with any of the above syntax specifications. As it
contructs the objects it will want to take the code blocks that the
developer gives it (through the syntax) and define them in the correct
package. After it has defined the objects and they are being executed, I
want them to behave exactly as if the programmer had defined them
themselves in a real package. It is close to doing this, but if the
programmer tries to use the caller() function or the __PACKAGE__ symbol,
then they will not get the result they expect since the actual code block
will probably not be defined in the context of a "package MyPackage"
declaration.

I hope all of the above makes sense. I know what I want in my head, but
specifing it on paper has always been harder for me to do. The basic thing
I want to do is be able to define keywords so that object construction is
easier. These keyword additions would allow me to have keywords for
different problem spaces that I work in and allow me to not have to
continue to rewrite the same basic concepts.

I realize that often libraries are used for this same purpose, but
sometime trying to use a library to do the above become more trouble than
it is worth. Sometime additional syntax makes it much easier. For example
you can do objects in C, but it is easier to specify object in a language
that directly supports object creation. Libraries like glib/gtk use macros
to add the needed syntax to C to make it decent. I am kind of doing the
same thing here, but the objects are being contructed at runtime, and the
framework allows the different ideas to work together.

> that isn't a way to design something. you can't just code up multiple
> answers to a problem and just see if they work. you still haven't
> specified a proper requirement which state the desired result and not some
> implementation. you have to learn the difference between requirements and
> design. your writing here shows that you conflate the two but they are
> very different.

I'm not trying to code up multiple answers to my problem to see if they
work. I have the ideas already specified (given it is mostly just in my
brain and would be better on paper), and I want to do an initial
implementation so that I can see if it will truly solve problems for me.
I want to see what I may not have thought about. I can do that in one of
two ways:

1) Write my thoughts up more formally and discuss them with others to
continue to refine my thoughts. Spend my time trying to guess what I
haven't thought of, or if my ideas will work
2) Develop my thoughts thus far and see how they work out. Remove things
that don't work, come up with new solutions to problems that aren't
solved, and evaluate if I am on the right track.

Since I am for the most part a lone developer I am opting for approach
number two.

> then you still have an XY problem. you are seeking implementation answers
> to a problem that can probably be solved in a very different way. i can't
> help with only the meager info you are giving out.

I am seeking implementation answers, and perhaps it can be solved in a
very different way. I have looked at this for a while and this is the best
I have come up with thus far, but I'm sure there is tons that I have not
explored. I hope some of the specification that I have given above
provides more than meager information.
 
> i reiterate that your design direction is flawed. if you want such a
> beast, then you have a poor architecture and you have to rethink it.

I have been thinking about what I want in the back of my mind for several
years now. I know many of the drawbacks and advatanges. I have tried to
discuss it with others, but most do not seem to understand what problem I
am trying to solve or what I am trying to do. Perhaps that is a sign that
I am wasting my time and am not really solving a problem. Or perhaps it is
simply because I cannot explain it well. But I feel fairly certain with
the framework that I have described above I can use additional keywords to
be able to define classes more quickly. But I am tired of wondering, and
just want to write an initial implemenation because a working
implementation often says a lot more than hundreds of pages of
specifications.

So I hope this helps you better understand what I am doing. I have looked
at this a bit and cannot see a way to define a method in the context of
another package and have it behave exactly the same as if it were defined
in plain code. I could just require that caller() and __PACKAGE__ not be
used, but I would prefer the developer's code to behave the same with no
special exceptions. What I am wondering is if there is a way to do what I
want?

Thanks for all your help,

--
Eric Anderson



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

Date: Sun, 26 Jan 2003 22:17:52 +0100
From: Ingo Wiarda <Ingo_Wiarda@web.de>
Subject: Re: How to make standalone perlscript
Message-Id: <b11j6h$ucn62$1@ID-40614.news.dfncis.de>

Michael Peuser (h) wrote:

> 
> "Ben Morrow" <mauzo@mimosa.csv.warwick.ac.uk> schrieb im Newsbeitrag
> news:b10to0$nsb$1@wisteria.csv.warwick.ac.uk...
>> "William Hymen" <t18_pilot@hotmail.spam.com> wrote:
>> >I need to distribute a standalone perlscript
>> >"tarfile.pl" in my company.  It bundles a directory
>> >tree into a tarfile.  I do not want to run around and
>> >install Perl or the GNU toolset everywhere.
>> >I only want to give people these (4) things:
>> >
>> >someones perl.exe;
>> >someones tar.exe;
>> >my tarfile.pl
>> >install (file copy) instructions;
>> >
>> >The problem here is I need the following
>> >package:
>> >use Cwd qw(chdir getcwd abs_path);
>> >I need to use
>> >chdir , getcwd , opendir
>> >
>> >How can I include these packages into my script as
>> >a callable function ?
>>
>> You can copy the contents of Cwd.pm into the top of your script, and
> remove
>> the use Cwd; line. You can also use Archive::Tar to do the tarring. You
> may
>> need to install more than just perl.exe: I suspect you'll need perl56.dll
> or
>> some such unless you build perl yourself staticly.
>>
> 
> If you are willing to spend money look at:
> 
> - www.activestate.com  for PerlApp
> - www.indigostar.com for Perl3Exe
> 
> 
> Kindly Mike

And if you are unwilling to spend money, look at
http://perlbin.sf.net [Beta-Version but working]

Ingo


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

Date: 26 Jan 2003 12:51:06 -0800
From: dgooderidge@powerup.com.au (Daniel Gooderidge)
Subject: mod_perl instalation error
Message-Id: <cd88f4a8.0301261251.5a237f35@posting.google.com>

Hi,

I don't know if this is a bug or not but I keep getting the same error
when I compile mod_perl v2.

[root@fsln1 src]# cd mod_perl-1.99_07
[root@fsln1 mod_perl-1.99_07]# perl Makefile.PL
MP_AP_PREFIX=/usr/local/apache2/current/ MP_INST_APACHE2=1
Reading Makefile.PL args from @ARGV
   MP_AP_PREFIX = /usr/local/apache2/current/
   MP_INST_APACHE2 = 1
Configuring Apache/2.0.44 mod_perl/1.99_07 Perl/v5.8.0
Checking if your kit is complete...
Looks good
    generating script t/TEST
Checking if your kit is complete...
Looks good
Writing Makefile for Apache::Test
    generating script t/TEST
Checking if your kit is complete...
Looks good
Writing Makefile for ModPerl::Registry
Writing Makefile for API
#
#removed for clarity
#
Note (probably harmless): No library found for -lapr
Note (probably harmless): No library found for -laprutil
#
#removed for clarity
#
Writing Makefile for APR
*** mod_perl dso library will be built as mod_perl.so
*** mod_perl static library will be built as mod_perl.a
*** You'll need to add the following to httpd.conf:
***  LoadModule perl_module modules/mod_perl.so

*** Apache Perl modules will be installed relative to Apache2/
*** Don't forget to:
*** - configure 'PerlModule Apache2' in httpd.conf
*** - or 'use Apache2 ();' in a startup script
[root@fsln1 mod_perl-1.99_07]# make
#
#removed for clarity
#
gcc -I/usr/local/src/mod_perl-1.99_07/src/modules/perl
-I/usr/local/src/mod_perl-1.99_07/xs
-I/usr/local/apache2/current//include -D_REENTRANT -D_GNU_SOURCE
-fno-strict-aliasing  -I/usr/include/gdbm 
-I/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE -DMOD_PERL -O3
-fomit-frame-pointer -pipe -mcpu=pentiumpro -march=i586 -ffast-math
-fno-strength-reduce -fpic \
-c modperl_constants.c && mv modperl_constants.o modperl_constants.lo
modperl_constants.c: In function `modperl_constants_lookup_apr':
modperl_constants.c:631: `APR_POLLIN' undeclared (first use in this
function)
modperl_constants.c:631: (Each undeclared identifier is reported only
once
modperl_constants.c:631: for each function it appears in.)
modperl_constants.c:636: `APR_POLLPRI' undeclared (first use in this
function)
modperl_constants.c:641: `APR_POLLOUT' undeclared (first use in this
function)
modperl_constants.c:646: `APR_POLLERR' undeclared (first use in this
function)
modperl_constants.c:651: `APR_POLLHUP' undeclared (first use in this
function)
modperl_constants.c:656: `APR_POLLNVAL' undeclared (first use in this
function)
make[1]: *** [modperl_constants.lo] Error 1
make[1]: Leaving directory
`/usr/local/src/mod_perl-1.99_07/src/modules/perl'
make: *** [modperl_lib] Error 2

Any pointers would be a great help.

Thanks

Daniel


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

Date: 26 Jan 2003 20:49:00 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Possible dead links in perlfaq
Message-Id: <b11hjs$r63$2@mamenchi.zrz.TU-Berlin.DE>

brian d foy  <comdog@panix.com> wrote in comp.lang.perl.misc:
> [also posted to documentation@perl.org, perlfaq-workers@perl.org]
> 
> thes links have failed a couple of times this week.  anyone know where
> these might have moved to?
> 
> ------------------------------------------------------------------------
> FAILURE REPORT
> ------------------------------------------------------------------------
> 
> ======perlfaq3.pod
>         http://alpha.olm.net/
>         http://www.MultiEdit.com/

MultiEdit works fine for me. (Sun Jan 26 20:47:26 UTC 2003)

>         http://www.binevolve.com/

The other two don't resolve.

Anno


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

Date: Sun, 26 Jan 2003 19:34:55 GMT
From: "devrick" <rick@shleprock.net>
Subject: removing a "-"
Message-Id: <zLWY9.48541$Ve4.5765@sccrnsc03>

I know this is going to be simple to most of the folks in here, but I'm
having a problem removing a "-" for some reason.

Here's an example of the line I'm looking to modfy.

TEST-THIS GOODTEXT

I want to remove TEST-THIS and end up with GOODTEXT at the beginning of the
line.

I've tried a few things, but they all seem to leave me with this.

- GOODTEXT

Here's a few of the things I've tried so far, none of which have worked.
s/TEST-THIS //;
s/TEST\-THIS //;
s/TEST\W+THIS //;
s/TEST\-.THIS //;
and a few more variations.

Could someone point me in the right direction and let me know what I'm doing
wrong?  Thanks.




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

Date: 26 Jan 2003 19:51:50 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: removing a "-"
Message-Id: <b11e8m$lvp$1@nets3.rz.RWTH-Aachen.DE>

Also sprach devrick:

> I know this is going to be simple to most of the folks in here, but I'm
> having a problem removing a "-" for some reason.
> 
> Here's an example of the line I'm looking to modfy.
> 
> TEST-THIS GOODTEXT
> 
> I want to remove TEST-THIS and end up with GOODTEXT at the beginning of the
> line.
> 
> I've tried a few things, but they all seem to leave me with this.
> 
> - GOODTEXT
> 
> Here's a few of the things I've tried so far, none of which have worked.
> s/TEST-THIS //;

This one should work fine. I don't know what you did that it did not
work for you. It works fine for me.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Sun, 26 Jan 2003 20:00:44 +0000 (UTC)
From: "eric" <eric.ehlers@btopenworld.com.nospam>
Subject: Re: removing a "-"
Message-Id: <b11epb$9ht$1@venus.btinternet.com>

"devrick" <rick@shleprock.net> wrote in message
news:zLWY9.48541$Ve4.5765@sccrnsc03...
> I know this is going to be simple to most of the folks in here, but I'm
> having a problem removing a "-" for some reason.
>
> Here's an example of the line I'm looking to modfy.
>
> TEST-THIS GOODTEXT
>
> I want to remove TEST-THIS and end up with GOODTEXT at the beginning of
the
> line.
>
> I've tried a few things, but they all seem to leave me with this.
>
> - GOODTEXT
>
> Here's a few of the things I've tried so far, none of which have worked.
> s/TEST-THIS //;
> s/TEST\-THIS //;
> s/TEST\W+THIS //;
> s/TEST\-.THIS //;
> and a few more variations.
>
> Could someone point me in the right direction and let me know what I'm
doing
> wrong?  Thanks.

the first three patterns look OK to me -

use warnings;
use strict;
my @patterns=('TEST-THIS ','TEST\-THIS ','TEST\W+THIS ','TEST\-.THIS ');
for (@patterns){
  my $string="TEST-THIS GOODTEXT";
  $string=~s/$_//;
  print "pattern=>$_< output=>$string<\n";
}

output -
pattern=>TEST-THIS < output=>GOODTEXT<
pattern=>TEST\-THIS < output=>GOODTEXT<
pattern=>TEST\W+THIS < output=>GOODTEXT<
pattern=>TEST\-.THIS < output=>TEST-THIS GOODTEXT<

the fourth pattern doesn't work because there's nothing for the . to match.

i guess your problem is not in the pattern but somewhere else in your
program.  can you post a small standalone piece of code which illustrates
the problem?

-eric




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

Date: Sun, 26 Jan 2003 20:01:25 GMT
From: "devrick" <rick@shleprock.net>
Subject: Re: removing a "-"
Message-Id: <p8XY9.50522$6G4.10370@sccrnsc02>


"Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de> wrote in
message news:b11e8m$lvp$1@nets3.rz.RWTH-Aachen.DE...
> Also sprach devrick:
>
> > I know this is going to be simple to most of the folks in here, but I'm
> > having a problem removing a "-" for some reason.
> >
> > Here's an example of the line I'm looking to modfy.
> >
> > TEST-THIS GOODTEXT
> >
> > I want to remove TEST-THIS and end up with GOODTEXT at the beginning of
the
> > line.
> >
> > I've tried a few things, but they all seem to leave me with this.
> >
> > - GOODTEXT
> >
> > Here's a few of the things I've tried so far, none of which have worked.
> > s/TEST-THIS //;
>
> This one should work fine. I don't know what you did that it did not
> work for you. It works fine for me.
>
> Tassilo
> --
>
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
>
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
>
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


Hmph, yeh, that is strange.  I just ran it on it's own and it works just
fine.  Looks like something else in the program is causing some problems.
I'll have to dig a little deeper, thanks.




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

Date: Sun, 26 Jan 2003 19:27:03 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Reverse Inheritance?
Message-Id: <vbd83vk5dhsfcn03sjjh6mu4qkchkje3vj@4ax.com>

Ben Morrow wrote:

>Is it worth suggesting to p5p that
>require "Foo::Bar";
>be made to do the right thing? If you actually need to load a file called
>'Foo::Bar' you can use
>require "./Foo::Bar";

I wouldn't mind. I think MacPerl already does some of this kind of
magic, with do or with require, or both, I'm not absolutely sure.

Anyway, the system could go search for the literal filename first...
when it's not found, it could try a second time, this time with this
substitution.

When you think about it, it seems kind of silly one has to hardcode this
kind of conversion, *which is already built into perl*, only you can't
reach it.

-- 
	Bart.


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

Date: 26 Jan 2003 20:12:48 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Reverse Inheritance?
Message-Id: <b11fg0$r63$1@mamenchi.zrz.TU-Berlin.DE>

Bart Lateur  <bart.lateur@pandora.be> wrote in comp.lang.perl.misc:
> Ben Morrow wrote:
> 
> >Is it worth suggesting to p5p that
> >require "Foo::Bar";
> >be made to do the right thing? If you actually need to load a file called
> >'Foo::Bar' you can use
> >require "./Foo::Bar";
> 
> I wouldn't mind. I think MacPerl already does some of this kind of
> magic, with do or with require, or both, I'm not absolutely sure.
> 
> Anyway, the system could go search for the literal filename first...
> when it's not found, it could try a second time, this time with this
> substitution.

That would make the interpretation depend on the current contents of
the file system.  Not that that is necessarily a bad thing.

> When you think about it, it seems kind of silly one has to hardcode this
> kind of conversion, *which is already built into perl*, only you can't
> reach it.

Currently the conversion is only done when the parameter is given as a
bareword (which makes it hard to access).  Applying the substitution in more
cases means that the new require() may spuriously find a module where Old
Code would expect it not to.  A toughie if it happens, but perhaps
sufficiently unlikely to be acceptable.

Anno


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

Date: 26 Jan 2003 13:01:46 -0800
From: mattias@mail.nu (Mattias)
Subject: totally newbie question
Message-Id: <cd848ce5.0301261301.21c972ee@posting.google.com>

Hi I´m totally new at this. I found a free perl script that saves text
into a text file names_file.txt.

Heres the code:
___________________
#!/usr/bin/perl
#save_file.pl
read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
$buffer =~ tr/+/ /;
$buffer =~ s/\r/ /g;
$buffer =~ s/\n/ /g;
@pairs = split(/&/,$buffer);
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$formdata{$key}.="$value";
}
$first=$formdata{'first'};
$last=$formdata{'last'}; 

open(INFO, ">>names_file.txt"); # Open for appending
print INFO "$last|$first\n";
close (INFO);


print "Content-type:text/html\n\n"; #Content Header

print <<End_of_Doc;
<html>
<head><title>Sample Response Page</title></head>
<body>
Thank you: $first $last
</body>
</html>
End_of_Doc
----------------

This works fine but I want to delete everything in the text file
before the new text gets there. Not append it like it does know. How
do I do?
/Mattias


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

Date: 26 Jan 2003 21:20:55 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: totally newbie question
Message-Id: <b11jfn$r74$1@nets3.rz.RWTH-Aachen.DE>

Subject: totally newbie question

Please spend a little more time with finding a meaningful subject. This
one is useless, to put it mildly.


Also sprach Mattias:

> Hi I´m totally new at this. I found a free perl script that saves text
> into a text file names_file.txt.

Unfortunately, free is often synonym to cargo cult.

> Heres the code:
> ___________________
> #!/usr/bin/perl
> #save_file.pl
> read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
> $buffer =~ tr/+/ /;
> $buffer =~ s/\r/ /g;
> $buffer =~ s/\n/ /g;
> @pairs = split(/&/,$buffer);
> foreach $pair(@pairs){
> ($key,$value)=split(/=/,$pair);
> $formdata{$key}.="$value";
> }
> $first=$formdata{'first'};
> $last=$formdata{'last'}; 
> 
> open(INFO, ">>names_file.txt"); # Open for appending
> print INFO "$last|$first\n";
> close (INFO);
> 
> 
> print "Content-type:text/html\n\n"; #Content Header
> 
> print <<End_of_Doc;
><html>
><head><title>Sample Response Page</title></head>
><body>
> Thank you: $first $last
></body>
></html>
> End_of_Doc
> ----------------
> 
> This works fine but I want to delete everything in the text file
> before the new text gets there. Not append it like it does know. How
> do I do?

By changing the open-statemt:

    open INFO, ">names_files.txt" or die $!;
                ^

'>>' means appending to the end while '>' will wipe the previous
content.

If you have any (even mild) ambitions with Perl you should first learn
that CGI-scripts are best done using the CGI.pm module which is the
de-facto standard for such tasks. It will also make your script much
shorter. Here's a complete replacement for the above:

    #!/usr/bin/perl -w
    
    use strict;
    use CGI::Carp qw/fatalsToBrowser/; # errors go to the browser
    use CGI       qw/param header/;    # we need param() and header()

    my $first = param("first");
    my $last  = param("last");

    open INFO, ">names_files-txt" 
        or die $!;                      # in case opening fails
    print INFO "$last|$first\n";
    close INFO;
    
    print header();
    print <<EOHTML;
    <html>
    <head><title>Sample Response Page</title></head>
    <body>
     Thank you: $first $last
    </body>
    </html>
    EOHTML

All CGI-related matters are carried out by the CGI module (reading
parameters with param(), printing out a header with header()).

Perl also comes with excessive documentation. See 'perldoc perl' for a
long list of available manpages. You read the one you are interested in
with 'perldoc <manpage>'. 'perldoc CGI' will tell you all about the CGI
module but this will only be of use to you if you are a little more
familiar with Perl.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Sun, 26 Jan 2003 16:32:59 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: totally newbie question
Message-Id: <slrnb38ogr.url.tadmc@magna.augustmail.com>

Mattias <mattias@mail.nu> wrote:

> Hi I´m totally new at this. 


Perhaps you do not realize it, but you are totally new to *these*.

You are learning several things all at once. They will become
all intertwingled in your mind and you won't be able to sort
out which one is causing a particular problem very easily.

I strongly suggest that you learn Perl first, before turning
your attention to a particular application area, most especially
CGI programs that you are going to put on a publically
available web server.

CGI is a rather advanced area to write Perl programs for. Some
of the advanced things that typical CGI programs need to handle are:

   1) multi-tasking, file locking
   2) encoding/decoding
   3) multiple input possibilities
   4) security
   5) performance if on a popular web site


You should try things that are simpler than CGI while learning Perl.


> I found a free perl script 
            ^^^^

It is worth a good deal *less* than what you paid for it.

It will cost you money if you put it on a live web site.

It is buggy and clearly written by an amateur. Learning other
people's bad practices means you will just have to unlearn
them after you have acquired clue.

You should study good programs, rather than bad ones, when
learning Perl.


> that saves text
> into a text file names_file.txt.
> 
> Heres the code:


This POS code has at least the first 4 Bad Things listed above,
all worked into just 15 lines of code!

Perl lets you get a lot of work done with only a little code,
but it is hoped that it is _good_ work.  :-)


> #!/usr/bin/perl


You should ask for all the help you can get. Perl itself will
catch many common programming mistakes for you, but you have
to ask for the help:

   use strict;
   use warnings;


> read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});


That line has problems #3 and #4.

   3) multiple input possibilities
      It only handles GET requests. What about POST requests?

   4) security
      What will it do if the CONTENT_LENGTH env var is
      given a really huge number, like 2000000000000 ?

It also does not check to see if the read() succeeded or failed.

Blindly continuing to attempt to process data when the data was
never acquired in the first place is pretty silly.

It also makes a very poor attempt at URL decoding, when there
are modules that will do it flawlessy with a couple lines of code,
such as the all-singing, all-dancing CGI.pm module.


> $buffer =~ tr/+/ /;
> $buffer =~ s/\r/ /g;
> $buffer =~ s/\n/ /g;
> @pairs = split(/&/,$buffer);
> foreach $pair(@pairs){
> ($key,$value)=split(/=/,$pair);
> $formdata{$key}.="$value";
> }


   2) encoding/decoding
      It does not do URL decoding correctly


> $first=$formdata{'first'};
> $last=$formdata{'last'}; 
> 
> open(INFO, ">>names_file.txt"); # Open for appending


You should always, yes *always*, check the return value from open():

   open(INFO, ">>names_file.txt") or die "could not open 'names_file.txt' $!";

Replace die() with something more appropriate to the CGI environment.

   1) multi-tasking, file locking
      The contents of the file are likely to become corrupted.
      You need to implement file locking in a multi-tasking
      environment such as CGI.


> This works fine 


No it doesn't, you just haven't happened to throw data at it
that will reveal its bugs.


> but I want to delete everything in the text file
> before the new text gets there. Not append it like it does know. How
> do I do?


Use 1 angle bracket instead of 2 in the open() statement.


Learn Perl from the command line first.

Once you move on to CGI programming, get the _CGI_ program
working from the command line before moving to the web server.



-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

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


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