[23393] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5611 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 3 18:05:40 2003

Date: Fri, 3 Oct 2003 15:05:06 -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           Fri, 3 Oct 2003     Volume: 10 Number: 5611

Today's topics:
    Re: Debug Messages <sfandino@yahoo.com>
    Re: fastest count of instances in string? <uri@stemsystems.com>
    Re: How to generate this list? <krahnj@acm.org>
        multiple SQL line query via Perl (JohnnyQ)
        Opposite of regex match? <marv@stevens.com>
    Re: Opposite of regex match? <mpapec@yahoo.com>
    Re: Opposite of regex match? <marv@stevens.com>
    Re: Opposite of regex match? <krahnj@acm.org>
        problem writing file <x12code-del@del-yahoo.com>
    Re: problem writing file <mbinache4022@cedarcrest.net>
    Re: problem writing file <x12code-del@del-yahoo.com>
    Re: problem writing file <waltman@pobox.com>
        Trouble with throttling fork() (Jim)
    Re: Unexpected alteration of array's content <nobull@mail.com>
    Re: Unexplained Multiple Lauches of Script with NetBack (James Willmore)
    Re:  <bwalton@rochester.rr.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 03 Oct 2003 20:38:17 +0100
From: Salvador Fandino <sfandino@yahoo.com>
Subject: Re: Debug Messages
Message-Id: <blkj7f$g98$06$1@news.t-online.com>



Kasp wrote:

> "Abigail" <abigail@abigail.nl> wrote in message
> news:slrnbnipqq.e7b.abigail@alexandra.abigail.nl...
> 
> 
>>Given that you already know how to get command line arguments,
>>what exactly is your question? Surely your "dumb clients" can
>>type '--debug' as a parameter?
> 
> 
> Here is the scenario:
> I make a Perl script (test it) and send it to the clients. They use it.
> On fine day, shit happens - bad input, disk full..etc.
> At this stage, I want to give my client an option of running the script
> (again) with --debug option so that some trace messages can be logged into a
> log file.
> The client sends me this trace file and I try to make sense of it as to why
> the script is failing.
> 
> Now, in C/C++, we use the if(DEBUG) approach to log trace messages
> if --debug is specified as a command line arg.
> Is the approach in Perl any different or identical?
> Thanks.

I have a module on CPAN, ctflags, that allows to set constants from 
environment variables, it's a little obscure, but you don't need to know 
too much about it to use it, just add these lines at the beginning of 
your script:

   use ctflags::parse allow=>'', ns=>'debug', env=>'FOODEBUG';
   use ctflags package=>'dbg', prefix=>'', 'debug:*';

and the set of constants dbg::a dbg::b dbg::c ... dbg::z and dbg::A ... 
dbg::Z will become defined. You can use them to control debugging in 
this way:

   dbg::S and print "socket is ok\n";
   ...
   dbd::D and print "connected to the database\n";
   ...
   dbg::w and print "water in the CPU!\n";

To set the constants you use environment var. FOODEBUG from the shell, i.e.:

   $ FOODEBUG=DS myscript.pl

there is also a willcard to set all the constants to true '!'

   $ FOODEBUG='!' myscript.pl

or

   $ FOODEBUG='!D' myscript.pl

to set everything but dbg::D


Bye,

   - Salva





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

Date: Fri, 03 Oct 2003 18:32:05 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: fastest count of instances in string?
Message-Id: <x7pthemau3.fsf@mail.sysarch.com>

>>>>> "RJ" == Roy Johnson <rjohnson@shell.com> writes:

  RJ> AlV <skweek@no.spam> wrote in message news:<blk2v6$ub8$1@news-reader5.wanadoo.fr>...
  >> # time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA"; 
  >> $n=tr/$char/$char/; }'

  RJ> You'll get a smidge faster if you don't bother to replace anything.
  RJ> tr/// doesn't delete unless you specify the /d modifier, so you can
  RJ> just do
  RJ> $n=tr/$char//;
  RJ> and it is non-destructive.

first, you have the same problem that tr/// doesn't intrpolate.

secondly, replacing each char by itself is never destuctive. and your
notion that not using the replacement string is different then you need
to rtfm more carefully:

        If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated.
        This latter is useful for counting characters in a class or for
        squashing character sequences in a class.

so not passing in the replacement string is just syntactic sugar and
nothing special semantically.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Fri, 03 Oct 2003 19:17:02 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: How to generate this list?
Message-Id: <3F7DCAF2.7DF0A772@acm.org>

