[24465] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6648 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jun 3 14:06:07 2004

Date: Thu, 3 Jun 2004 11: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)

Perl-Users Digest           Thu, 3 Jun 2004     Volume: 10 Number: 6648

Today's topics:
        Escaping single quotes with sql (Wilco van der Veer)
    Re: Escaping single quotes with sql <jack_challen@ocsl.co.uk>
    Re: Escaping single quotes with sql <odyniec-usenet@odyniec.net>
    Re: Escaping single quotes with sql <glex_nospam@qwest.invalid>
    Re: Escaping single quotes with sql ctcgag@hotmail.com
    Re: Escaping single quotes with sql <ghostie2000@hotmail.com>
    Re: Escaping single quotes with sql <ittyspam@yahoo.com>
    Re: Escaping single quotes with sql <ghostie2000@hotmail.com>
    Re: Escaping single quotes with sql <dwall@fastmail.fm>
    Re: Escaping single quotes with sql <ghostie2000@hotmail.com>
    Re: Escaping single quotes with sql <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: Escaping single quotes with sql <usenet@morrow.me.uk>
    Re: Escaping single quotes with sql BreadWithSpam@fractious.net
    Re: ISO two Perl idioms... <zen13097@zen.co.uk.no.spam.invalid>
    Re: ISO two Perl idioms... <dwall@fastmail.fm>
        mnemonics, was Re: problem with rand on OSX <cabneySPAM@SPAMucsdSPAM.SPAMedu>
    Re: my @hash{ @array } = syntax? (was Re: Joining 2 arr ctcgag@hotmail.com
    Re: my @hash{ @array } = syntax? (was Re: Joining 2 arr <usenet@morrow.me.uk>
    Re: Perl tutorial at U Penn on Friday 4 June (Mark Jason Dominus)
    Re: Perl tutorial at U Penn on Friday 4 June (Mark Jason Dominus)
    Re: problem with rand on OSX <cabneySPAM@SPAMucsdSPAM.SPAMedu>
    Re: problem with rand on OSX <cabneySPAM@SPAMucsdSPAM.SPAMedu>
        regexp <wojmichal@poczta.onet.pl>
    Re: regexp (Walter Roberson)
    Re: regexp <wojmichal@poczta.onet.pl>
    Re: Why is this upload script not working (Mark Constant)
    Re: Why is this upload script not working <usenet@morrow.me.uk>
    Re: Why is this upload script not working <noreply@gunnar.cc>
    Re: Win32::OLE, WMI and executing methods <Petri_member@newsguy.com>
    Re: Win32::OLE, WMI and executing methods <usenet@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 3 Jun 2004 08:31:12 -0700
From: ghostie2000@hotmail.com (Wilco van der Veer)
Subject: Escaping single quotes with sql
Message-Id: <25a32d80.0406030731.75cc2370@posting.google.com>

Goodday,

I am trying to put values that may contain a ' (single quote) into a
sql database but it keeps crashing when the value contains a '. I have
tried using the quote function:

$string = "'s avond's";
my $clean_string = $dbh->quote($string);

But even then it crashes whenever a ' appears.

Does anyone have a solution for this?

Thanks,

Wilco van der Veer


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

Date: Thu, 03 Jun 2004 16:45:30 +0100
From: Jack Challen <jack_challen@ocsl.co.uk>
Subject: Re: Escaping single quotes with sql
Message-Id: <EIHvc.3$OG.420@psinet-eu-nl>

Wilco van der Veer wrote:
> I am trying to put values that may contain a ' (single quote) into a
> sql database but it keeps crashing when the value contains a '. I have
> tried using the quote function:
> 
> $string = "'s avond's";
> my $clean_string = $dbh->quote($string);


What happens if you try using placeholders (untested code ahead)?

my $query = "INSERT INTO foo VALUES(?)";
my $sth = $dbh->prepare($query);
$sth->execute($string);

Cheers
jack


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

Date: 03 Jun 2004 17:47:44 +0200
From: Michal Wojciechowski <odyniec-usenet@odyniec.net>
Subject: Re: Escaping single quotes with sql
Message-Id: <87k6yozlr3.fsf@odyniec.odyniec.net>

ghostie2000@hotmail.com (Wilco van der Veer) writes:

> I am trying to put values that may contain a ' (single quote) into a
> sql database but it keeps crashing when the value contains a '. I have
> tried using the quote function:
> 
> $string = "'s avond's";
> my $clean_string = $dbh->quote($string);

What happens next? How do you use $clean_string in a query?

-- 
Michal Wojciechowski : for(<>){/\s/,$l{$m=$`}=$'}$_ : 10 PRINT "Yet another"
odyniec()odyniec;net : =$l{$c},/O\s/?$c=$'-1:y/"//d : 20 PRINT "Perl hacker"
 http://odyniec.net  : ,/T\s/?print$':0while$c++<$m : 30 GOTO 10


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

Date: Thu, 03 Jun 2004 10:49:25 -0500
From: "J. Gleixner" <glex_nospam@qwest.invalid>
Subject: Re: Escaping single quotes with sql
Message-Id: <aMHvc.13$Ta1.12802@news.uswest.net>

Wilco van der Veer wrote:
> Goodday,
> 
> I am trying to put values that may contain a ' (single quote) into a
> sql database but it keeps crashing when the value contains a '. I have
> tried using the quote function:
> 
> $string = "'s avond's";
> my $clean_string = $dbh->quote($string);
> 
> But even then it crashes whenever a ' appears.
> 
> Does anyone have a solution for this?

That's one way to do it. It will work, depending on how you're using it. 
   Show us your code where you use $clean_string to do your insert.


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

Date: 03 Jun 2004 15:56:50 GMT
From: ctcgag@hotmail.com
Subject: Re: Escaping single quotes with sql
Message-Id: <20040603115650.571$CF@newsreader.com>

ghostie2000@hotmail.com (Wilco van der Veer) wrote:

> Goodday,
>
> I am trying to put values that may contain a ' (single quote) into a
> sql database but it keeps crashing when the value contains a '. I have
> tried using the quote function:


First off, "Use bind variables and place holders" is the best answer to
most of the DBI questions posted here.  Yours is no exception.  Use place
holders and bind variables.


> $string = "'s avond's";
> my $clean_string = $dbh->quote($string);
>
> But even then it crashes whenever a ' appears.

What do mean by "crashes"?  Perl produces error messages for your
benefit.  Do you not think that we should at least look at them?

> Does anyone have a solution for this?

Yes.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Thu, 3 Jun 2004 17:58:21 +0200
From: "Wilco van der Veer" <ghostie2000@hotmail.com>
Subject: Re: Escaping single quotes with sql
Message-Id: <40bf4a9e$0$48933$e4fe514c@news.xs4all.nl>


"J. Gleixner" <glex_nospam@qwest.invalid> wrote in message
news:aMHvc.13$Ta1.12802@news.uswest.net...
>
> That's one way to do it. It will work, depending on how you're using it.
>    Show us your code where you use $clean_string to do your insert.

#!/usr/bin/perl

# load module
use DBI();

# connect
my $dbh = DBI->connect("DBI:PgPP:database=honnepon;host=$hostname", "$user",
"$pass", {'RaiseError' => 1});

$string = "'s avond's";
my $clean_string = $dbh->quote($string);

my $sth = $dbh->prepare("INSERT INTO files (filename) VALUES ('$string');");
$sth->execute();

# clean up
$dbh->disconnect();




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

Date: Thu, 3 Jun 2004 12:11:53 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Escaping single quotes with sql
Message-Id: <20040603121002.X8971@dishwasher.cs.rpi.edu>

On Thu, 3 Jun 2004, Wilco van der Veer wrote:

> $string = "'s avond's";
> my $clean_string = $dbh->quote($string);
>
> my $sth = $dbh->prepare("INSERT INTO files (filename) VALUES ('$string');");

quote() doesn't modify its argument, it returns a new string.  $string is
exactly what it was before the call to quote().  You should be using
$clean_string in the SQL statement.

However, the 'correct' answer is still to be using place holders, as
otehrs in this thread have said.

read perldoc DBI for more info.

Paul Lalli


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

Date: Thu, 3 Jun 2004 18:15:55 +0200
From: "Wilco van der Veer" <ghostie2000@hotmail.com>
Subject: Re: Escaping single quotes with sql
Message-Id: <40bf4eba$0$33920$e4fe514c@news.xs4all.nl>


<ctcgag@hotmail.com> wrote in message
news:20040603115650.571$CF@newsreader.com...
>
> > $string = "'s avond's";
> > my $clean_string = $dbh->quote($string);
> >
> > But even then it crashes whenever a ' appears.
>
> What do mean by "crashes"?  Perl produces error messages for your
> benefit.  Do you not think that we should at least look at them?

Errorcode:

DBD::PgPP::st execute failed: ERROR:  parser: parse error at or near "s" at
character 40
DBD::PgPP::st execute failed: ERROR:  parser: parse error at or near "s" at
character 40




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

Date: Thu, 03 Jun 2004 16:16:44 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Escaping single quotes with sql
Message-Id: <Xns94FD7CE87920dkwwashere@216.168.3.30>

Wilco van der Veer <ghostie2000@hotmail.com> wrote:

> #!/usr/bin/perl

No strictures or warnings?  Or did you just cut them from your post?

> # load module
> use DBI();

DBI doesn't export anything, so the parentheses are unnecessary.

> 
> # connect
> my $dbh =
> DBI->connect("DBI:PgPP:database=honnepon;host=$hostname", "$user", 
> "$pass", {'RaiseError' => 1});

You don't need quotes around $user and $pass.  See 
perldoc -q quoting

> $string = "'s avond's";
> my $clean_string = $dbh->quote($string);

This line is not necessary if you use placeholders, as at least two 
people suggested. See below.

> my $sth = $dbh->prepare("INSERT INTO files (filename) VALUES
> ('$string');"); 
> $sth->execute();

Better written as (rearranged to fit into an 80-character line)

    my $sth = $dbh->prepare(
        'INSERT INTO files (filename) VALUES (?)'
    ); 
    $sth->execute($string);

> # clean up
> $dbh->disconnect();


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

Date: Thu, 3 Jun 2004 18:22:30 +0200
From: "Wilco van der Veer" <ghostie2000@hotmail.com>
Subject: Re: Escaping single quotes with sql
Message-Id: <40bf5046$0$48959$e4fe514c@news.xs4all.nl>


"David K. Wall" <dwall@fastmail.fm> wrote in message
news:Xns94FD7CE87920dkwwashere@216.168.3.30...
> Wilco van der Veer <ghostie2000@hotmail.com> wrote:
> Better written as (rearranged to fit into an 80-character line)
>
>     my $sth = $dbh->prepare(
>         'INSERT INTO files (filename) VALUES (?)'
>     );
>     $sth->execute($string);
>


Great, that works!

Thanks alot for the help everbody!




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

Date: Thu, 3 Jun 2004 09:24:04 -0700
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: Escaping single quotes with sql
Message-Id: <4bjn9c.un9.ln@goaway.wombat.san-francisco.ca.us>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

On 2004-06-03, Wilco van der Veer <ghostie2000@hotmail.com> wrote:
>
> #!/usr/bin/perl

Is it safe to assume that you have

use strict;
use warnings;

in your code?

[snip DBI code]

> $string = "'s avond's";
> my $clean_string = $dbh->quote($string);
>
> my $sth = $dbh->prepare("INSERT INTO files (filename) VALUES ('$string');");
                                                                 ^^^^^^^
Did you mean to use the unquoted string?  :)

Even so, if you read perldoc DBI (you did that, right?), and look for the
description of the quote method, you'll see that $clean_string should
probably already have the outside quotes.  You'll probably want

my $sth = $dbh->prepare("INSERT INTO files (filename) VALUES ($clean_string)");

Or, as someone else mentioned, just use placeholders, which will allow
DBI and DBD::PgPP to figure out all the quoting for you.

- --keith

- -- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAv1ChhVcNCxZ5ID8RAomXAJ9QZR9q6c6i5sHNnbOCAUFoKbybTACeJ1xS
akvli8piiNRIfYmA4Ixu6y8=
=K3FM
-----END PGP SIGNATURE-----


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

Date: Thu, 3 Jun 2004 16:44:24 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Escaping single quotes with sql
Message-Id: <c9nkh8$ppt$2@wisteria.csv.warwick.ac.uk>


Quoth "David K. Wall" <dwall@fastmail.fm>:
> > my $dbh =
> > DBI->connect("DBI:PgPP:database=honnepon;host=$hostname", "$user", 
> > "$pass", {'RaiseError' => 1});
> 
> You don't need quotes around $user and $pass.  See 
> perldoc -q quoting

Nor around 'RaiseError'.=> autoquotes its left argument if it matches 
/^\w+$/.

Ben

-- 
For the last month, a large number of PSNs in the Arpa[Inter-]net have been
reporting symptoms of congestion ... These reports have been accompanied by an
increasing number of user complaints ... As of June,... the Arpanet contained
47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] * ben@morrow.me.uk


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

Date: 03 Jun 2004 13:55:30 -0400
From: BreadWithSpam@fractious.net
Subject: Re: Escaping single quotes with sql
Message-Id: <yobise8plv1.fsf@panix2.panix.com>

ctcgag@hotmail.com writes:
> ghostie2000@hotmail.com (Wilco van der Veer) wrote:

> > I am trying to put values that may contain a ' (single quote) into
> > a sql database but it keeps crashing when the value contains a
> > '. I have tried using the quote function:

> First off, "Use bind variables and place holders" is the best answer
> to most of the DBI questions posted here.  Yours is no exception.
> Use place holders and bind variables.

If available.

Not so much with DBD::Sybase and FreeTDS pointing at SQL Server.

Can't speak for the OP, but in my case, I'm stuck playing
the quote game, unpleasant as it is.

FWIW.  Annoying when building DBD::Sybase - certain tests fail.



-- 
Plain Bread alone for e-mail, thanks.  The rest gets trashed.
No HTML in E-Mail! --    http://www.expita.com/nomime.html
Are you posting responses that are easy for others to follow?
   http://www.greenend.org.uk/rjk/2000/06/14/quoting


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

Date: 03 Jun 2004 15:10:03 GMT
From: Dave Weaver <zen13097@zen.co.uk.no.spam.invalid>
Subject: Re: ISO two Perl idioms...
Message-Id: <slrncbufqb.5oj.zen13097@wormhole.dontmind.net>

On Thu, 20 May 2004 20:19:27 -0000, David K. Wall <dwall@fastmail.fm> wrote:
>  
>  This could be a bit shorter and still not be obfuscated:
>  
>      while (<DATA>) {
>          last if meets_condition($_);
>      }
>      die "Bad data" unless $_;

Perhaps you meant:

	die "Bad data" unless meets_condition($_);
?

Otherwise a value of 0 in the data that meets the condition will give "Bad
data".

-- 
Dave.


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

Date: Thu, 03 Jun 2004 16:06:27 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: ISO two Perl idioms...
Message-Id: <Xns94FD7B29483B5dkwwashere@216.168.3.30>

Dave Weaver <zen13097@zen.co.uk.no.spam.invalid> wrote:

> On Thu, 20 May 2004 20:19:27 -0000, David K. Wall
> <dwall@fastmail.fm> wrote: 
>>  
>>  This could be a bit shorter and still not be obfuscated:
>>  
>>      while (<DATA>) {
>>          last if meets_condition($_);
>>      }
>>      die "Bad data" unless $_;
> 
> Perhaps you meant:
> 
>      die "Bad data" unless meets_condition($_);
> ?
> 
> Otherwise a value of 0 in the data that meets the condition will
> give "Bad data".

Yes, Uri Guttman pointed that out later in the thread. 


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

Date: 2 Jun 2004 17:35:57 -0600
From: "C. A." <cabneySPAM@SPAMucsdSPAM.SPAMedu>
Subject: mnemonics, was Re: problem with rand on OSX
Message-Id: <40be645d$1_1@omega.dimensional.com>

On Wed, 02 Jun 2004 16:06:28 -0600, Eric Schwartz <emschwar@pobox.com> wrote:
> "C. A." <cabneySPAM@SPAMucsdSPAM.SPAMedu> writes:

>> if ( $pid != 0 ) {
> 
> This would read more clearly if it were
> 
>   if ($parent) {

parent is a better mnemonic in both the parent and the child, I'll agree.
The fact it's value is the process id of the child (in the parent) could
be considered a side effect?

> or even 
> 
>   if (@jobs) {

@jobs is not empty in two of the three children.

Yours,

CA
-- 


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

Date: 03 Jun 2004 15:30:19 GMT
From: ctcgag@hotmail.com
Subject: Re: my @hash{ @array } = syntax? (was Re: Joining 2 arrays into hashes)
Message-Id: <20040603113019.861$v5@newsreader.com>

Kevin Collins <spamtotrash@toomuchfiction.com> wrote:
> >
> > Uhm, I think the {} clearly shows what the intentions are? I mean, I
> > can do my %array = @array; (And I am happy with that)
>
> Yes, but think of it this way: you are defining a *slice* of a hash, not
> (necessarily) the whole hash. Its the same reason you can't do:
>
> my $var[2] = 'val';
>
> You can't lexically scope only part of an array or a hash, you have to do
> the whole thing.

But we aren't asking to lexically scope part of an array or hash, just to
be able to lexically scope an entire array or hash in the same statement
that initializes a portion of it.  I don't see any fundamental reason it
wouldn't work, as far as I can tell it doesn't introduce any syntactic
ambiguity.  It would just be a matter of adding a little more DWIM to the
language.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Thu, 3 Jun 2004 16:41:49 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: my @hash{ @array } = syntax? (was Re: Joining 2 arrays into hashes)
Message-Id: <c9nkcd$ppt$1@wisteria.csv.warwick.ac.uk>


Quoth ctcgag@hotmail.com:
> Kevin Collins <spamtotrash@toomuchfiction.com> wrote:
> > >
> > > Uhm, I think the {} clearly shows what the intentions are? I mean, I
> > > can do my %array = @array; (And I am happy with that)
> >
> > Yes, but think of it this way: you are defining a *slice* of a hash, not
> > (necessarily) the whole hash. Its the same reason you can't do:
> >
> > my $var[2] = 'val';
> >
> > You can't lexically scope only part of an array or a hash, you have to do
> > the whole thing.
> 
> But we aren't asking to lexically scope part of an array or hash, just to
> be able to lexically scope an entire array or hash in the same statement
> that initializes a portion of it.  I don't see any fundamental reason it
> wouldn't work, as far as I can tell it doesn't introduce any syntactic
> ambiguity.  It would just be a matter of adding a little more DWIM to the
> language.

Yes. However, it would be hard to implement given the current perl
parser: the subject of a my would have to have two semantic values: the
object that was being defined and the lvalue that was the return value
of the my (in this case, the whole array and the slice respectively).
This would be very messy to put into perly.y.

Ben

-- 
               We do not stop playing because we grow old; 
                  we grow old because we stop playing.
                            ben@morrow.me.uk


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

Date: Wed, 2 Jun 2004 23:29:12 +0000 (UTC)
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Perl tutorial at U Penn on Friday 4 June
Message-Id: <c9lns8$jls$1@plover.com>

In article <c9ld2f$4j4$1@plover.com>,
Mark Jason Dominus <mjd@plover.com> wrote:
>
>
>Friday, 4 June.
>
>The tutorial will start around 6:30 PM and will last until about
>10:00, including a 30-minute break in the middle.

Sorry, this was incorrect.

The tutorial will start around 6:00 PM and will last until about 9:30.

I apologize for any inconvenience my error might cause.



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

Date: Wed, 2 Jun 2004 23:38:01 +0000 (UTC)
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Perl tutorial at U Penn on Friday 4 June
Message-Id: <c9locp$kga$1@plover.com>

In article <c9lns8$jls$1@plover.com>,
Mark Jason Dominus <mjd@plover.com> wrote:
>In article <c9ld2f$4j4$1@plover.com>,
>Mark Jason Dominus <mjd@plover.com> wrote:
>>
>>
>>Friday, 4 June.
>>
>>The tutorial will start around 6:30 PM and will last until about
>>10:00, including a 30-minute break in the middle.
>
>Sorry, this was incorrect.
>
>The tutorial will start around 6:00 PM and will last until about 9:30.
>
>I apologize for any inconvenience my error might cause.
>


Moreover, I posted the wrong location.

The talk will be in Heilmeier hall (room 100) of the Towne Building,
at 220 South 33rd St.

        http://www.facilities.upenn.edu/mapsBldgs/view_map.php3?id=158

It will begin around 6:00.

I hope I did not make any other mistakes.



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

Date: 3 Jun 2004 09:45:23 -0600
From: "C. A." <cabneySPAM@SPAMucsdSPAM.SPAMedu>
Subject: Re: problem with rand on OSX
Message-Id: <40bf4793_3@omega.dimensional.com>

On 3 Jun 2004, Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
> C. A. <cabneySPAM@SPAMucsdSPAM.SPAMedu> wrote in comp.lang.perl.misc:
>> [fork & rand on OSX]
> 
> Perl initializes its random generator once at startup.  The kids inherit
> the interpreter, complete with initialized random generator, so the
> random sequence is the same in all kids.  Call srand() in each of the kids.

Perl porters says this is fixed on 5.8.4 (that srand isn't called until rand
is), but someone told me it still has this behavior in OpenBSD, even
on 5.8.4.bleeding.

Yours,

Charles
-- 


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

Date: 3 Jun 2004 09:57:43 -0600
From: "C. A." <cabneySPAM@SPAMucsdSPAM.SPAMedu>
Subject: Re: problem with rand on OSX
Message-Id: <40bf4a77$1_3@omega.dimensional.com>

On 2 Jun 2004 15:48:07 -0600, C. A. <cabneySPAM@SPAMucsdSPAM.SPAMedu> wrote:
> I expect each child to have a different $slp, but they are identical:

from perlbug:

-----Forwarded Message-----

From: Dominic Dunlop <domo@computer.org>
To: perl5-porters@perl.org
Cc: C. Abney (via RT) <perlbug-followup@perl.org>
Subject: Re: [perl #30028] rand broken on Mac OSX (G5), was Re: problem with~
Date: 03 Jun 2004 16:59:11 +0200

Summary: fixed in 5.8.4.

On 3 Jun 2004, at 04:21, C. Abney (via RT) wrote:
> #! /usr/bin/perl -w
>
> use strict;
>
> my @jobs;
>
> for (0..2)
> {
>         defined( my $pid = fork() ) or die "my BABY!!!";;
>         last if $pid == 0;
>         push @jobs, $pid;
> }
>
> if ( 3 == @jobs ) {
>         $SIG{CHLD} = 'IGNORE';
>         sleep 1;
> } else {
>         my $slp = int rand 20;
>
>         print "child gets $slp second(s)\n";
>         exit 0;
> }
>
Well, on my Mac OS 10.3.4 (with a G3 processor) that indeed gives

child gets 6 second(s)
child gets 6 second(s)
child gets 6 second(s)

(or similar) with perl5.8.1rc3 (as shipped with Mac OS) or with a 
freshly-built perl5.8.1. Given that the random number seed is stored in 
memory that gets cloned from the parent to each child, and each child 
applies the same algorithm to the seed to get the next random number, 
this is the behaviour I'd expect. However, I get

child gets 19 second(s)
child gets 11 second(s)
child gets 0 second(s)

(or similar) with perl5.8.4, the current stable release, and with 5.9.2 
on the development track.

Both perls are using the same random number generator:
$ perl '-V:rand.*'  # either perl version
randbits='48'
randfunc='drand48'
random_r_proto='0'
randseedtype='long'

What seems to have changed is where the random number generator seed 
gets set: in 5.8.1, it appears to be in the parent before any call to 
rand(), so each child gets the same seed; in 5.8.4 the setting seems to 
be delayed until the first use of rand() -- so each child gets a 
different seed. Adding a line

rand;

near the top of the script to force the setting of the seed in the 
parent brings back the 5.8.1 behaviour.
I can't see a patch between 5.8.1 and 5.8.4 which claims to have 
anything to do with random numbers so I don't know what changed the 
behaviour. Can anybody shed any light?

However, your solution is to upgrade to 5.8.4.

[schibble]



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

Date: Thu, 03 Jun 2004 19:17:49 +0200
From: "Wojtek Michalik" <wojmichal@poczta.onet.pl>
Subject: regexp
Message-Id: <pan.2004.06.03.17.17.48.853019@poczta.onet.pl>

Can anybody give me en example of working regex
that will match sample text given below:
____________
banana\n
\tapple\n
____________
\t=tab and \n=nweline 
I have been trying to match it by:
perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms 
nothing worked.
I use perl version 5.8 on linux


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

Date: 3 Jun 2004 17:22:33 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: regexp
Message-Id: <c9nmop$8p9$1@canopus.cc.umanitoba.ca>

In article <pan.2004.06.03.17.17.48.853019@poczta.onet.pl>,
Wojtek Michalik <wojmichal@poczta.onet.pl> wrote:
:Can anybody give me en example of working regex
:that will match sample text given below:
:____________
:banana\n
:\tapple\n
:____________
:\t=tab and \n=nweline 
:I have been trying to match it by:
:perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms 
:nothing worked.

You haven't really defined what your pattern is.

I would ask, though, why you are using the g modifier? Is this all part
of a larger string that there might be several such matches on? If so
then what happens if there is a line  strawberry-banana  in there? You
haven't anchored the banana portion.

I would also ask why you escaped the period and slash in your replacement
pattern? In replacement patterns, period and slash stand for themselves
[unless one of them happens to be the pattern delimeter.]

To confirm: the output you would expect over those lines would be

banana\n
\t./apple\n

??
-- 
Aleph sub {Aleph sub null} little, Aleph sub {Aleph sub one} little,
Aleph sub {Aleph sub two} little infinities...


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

Date: Thu, 03 Jun 2004 19:42:30 +0200
From: "Wojtek Michalik" <wojmichal@poczta.onet.pl>
Subject: Re: regexp
Message-Id: <pan.2004.06.03.17.42.26.612104@poczta.onet.pl>

On Thu, 03 Jun 2004 17:22:33 +0000, Walter Roberson wrote:

> In article <pan.2004.06.03.17.17.48.853019@poczta.onet.pl>,
> Wojtek Michalik <wojmichal@poczta.onet.pl> wrote:
> :Can anybody give me en example of working regex
> :that will match sample text given below:
> :____________
> :banana\n
> :\tapple\n
> :____________
> :\t=tab and \n=nweline 
> :I have been trying to match it by:
> :perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms 
> :nothing worked.
> 
> You haven't really defined what your pattern is.
> 
> I would ask, though, why you are using the g modifier? 

Because I am a perl newbie. Don't know much about it.    

>Is this all part
> of a larger string that there might be several such matches on? If so
> then what happens if there is a line  strawberry-banana  in there?

Can be, I will deal with it. The only -I hope -  problem I have, is newline inside my pattern

> You haven't anchored the banana portion.

Why is it so important ???

> I would also ask why you escaped the period and slash in your replacement
> pattern? In replacement patterns, period and slash stand for themselves
> [unless one of them happens to be the pattern delimeter.]

Often don't remember the obove - that's why.
 
> To confirm: the output you would expect over those lines would be
> 
> banana\n
> \t./apple\n
> 
> ??
------exactly----------



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

Date: 3 Jun 2004 09:44:17 -0700
From: constants@mix-net.net (Mark Constant)
Subject: Re: Why is this upload script not working
Message-Id: <ce43fdea.0406030844.53740e6f@posting.google.com>

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<2i68daFjf2oiU1@uni-berlin.de>...
> Mark Constant wrote:
> > Well on Unix my script now works without the -T switch on. The
> > error says that the file is still tainted. On a Windows Apache
> > Server it complains about Unaccetable file name and doesn't even
> > upload.
> 
> This simple upload script:
> 
> http://groups.google.com/groups?selm=bqpqmd%24262n27%241%40ID-184292.news.uni-berlin.de
> 
> works fine on Windows, and can be run with tainting enabled. Since it
> makes use of the upload() function instead of param() to grab the
> filehandle, it may also be more robust.

I used the script and it uploads the file but I don't want to give the
file the name of the starttime. I want it to be the original filename.
How would I do that?
#!c:/perl/bin/perl
use strict;
use warnings;
use CGI;
my $dir = 'c:/progra~1/apache~1/apache2/htdocs/quickbooks';
my $starttime = time;
my $q = new CGI;
my $fh = $q->upload('upfile');
$q->uploadInfo($fh)->{'Content-Disposition'} =~ /filename="([\w\-\.
]+)"/;
my $name = ($1 or $starttime);
open FILE, "> $dir/$name" or die $!;
binmode FILE;
print FILE $_ while <$fh>;
close FILE;

print "Content-type: text/html\n\n";
print "$name was uploaded.<br>";
print "It took ", time - $starttime, 

Also on my other script I get this error exactly. 
Cannot chdir to C:\Documents and Settings\Mark\Desktop\Trifold
Slide&Ride.doc:No such file or directory at C:/Program Files/Apache
Group/Apache2/htdocs/uploader.cgi line 22. Now I don't know why it
isn't talking about chdir when I only do that once through the whole
script. I feel like this script is close to finished but one or two
tiny things are wrong. If anybody has apache on Windows could they
test this code out. Thanks.

Now if go to line 22 in notepad it is this my $path = abs_path $file;

here is my script again
#!c:/perl/bin/perl 
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use strict;
use warnings;
use Cwd qw/cwd abs_path/;

my $q = CGI->new();
my $file = $q->param('upfile');
$file =~ /([\w.-]+)$/ or die "Unaccetable file name: $file";
my $filename = $1;
my $cdir = cwd;
chdir('C:/Program Files/Apache Group/Apache2/htdocs/quickbooks') or
die "Can't cd to quickbooks dir: $!";
my $dir = cwd;
my $path = abs_path $file;
my $notes = $q->param('notes');

print $q->header, $q->start_html('Uploading File');
print $q->h1('Upload Results');

if(!$file){
	print "Nothing Uploaded\n";
} else {
	print "Filename: $file<br />\n";
	print "Destination: $path<br />\n"; 
	print "Original CWD was: $cdir<br />\n";
	print "New CWD after chdir is: $dir<br />\n";
	my $ctype = $q->uploadInfo($file)->{'Content-Type'};
	print "MIME Type: $ctype<br />\n";
	open(OUTFILE, ">$path") or die "Can't create path $!";
	binmode(OUTFILE);
	my ($read, $buffer);
	print OUTFILE $buffer
		while $read = read $file, $buffer, 1024;
	defined $read or die "read failed: $!";
	close OUTFILE or die "Close of uploaded file failed: $!";
	close $file or die "Close of socked failed $!";
	print "File saved\n";
}

$q->end_html;


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

Date: Thu, 3 Jun 2004 17:12:44 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Why is this upload script not working
Message-Id: <c9nm6c$rhf$1@wisteria.csv.warwick.ac.uk>

Quoth constants@mix-net.net (Mark Constant):
> I used the script and it uploads the file but I don't want to give the
> file the name of the starttime. I want it to be the original filename.
> How would I do that?

Read the docs for upload and param. Upload returns a filehandle, param
returns the browser-specified name.

> #!c:/perl/bin/perl
> use strict;
> use warnings;
> use CGI;

use CGI::Carp             qw/fatalsToBrowser/;
use File::Spec::Functions qw/rel2abs/;
use File::Copy;

Some whitespace wouldn't hurt.

> my $dir = 'c:/progra~1/apache~1/apache2/htdocs/quickbooks';

You're still using ~1 names...

> my $starttime = time;
> my $q = new CGI;
> my $fh = $q->upload('upfile');
> $q->uploadInfo($fh)->{'Content-Disposition'} =~ /filename="([\w\-\.
> ]+)"/;
> my $name = ($1 or $starttime);

my $q = CGI->new;

my $UPLOAD = $q->upload('upfile');
my $upload = $q->param('upfile');
$upload =~ /([-\w.]+)$/ or die "unacceptable name: $upload";

> open FILE, "> $dir/$name" or die $!;
> binmode FILE;
> print FILE $_ while <$fh>;
> close FILE;

my $name = rel2abs $1, $dir;
copy $UPLOAD, $name
    or die qq/can't copy upload to "$name": $!/;

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

Since you're using CGI.pm, use its header method.

> print "$name was uploaded.<br>";

print qq/"$upload" was uploaded to "$name".<br>/;

or, better, use CGI's p method, which will escape the " for you.

> print "It took ", time - $starttime, 
> 
> Also on my other script I get this error exactly. 
> Cannot chdir to C:\Documents and Settings\Mark\Desktop\Trifold
> Slide&Ride.doc:No such file or directory at C:/Program Files/Apache
> Group/Apache2/htdocs/uploader.cgi line 22. Now I don't know why it
> isn't talking about chdir when I only do that once through the whole
> script. I feel like this script is close to finished but one or two
> tiny things are wrong. If anybody has apache on Windows could they
> test this code out. Thanks.
> 
> Now if go to line 22 in notepad it is this my $path = abs_path $file;

Cwd::abs_path (and Cwd::cwd) call chdir. 

Ben

-- 
   Although few may originate a policy, we are all able to judge it.
                                             - Pericles of Athens, c.430 B.C.
  ben@morrow.me.uk


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

Date: Thu, 03 Jun 2004 19:27:08 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Why is this upload script not working
Message-Id: <2i95qaFksqr0U1@uni-berlin.de>

Mark Constant wrote:
> Gunnar Hjalmarsson wrote:
>> 
>> http://groups.google.com/groups?selm=bqpqmd%24262n27%241%40ID-184292.news.uni-berlin.de
>> 
>> works fine on Windows, and can be run with tainting enabled.
>> Since it makes use of the upload() function instead of param() to
>> grab the filehandle, it may also be more robust.
> 
> I used the script and it uploads the file but I don't want to give
> the file the name of the starttime.

If the file was named with the start time, your browser probably grabs
the full path. (Btw, that may be the reason why your other script
failed on Windows.)

> I want it to be the original filename. How would I do that?

By modifying the regex. You could for instance replace

> $q->uploadInfo($fh)->{'Content-Disposition'} =~
>  /filename="([\w\-\. ]+)"/;
> my $name = ($1 or $starttime);

with

     $q->uploadInfo($fh)->{'Content-Disposition'} =~
      /filename=".*?([-\w. ]+)"/;
----------------^^^
     my $name = $1 or die "Couldn't grab filename $!";

Also: Replace

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

with

     print $q->header;

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: 3 Jun 2004 09:08:47 -0700
From: Petri <Petri_member@newsguy.com>
Subject: Re: Win32::OLE, WMI and executing methods
Message-Id: <c9nief02cnh@drn.newsguy.com>

In article <6e613a32.0406030603.23ce958b@posting.google.com>, Daniel Berger
says...
> A.S. Perl 5.8.3
> Windows XP Pro

> I'm familiar with how to get all InstancesOf() a WMI class
> (the only examples I could find), but I'm confused as to how to
> call methods on a class.  For example, I know how to iterate over
> a list of Services, but how do I call the StopService() method
> using Win32::OLE and WMI?

This short example seems to work fine:
#!/usr/bin/perl
# How to call a method:
#
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/calling_a_wmi_method.asp
# Return values from StopService():
#
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/stopservice_method_in_class_win32_service.asp

use strict;
use warnings;
use Win32::OLE qw(in with);

my $wmi = Win32::OLE->GetObject("winmgmts:");

my $serv_set = $wmi->InstancesOf("Win32_Service");
my $serv;
foreach $serv (in($serv_set)) {
    next unless ($serv->{'Name'} eq 'SETI'); # Find SETI service.
    if ($serv->{'State'} eq 'Running') { # If it's running, stop it.
	my $ok = $serv->StopService();
	print $serv->{'Name'} . ' has been succesfully stopped.' if (!$ok);
    } else { # If service is already stopped or in some other mysterious
	     # state, quit loop.
	last;
    }
}
__END__


Just follow the commented URLs for more info.
I leave error management as an exercise to the reader. :)

Hope this helps!


Petri



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

Date: Thu, 3 Jun 2004 16:51:24 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Win32::OLE, WMI and executing methods
Message-Id: <c9nkuc$ppt$3@wisteria.csv.warwick.ac.uk>


Quoth Petri <Petri_member@newsguy.com>:
> In article <6e613a32.0406030603.23ce958b@posting.google.com>, Daniel Berger
> says...
> > A.S. Perl 5.8.3
> > Windows XP Pro
> 
> > I'm familiar with how to get all InstancesOf() a WMI class
> > (the only examples I could find), but I'm confused as to how to
> > call methods on a class.  For example, I know how to iterate over
> > a list of Services, but how do I call the StopService() method
> > using Win32::OLE and WMI?
> 
> This short example seems to work fine:
> #!/usr/bin/perl
> # How to call a method:
> #
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/calling_a_wmi_method.asp
> # Return values from StopService():
> #
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/stopservice_method_in_class_win32_service.asp
> 
> use strict;
> use warnings;
> use Win32::OLE qw(in with);
> 
> my $wmi = Win32::OLE->GetObject("winmgmts:");
> 
> my $serv_set = $wmi->InstancesOf("Win32_Service");
> my $serv;
> foreach $serv (in($serv_set)) {
>     next unless ($serv->{'Name'} eq 'SETI'); # Find SETI service.
>     if ($serv->{'State'} eq 'Running') { # If it's running, stop it.
> 	my $ok = $serv->StopService();
> 	print $serv->{'Name'} . ' has been succesfully stopped.' if (!$ok);
>     } else { # If service is already stopped or in some other mysterious
> 	     # state, quit loop.
> 	last;
>     }
> }
> __END__

A bit more Perlish would be

my @srvs = grep { $_->{Name} eq 'SETI' } in $srv_set;
@srvs      or die "SETI service not installed"
@srvs == 1 or die "More than one SETI service installed";
my $srv = $srvs[0];

if ($srv->{State} eq 'Running') {
    my $err = $srv->StopService;
    $err and die "StopService failed for $srv->{Name}: $err";
    print "$srv->{Name} has been successfully stopped";
}

Ben

-- 
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
  ben@morrow.me.uk                                                 (Kate Rusby)


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 6648
***************************************


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