Roy Johnson wrote:
> 
> "John W. Krahn" <krahnj@acm.org> wrote in message news:<3F7B247C.586F33C@acm.org>...
> 
> > I don't know if this is the best way but:
> >
> > my @array = qw/ A A A A B B C D D D /;
> >
> > print "$_\n" for glob "{@{[ join ',', @array ]}}" x @array;
> 
> Would you explain the magic?

glob uses the comma separated list in braces as alternatives in the
result.

$ perl -le'print for glob "{AB,CD}XY"'
ABXY
CDXY
$ perl -le'print for glob "{A,B}{C,D}XY"'
ACXY
ADXY
BCXY
BDXY

So it passes a string of all the alternatives to glob which creates a
list of the alternatives.

print "$_\n" for glob
"{A,A,A,A,B,B,C,D,D,D}{A,A,A,A,B,B,C,D,D,D}{A,A,A,A,B,B,C,D,D,D}{A,A,A,A,B,B,C,D,D,D}{A,A,A,A,B,B,C,D,D,D}{A,A,A,A,B,B,C,D,D,D}{A,A,A,A,B,B,C,D,D,D}{A,A,A,A,B,B,C,D,D,D}{A,A,A,A,B,B,C,D,D,D}{A,A,A,A,B,B,C,D,D,D}";



John
-- 
use Perl;
program
fulfillment


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

Date: 3 Oct 2003 12:24:48 -0700
From: chicagojohnny1@yahoo.com (JohnnyQ)
Subject: multiple SQL line query via Perl
Message-Id: <8a1c0c84.0310031124.28e7862f@posting.google.com>

Hello:

I posted this in the comp.lang.perl, but I saw a thread that said it
was abandoned.  Sorry if this is a multiple post.

This is a question about formulating Sybase SQL queries in Perl.  

I can certainly do single command lines in perl using:

$dbh = DBI->connect("dbi:Sybase:server=$dbHost", $dbUser, $dbPass); 

if (!defined $dbh) { death ("Could not connect to database\n."); } 
else { print LOGFILE "Connected to database.\n"; }

$query = "use ${dbDatabase}";
$sth = $dbh->prepare(${query});
$sth->execute;

$query = "set rowcount 100000";
$sth = $dbh->prepare(${query});
$sth->execute;

However, as fas as I can tell Sybase needs to have variables in the
executable block that they are used.  So, I can't do something like:

$query = " declare @rowct int";
$sth = $dbh->prepare(${query});
$sth->execute;

$query = "select @rowct =1";
$sth = $dbh->prepare(${query});
$sth->execute;

Nor can I combine statements to do something like:

$query = "declare @rowct int\n select @rowct =1";
$sth = $dbh->prepare(${query});
$sth->execute;

So, my problem is that I have to find a way to group the following SQL
statements together and have them execute at once:

declare @rowct int
select @rowct = 1
while (@rowct > 0)
begin
delete Foo where creationDate < dateadd(day, -5, getdate())
select @rowct = @@rowcount
end

Thanks,

John


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

Date: Fri, 03 Oct 2003 20:11:43 GMT
From: Marv <marv@stevens.com>
Subject: Opposite of regex match?
Message-Id: <bnlrnvgc8l09einaaask1tosq216tot7r6@4ax.com>

Hello,

How would you write 'does not match' in a regex match?

Below is what I was assuming would work, but it doesn't. I guess you
cannot use "!=~" for does not match. What can you use here?

#!/usr/bin/perl


$var1="test";

$var2 = "testing";

if ($var2 !=~ m/$var1/){

        printf "Doesn't match\n";

} else {

        printf "Does match\n";

}

Thanks,
Max



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

Date: Fri, 03 Oct 2003 22:27:45 +0200
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: Opposite of regex match?
Message-Id: <6rmrnvknkljrscnda4bu2qormru2ea1hjd@4ax.com>

X-Ftn-To: Marv 

Marv <marv@stevens.com> wrote:
>How would you write 'does not match' in a regex match?
>
>Below is what I was assuming would work, but it doesn't. I guess you
>cannot use "!=~" for does not match. What can you use here?

You were close,
!~


-- 
Matija


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

Date: Fri, 03 Oct 2003 20:54:28 GMT
From: Marv <marv@stevens.com>
Subject: Re: Opposite of regex match?
Message-Id: <ehornvg2up2k3i5t079ehgj6n3s4cp9002@4ax.com>

On Fri, 03 Oct 2003 22:27:45 +0200, Matija Papec <mpapec@yahoo.com>
wrote:

>X-Ftn-To: Marv 
>
>Marv <marv@stevens.com> wrote:
>>How would you write 'does not match' in a regex match?
>>
>>Below is what I was assuming would work, but it doesn't. I guess you
>>cannot use "!=~" for does not match. What can you use here?
>
>You were close,
>!~

Thanks! That did it. 

I was pulling my hair out on that one. Couldn't find the answer
anywhere.

Max


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

Date: Fri, 03 Oct 2003 21:31:29 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Opposite of regex match?
Message-Id: <3F7DEA74.2373176B@acm.org>

Marv wrote:
> 
> On Fri, 03 Oct 2003 22:27:45 +0200, Matija Papec <mpapec@yahoo.com>
> wrote:
> >
> >Marv <marv@stevens.com> wrote:
> >>How would you write 'does not match' in a regex match?
> >>
> >>Below is what I was assuming would work, but it doesn't. I guess you
> >>cannot use "!=~" for does not match. What can you use here?
> >
> >You were close,
> >!~
> 
> I was pulling my hair out on that one. Couldn't find the answer
> anywhere.

perldoc perlop

perlop.pod has a description of all of perl's operators.


John
-- 
use Perl;
program
fulfillment


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

Date: Fri, 03 Oct 2003 13:32:09 -0500
From: David McDivitt <x12code-del@del-yahoo.com>
Subject: problem writing file
Message-Id: <3ndrnvs9eno4ber9rsjelessjn10vjrl3d@4ax.com>

I need help writing a file if someone would oblige. An image is retrieved
from a database, a file opened, and one of the fields written to the file.
When writing normally, extra bytes are added to the data and the image is
not interpretable. Probably Perl is converting control characters. I tried
using sysopen and syswrite so the data would be written as is, but cannot
get the syntax right. The code is pasted here. Thanks

use Fcntl qw( O_WRONLY O_CREAT ); #placed at top of program

sysopen JPGFILE, '>'.$$cfg{'ServerSaveImagePath'}.$jpg, (O_CREAT | O_WRONLY)
or problem('Cannot create image file');

syswrite JPGFILE, $field[0];

#open JPGFILE, '>'.$$cfg{'ServerSaveImagePath'}.$jpg or problem('Cannot
create image file');

#print JPGFILE $field[0] or problem('Cannot write image file');
close JPGFILE;



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

Date: Fri, 3 Oct 2003 14:09:50 -0500
From: "Mike Banache" <mbinache4022@cedarcrest.net>
Subject: Re: problem writing file
Message-Id: <vnribpap2ro37d@corp.supernews.com>

Would binmode(JPGFILE) work in this case?
My newbie 2 cents.

"David McDivitt" <x12code-del@del-yahoo.com> wrote in message
news:3ndrnvs9eno4ber9rsjelessjn10vjrl3d@4ax.com...
> I need help writing a file if someone would oblige. An image is retrieved
> from a database, a file opened, and one of the fields written to the file.
> When writing normally, extra bytes are added to the data and the image is
> not interpretable. Probably Perl is converting control characters. I tried
> using sysopen and syswrite so the data would be written as is, but cannot
> get the syntax right. The code is pasted here. Thanks
>
> use Fcntl qw( O_WRONLY O_CREAT ); #placed at top of program
>
> sysopen JPGFILE, '>'.$$cfg{'ServerSaveImagePath'}.$jpg, (O_CREAT |
O_WRONLY)
> or problem('Cannot create image file');
>
> syswrite JPGFILE, $field[0];
>
> #open JPGFILE, '>'.$$cfg{'ServerSaveImagePath'}.$jpg or problem('Cannot
> create image file');
>
> #print JPGFILE $field[0] or problem('Cannot write image file');
> close JPGFILE;
>




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

Date: Fri, 03 Oct 2003 14:44:55 -0500
From: David McDivitt <x12code-del@del-yahoo.com>
Subject: Re: problem writing file
Message-Id: <lckrnvckmhbvepponmfr961t0drd06nu1p@4ax.com>

>Subject: Re: problem writing file
>Date: Fri, 3 Oct 2003 14:09:50 -0500
>
>Would binmode(JPGFILE) work in this case?
>My newbie 2 cents.
>
>"David McDivitt" <x12code-del@del-yahoo.com> wrote in message
>news:3ndrnvs9eno4ber9rsjelessjn10vjrl3d@4ax.com...
>> I need help writing a file if someone would oblige. An image is retrieved
>> from a database, a file opened, and one of the fields written to the file.
>> When writing normally, extra bytes are added to the data and the image is
>> not interpretable. Probably Perl is converting control characters. I tried
>> using sysopen and syswrite so the data would be written as is, but cannot
>> get the syntax right. The code is pasted here. Thanks
>>
>> use Fcntl qw( O_WRONLY O_CREAT ); #placed at top of program
>>
>> sysopen JPGFILE, '>'.$$cfg{'ServerSaveImagePath'}.$jpg, (O_CREAT |
>O_WRONLY)
>> or problem('Cannot create image file');
>>
>> syswrite JPGFILE, $field[0];
>>
>> #open JPGFILE, '>'.$$cfg{'ServerSaveImagePath'}.$jpg or problem('Cannot
>> create image file');
>>
>> #print JPGFILE $field[0] or problem('Cannot write image file');
>> close JPGFILE;

Yes, binmode did it. Thanks



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

Date: Fri, 3 Oct 2003 15:57:03 -0400
From: Walt Mankowski <waltman@pobox.com>
Subject: Re: problem writing file
Message-Id: <slrnbnrl4f.9dj.waltman@waltman.dnsalias.org>

In article <3ndrnvs9eno4ber9rsjelessjn10vjrl3d@4ax.com>, David McDivitt wrote:
> I need help writing a file if someone would oblige. An image is retrieved
> from a database, a file opened, and one of the fields written to the file.
> When writing normally, extra bytes are added to the data and the image is
> not interpretable. Probably Perl is converting control characters. I tried
> using sysopen and syswrite so the data would be written as is, but cannot
> get the syntax right. The code is pasted here. Thanks
> 
> use Fcntl qw( O_WRONLY O_CREAT ); #placed at top of program
> 
> sysopen JPGFILE, '>'.$$cfg{'ServerSaveImagePath'}.$jpg, (O_CREAT | O_WRONLY)
> or problem('Cannot create image file');
> 
> syswrite JPGFILE, $field[0];
> 
> #open JPGFILE, '>'.$$cfg{'ServerSaveImagePath'}.$jpg or problem('Cannot
> create image file');
> 
> #print JPGFILE $field[0] or problem('Cannot write image file');
> close JPGFILE;

Are you on a win32 box?  If so, try calling binmode on the file handle
before writing to it.

Walt


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

Date: 3 Oct 2003 13:06:28 -0700
From: jimnl69@hotmail.com (Jim)
Subject: Trouble with throttling fork()
Message-Id: <3966ee66.0310031206.57020b01@posting.google.com>

I'm having a hard time using fork() and throttling it.  Perhaps it is
because I am using the fork() emulation in Activestate Perl for
Windows. Basically, what I want to do is loop through an array and run
a process on each element.  I need to fork each process but the array
is hundreds of elements big and I can only run 8 processes at a time.

my $parent = $$;
$StartProc = 1;
$TotProcs = 8;

# Trap signals from child processes and run this subroutine.
$SIG{CHLD} = \&ChldStopped;

foreach $x (@array) {
  if ($parent == $$){ # this is parent process
    while ($StartProc > $TotProcs) { # limit to 8 at a time
      sleep 1;
    }
  $StartProc ++;
  my $child = fork();
  if ($child == 0) { # This is a child process.
    # run process on $x
    exit 1;
  }
}

sub ChldStopped {
	$StartProc --;
}


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

Date: 03 Oct 2003 19:26:52 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Unexpected alteration of array's content
Message-Id: <u9oewydvo3.fsf@wcl-l.bham.ac.uk>

rjohnson@shell.com (Roy Johnson) writes:

> Matija Papec <mpapec@yahoo.com> wrote in message news:<hdronv01mtn2q3gke24vakffnf8j5i88h9@4ax.com>...
> > Brian McCauley <nobull@mail.com> wrote:
> > >> for my $line (map $_, @lines){
> > >
> > >Personally I'm supprised that works.  
> 
> What's surprising about it? map() generates a new array, 

No, map returns a _list_ not an _array_.  It is quite possible for a
function that returns a list to return a list of lvalues.

If I do:

  for my $line (grep 1, @lines){

Then @lines _is_ still modified.

The reason it works with map but not grep is that the expression in
map is evaluated as an rvalue rather than an lvalue.  This is
surprising since expressions in Perl are usually only converted to
rvalues at the last possible moment.

> It would be equivalent to say
> 
> for my $line (my @copy = @array) {
> 
> except, of course, that this names the copy. If you want to avoid new
> names, you can say

No, that creates an array not just a list.
 
> for my $line (my @array = @array) {

> which might cause people to blink.

Yes, indeed.  Much better to use an anonymous array.

> I can tell you like code that makes people blink. :-)

No I don't.

> > >But even though it does [...] I still prefer:
> > >
> > >  for my $line (@{[@lines]}){
> 
> This also generates a new copy of the entire array (into an anonymous
> ref, which is then dereferenced).

Yes - I know what it does.

> It has a somewhat higher blink factor than the earlier examples.

Not once you get used to it (I certainly blinked at the map example).

> But why not just copy the element you're playing with? Save a little
> memory.
> 
> for (@lines) {
>   my $line = $_;

Yes I was going to post exactly this in answer to the OP and had even
got as far as typing a follow-up but then decided it was too obvious
to mention and I didn't hit send.

> If you want to use $_, you'll need to make it local, rather than my:
> 
> for (@array) {
>     local $_ = $_;
>     $_++;
> print "Affected $_\n";
> }
> print join('', @array), "\n";

By rights that should modify each of the elements of @array in turn
but put them back afterwards.  That's not what it does in current
versions of Perl but maybe one day it will.

If @array is tied really odd (bad) things happen.  Again maybe this
will be fixed one day.  If it is fixed then you can probably expect
the behavior for untied @array to change too.

Please see other current thread about why local $_ can do strange
things.

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


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

Date: 3 Oct 2003 14:46:11 -0700
From: jwillmore@cyberia.com (James Willmore)
Subject: Re: Unexplained Multiple Lauches of Script with NetBackup ... could it be PERL?
Message-Id: <e0160815.0310031346.646cde58@posting.google.com>

"Jay W" <jandk1@nospanners.comcast.net> wrote in message news:<8_2cnfo5-731-uCiXTWJig@comcast.com>...
> I have a PERL script that is executed by NetBackup "bpstart". From within
> the script two different lines are launched depending on the Policy passed
> to bpstart in %2. Only one of the two lines should be launched at any given
> time.

What is "bpstart" supposed to do?  What does it have to do with Perl?

> 
> The problem is that shortly after launching the script (about 5 minutes), a
> second occurrance starts, then a 3rd, then a 4th .... up to 29 occurances in
> some cases. Usually this scenario ends with a Dr Watson abort. The odd thing
> is that I use about 7 command line parameters that are identical in each
> launch, so a full command line is being "re-executed"  :
<snip>

Is it a Perl script that is executed and then re-executed?  I guess
we're back to the first question - what is "bpstart" supposed to do?
and What does it have to do with Perl?

> 
> I guess, the questions are :
> 
> 1. Has anyone else run into this?

No.  Right now where I work we use Syncsort's Backup Express, but will
be using a Veritas product in the future.  I'm drawing upon my
experience with Backup Express on this, but ... is "bpstart" supposed
to start a backup?  If so, is your Perl script a pre-backup script
that's supposed to run?  Maybe take down a database or kill a process?
 If so, then it may be "bpstart that's starting these numerious
instances of Perl.  Again, this is a guess.

> 2. Is there a syntax that may inadvertantly start another instance of the
> command that was originally launched? (I'm 1st year PERL)

Usless you have a 'system' call to start another script or are using
threads in some strange fashion, no.  The invocation of a Perl script
will _not_ cause another invocation of the same script unless you tell
the script to do so.  However, if this "bpstart" application is
supposed to start numerious times, then it may be starting numerious
instances of the script.  Again ... answer the first two questions to
shed more light on this.

> 
> I'm trying to prove through debug code that it is NetBackup's fault and not
> PERL
<snip>

Just some suggestions ....
1) depending upon what your script is trying to do, you could put in
some 'alarm's to kill the script after a certain period of time.
2) use some type of lock file scheme to prevent and detect the script
being started a second time.  The LockFile::Simple module _may_ be
able to aid in this.
3) put in some logging code -or- use a logging module to see what the
script is doing, when it's doing it, etc.  Log::Dispatch and other
modules may fit the bill for your purposes.

You need to provide some _more_ information than what you did.  What
you provided was not very specific except that there is an issue and
you're not sure what it is.  Some code would help, as well as
answering some of the questions earlier in the post.

HTH

Jim


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

Date: Sat, 19 Jul 2003 01:59:56 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: 
Message-Id: <3F18A600.3040306@rochester.rr.com>

Ron wrote:

> Tried this code get a server 500 error.
> 
> Anyone know what's wrong with it?
> 
> if $DayName eq "Select a Day" or $RouteName eq "Select A Route") {

(---^


>     dienice("Please use the back button on your browser to fill out the Day
> & Route fields.");
> }
 ...
> Ron

 ...
-- 
Bob Walton



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

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


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