Re: Changing the String of the file at one shot.

2005-03-08 Thread David Storrs
On Tue, Mar 08, 2005 at 12:02:09PM +0100, John Doe wrote:
> Am Dienstag, 8. März 2005 02.04 schrieb David Storrs:
> > > >Suneel Kumar B <mailto:[EMAIL PROTECTED]> wrote:
> > > >: Can i
> > > >: have an option by which i could open a file for both read and
> > > >: write simultaneously

> > The literal answer to your question would be this:
> >
> >open FILE, '/path/to/the/file', '+<'
> >   or die "Couldn't open: $!";
> 
> open FILE, '+<', '/path/to/the/file'  or die "Couldn't open: $!";
> 
> (see order of arguments)
> 
> perldoc -f open

Sorry, yes.  I had originally used the 'IO::File' version there, for
which that is the correct order.  When I changed it to the bare open
call, I forgot to swap them.  This difference in the order is
something that has tripped me a lot.

> > or, preferably (as it will manage some safety issues for you):
> >
> >use IO::File;
> >my $file = new IO::File('/path/to/the/file', '+<')
> >   or die "Couldn't open: $!";


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Changing the String of the file at one shot.

2005-03-08 Thread David Storrs
On Mon, Mar 07, 2005 at 11:00:49AM -0500, Wiggins d'Anconia wrote:
> Charles K. Clarkson wrote:
> >Suneel Kumar B  wrote:
> >
> >: I have a situation where in, iam updating a file by replacing few
> >: strings in some lines with another string. While doing this, Can i
> >: have an option by which i could open a file for both read and
> >: write simultaneously, so that i could search and replace the
> >: string straight away..??
> >
> >Sounds like you need in place editing. Check out the -i switch
> >in the 'perlrun' documentation for an example.
> >
> >Charles K. Clarkson
> 
> or Tie::File,
> 
> perldoc Tie::File
> 
> http://danconia.org

The literal answer to your question would be this:

   open FILE, '/path/to/the/file', '+<'
  or die "Couldn't open: $!";

or, preferably (as it will manage some safety issues for you):

   use IO::File;
   my $file = new IO::File('/path/to/the/file', '+<') 
  or die "Couldn't open: $!";

This isn't a great solution for updating text files, however, since
they have variable-length records (e.g., you overwrite a 10-character
sentence with 12 characters and you have just stomped on the next 2
characters).

One of the above solutions (-i or Tie::File) will stand you in better
stead.

--Dks

-- 
[EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Appending constant string to variable inside a here-document

2005-03-08 Thread David Storrs
On Sun, Mar 06, 2005 at 11:21:23PM -0600, Charles K. Clarkson wrote:
> Harold Castro  wrote:

> : if the current content of $word is the word "big", and
> : I would want to print a here document appending
> : something to "big" like "foot":
> : 
> : print < : 
> : $word."foot" -> won't work, display it as is.
> : "$word"."foot"   -> neither this one
> : $wordfoot-> error. it becomes a single
> :  variable named $wordfoot
> : 
> : EOF
> 
> my $word = 'big';
> 
> print < 
> ${word}foot -> Works Fine.
> 
> EOF
> 
> Charles K. Clarkson

Harold,

It looks like Charles already answered your question, but I wanted to
offer a couple of other thoughts that you might find useful.


1) A here-doc is really just a long string.  It can be either single-
   or double-quoted, but it defaults to double-quoted, like so:
  
  print <<'SINGLE';  # single quoted
  print <<"DOUBLE";  # double quoted
  print <.  The reference to this array is then dereferenced
   into an actual array, which (just like any other array variable)
   will be interpolated into the double-quoted string that is your
   heredoc.  Whew!

   Note that you can replace C with any arbitrary Perl
   code and it will work.  So, your C< $word . "foot" > from above
   would have worked if you had done it this way...but the way that
   Charles suggested is /much/ better.

Hope this helps,

--Dks


  

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Count the occurence of a character

2004-07-24 Thread David Storrs

On Fri, Jul 23, 2004 at 12:13:48PM +0200, Paul Johnson wrote:
> On Fri, Jul 23, 2004 at 03:12:22PM +0530, Jaffer Shaik wrote:
> 
> > I have the below strig
> > 
> > $str = "abckdweqadidkisdikda";
> > 
> > In the above string, I want to count the occurrences of character 'a',
> > i.e I should get count of a = 7.
> > 
> > How can i achieve this in Perl.
> 
> $count = $str =~ tr/a//;

Note that this modifiees the string.  You might want to do 

 $count = $str =~ tr/a/a/;

instead.

--Dks


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Elegant quoted word parsing

2004-06-12 Thread David Storrs
On Fri, Jun 11, 2004 at 10:08:23AM -0500, James Edward Gray II wrote:
> On Jun 10, 2004, at 9:46 PM, Beau E. Cox wrote:
> 
> >Hi -
> >
> >I am trying to come up with a simple, elegant word parsing script, 
> >that:
> >
> >* takes a scalar string, and
> >* splits it into words separating on white space, commas,
> >  and a set of delimiters: "" '' // () {} [] ##, and
> >* returns the array of words.

Personally, I would do this:

#!/usr/bin/perl
use Text::ParseWords;

my $delims = qr!["'{}[\]#\s]!;
my $line = 'This is#a#test\'of\'the"quotewords"function{with}separators[of
many]kinds';

my @words = "ewords($delims, 0, $line);
$i = 0;
for (@words) {
print "$i: <$_>\n";
$i++;
}


Which outputs the following:


0: 
1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
10: 
11: 
12: 

Note that, since you are splitting on ', contractions and possessives
will be mangled.

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Very wierd problem with CGI.pm

2004-06-12 Thread David Storrs
On Fri, Jun 11, 2004 at 01:07:54PM +0100, mike wrote:
> Has anyone ever come accross this wied problem before
> 
> I have a script which pulls records from a DB then loops into a form and
> shows each record, with the id no being concatenated to the field name
> to give a unique record id.
> 
> However if I have this
> "Banked",textfield(-name=>'bk_amt'.$bank_inv,-value=>$bk_amt1);
> 
> the params are zeroed
> 
> but if I have this
> 
> "Banked",textfield(-name=>' bk_amt'.$bank_inv,-value=>$bk_amt1);
>  
> (ie; a space)
> everything is fine
> 
> Anyone have any idea what is going on here?


Can't really tell without seeing your form but my first guess would be
that the HTML for the textfield looks something like this:



Meaning that its name actually IS ' bk_amt' and not 'bk_amt'.
Therefore, when you try to access the field name without a space,
there is no such field, so you get nothing.

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: returning problem of regexp

2004-06-12 Thread David Storrs

On Fri, Jun 11, 2004 at 09:10:48AM +0200, Graf Laszlo wrote:
> Hi Ziggy
> 
> You got me wrong. Anyway thanks for example.
> Take a look here:
> 
> #!/usr/bin/perl
> @str = ();
> push @str, "\n";
> push @str, "\n";
> push @str, "BBB\n";
> push @str, "\n";
> push @str, "\n";#<- watch this line
> push @str, "\n";
> push @str, "CCC1\n";
> push @str, "\n";
> push @str, "\n";
> push @str, "CCC2\n";
> push @str, "\n";
> push @str, "\n";   #<- and this line
> push @str, "\n";#<- and this line
> 
> my $i = 1;
> for (@str) {
>   if ( /^(.*?)()(.*?)$/ .. /^(.*?)(<\/sm:.+?>)/ ) {
>   print "$_";
>   }
>   $i++;
> }
> 
> wich returns these lines:
> 
> 
> 
> BBB
> 
>   <- the first watched line is present
> 
> CCC1
> 
> 
> CCC2
> 
>  <- the last two are missing from the end
> 
> What is wrong in regexp ?
> 
> Graf  Laszlo

Hi Graf,

You've got an interesting and fairly subtle problem here.  The problem
is not actually with your regex, it's with the .. operator that you
are using to control your 'if' statement.  First, read this:
http://perldoc.com/perl5.8.4/pod/perlop.html
(Search for:   In scalar context, ".." returns a boolean value.)

So, let's walk through it (what I am showing here is slightly
inaccurate, as I detail below, but it's easier to understand this
way): 

push @str, "\n";  # Left side turns on; .. == true
ush @str, "\n";   # Still true
push @str, "BBB\n"; # Still true
push @str, "\n"; # **) Right side fires; .. == false
push @str, "\n"; # Left side turns on; .. == true
push @str, "\n"; # Still true
push @str, "CCC1\n";# Still true
push @str, "\n"; # **) Right side fires; .. == false
push @str, "\n"; # Left side turns on; .. == true
push @str, "CCC2\n";# Still true
push @str, "\n"; # **) Right side fires; .. == false
push @str, "\n";# Still false
push @str, "\n"; # Still false

Ok, so you see why anywhere that .. is true, that line will be
printed and when it is false, that line will not be printed.  That
should explain everything except the three lines marked **--which, not
coincidentally, are the lines where the right side of the .. fired.  

Here's the reason (quoted from perldoc "It [the .. operator] doesn't
become false till the next time the range operator is evaluated."  So,
basically, when the right side initially fires, the range operator
returns TRUE, but then changes its state to FALSE immediately
thereafter (which is the slight inaccuracy I mentioned above; the
change doesn't happen on the marked lines, it happens on the lines
following). 


Hope that makes sense.

--Dks


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: vi editor

2004-06-07 Thread David Storrs
On Sat, Jun 05, 2004 at 04:34:00PM +0100, aditi gupta wrote:
> hi,
>  
> vi is used only in unix and linux.which editor can be used in windows and is better 
> than notepad.
>  
> thanx,
>  
> aditi

I'm extremely pleased with Textpad (http://www.textpad.com).  Fast
start time, easy to use, highly customizable, powerful macros and
regex abilities, nicely organized menu structures.

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: searching a code for performing blast n giving output in a particular format.

2004-06-06 Thread David Storrs

On Fri, Jun 04, 2004 at 03:15:28PM -, aditi wrote:
> hi, 
> 
> i have to blast genes of human Y chromosome(104 genes)with genomes of non 
> human species to find which genes are conserved on Y and on non-Y 
> chromosomes in other species.and for each gene,its chromosome its 5' and 
> 3'end location w.r.t. human Y gene and similarity percentage is also 
> required.
> if done manually,it would amount for over 3000 blasts.it would be of great 
> help if anyone could tell me the link to a perl script or similar script , 
> if available.
> 
> thanking you,
> 
> aditi


Just for the record...I think I understood all of those words, but I
have no idea what you asked for.  It sounds like your question is
"could someone please write a script that does this"? If that is the
case, I'm afraid it's a bit out of scope for this list--at least, if
you want it done for free.  I'm sure many of us are contractors (I
certainly am) and would be willing to be hired to do it.  But we can't
do it for free.

If you have questions that are more specifically Perl related, we'd be
glad to answer those for you.

--Dks
[EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: how to use perl in windows

2004-06-06 Thread David Storrs

On Sat, Jun 05, 2004 at 02:38:06PM +0100, aditi gupta wrote:
> hi,
>  
> for writing perl programs,notepad is used.But what should be done to execute the 
> programs?

What I generally do is to open a DOS window and type 'perl '.  So, if I want to run foo.pl, I type:   perl foo.pl


> Also,how can i take input from online databases?like,if i have to get a 
> gene sequence from Genbank,what command should i write in program?

Read up on the DBI (Perl's DataBase Interface layer):

Here's a good place to start:
http://www.perl.com/pub/a/1999/10/DBI.html

You can install it from CPAN, along with all the DBD modules (see
above tutorial for what the DBD modules are).

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: removing element of array

2004-05-16 Thread David Storrs
On Fri, May 14, 2004 at 10:35:45PM -0500, Andrew Gaffney wrote:
> Charles K. Clarkson wrote:
> >Andrew Gaffney <[EMAIL PROTECTED]> wrote:
> >: 
> >: This doesn't quite work because I'm removing elements
> >: from the array I'm looping through. What would be the
> >: best way to take care of this?
> >
> >Don't use splice::)
> >
> >print Dumper $array1;
> >
> >@$array1 = grep ! /^!/, @$array1;
> >
> >print Dumper $array1;

Note that using grep constructs a whole new array and is potentially
very slow if working with large arrays--especially if the array is
tied to (e.g.) a database.

Which is not to say you _shouldn't_ use grep...just make sure you know
something about the size of your data.  I use a 'for' loop below,
which will have the same problem.

> Well, I'm working with the data in the array at the same time I'm trying to 
> remove certain elements. I need to process the elements that start with a 
> '!' and then remove them. I process all other elements differently.

This should do what you want.  It also does it in one pass, and
without counting backward or any other fancy tricks.


#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $a = [qw(foo bar baz !jaz frob quux)];

print Dumper $a;

my $i = 0;
for (@$a) { 
unless ( /^!/ ) {
process_normal_item($_);
$i++;
} else {
process_negated_item($_);
splice @$a, $i, 1;
$_ = $a->[$i];
redo;
}
}

print Dumper $a;

#  These are only here to provide you with convenient hooks for
#  expanding this into your own code.
#
sub process_normal_item { print shift; }
sub process_negated_item { print shift; }

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: How to load environment ?

2004-05-16 Thread David Storrs
On Fri, May 14, 2004 at 07:59:40PM -0500, max wrote:
[...]
> So i use another bash script to load the environment and run the perl script 
> from one line in the crontab, the content of the load and run script is this:
> 
> ---
> !/bin/sh
> . ~max/tmp/g_amb.sh   # <-- This Loads the environment.
> /home/max/tmp/l_amb.pl  # <-- This uses the environment loaded.
> ---
> 
> How could i load the environment from the perl script without using the load 
> and run script ?
> 
> Some perl or bash scripts are going to use the environment variables and i 
> dont want to define the variables on every script, 


Max,

It sounds like you have a perfectly good solution.  Quite honestly, I
would stick with it.  Here's why:

First off, you've got two factors working together here:

  1) Multiple Perl scripts must be able to see these variables.

  2) At the time the Perl scripts are run, the environment is not
  normally available.

#1 says that you need to centralize the variables somewhere so that
multiple scripts can load them up.  

#2 says that you are going to need to take some explicit action to
instantiate the variables.



If you don't like that, then something like the following might work
(not tested):

-- File MyEnv.pm in @INC
package MyEnv;
use Env;

$ENV{PVAR} = 'p var';
$ENV{XVAR} = 'x var';
-- End 


-- Begin script file:
#!/usr/bin/perl 

use MyEnv;
-- End 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: lowercase

2003-10-26 Thread David Storrs
Or you can even do it without the parens:

  $stri = lc $stri;

--Dks

On Fri, Oct 24, 2003 at 09:26:40PM -0700, [EMAIL PROTECTED] wrote:
> or you can do $stri=lc($stri);
> 
> -rkl
> 
> > try this:
> > $stri =~ tr/A-Z/a-z/;
> >
> > yi
> >
> > --- Andre Chaves Mascarenhas <[EMAIL PROTECTED]> wrote:
> >> Hi if i have an string lets say
> >> $stri="Joana Prado";
> >> How do i transformm it into
> >> "joana prado"
> >> (all lowercase)
> >> Thanks
> >>
> >
> >
> > __
> > Do you Yahoo!?
> > The New Yahoo! Shopping - with improved product search
> > http://shopping.yahoo.com
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> 
> 
> 
> -
> eMail solutions by 
> http://www.swanmail.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Passing and returning values from another script?

2003-10-26 Thread David Storrs
On Fri, Oct 24, 2003 at 09:47:14AM -0800, Mark Weisman wrote:
> I'm not sure, however I put the script mods in that you suggested, and
> my $output is the word "SCRIPT", I would like clarification on line 2 if
> you would please?
> >my $output = join("", SCRIPT);

> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> > open (SCRIPT, "./scriptname.pl password|");
> > my $output = join("", SCRIPT);
> > close SCRIPT

Actually, I think you just need to put angle branckets around the
SCRIPT, like so:

my $output = join("", 

Re: Parsing pipe delimited file

2003-10-25 Thread David Storrs
On Wed, Oct 22, 2003 at 07:22:24PM +0200, Kevin Pfeiffer wrote:
> In article <[EMAIL PROTECTED]>, Charles K. Clarkson
> wrote:
> 
> > Kevin Pfeiffer <[EMAIL PROTECTED]> wrote:
> > 
> > : Where I am stuck is on the question:
> > : 
> > : Given an @array such as
> > : ("Title of Song", "Artist", "Title", "Another Artist", "etc"),
> > : is there an easy way to strip out the quotation marks.
> > 
> > s/"//g foreach @array;
> 
> Ahhh, that's so nice. I should have known better and just tried that. I'm
> guessing that in such a format internal quote marks would have to be
> escaped? So I could do:
> 
> s/[^\]"//g...   ?


Kevin, 

I don't think that's what you want.  That will run through your string
and, everywhere that it sees a " it will strip the quote and the
previous character unless the previous character is a \, in which case
it won't do anything.  So, yes, it preserves escaped "s, but it
accidentally nukes other characters.  (Also, you need to escape the
\.)

I think what you want is a negative look-behind:

s/(?http://perldoc.com/perl5.8.0/pod/perlre.html for details on
negative (and positive) look-behinds and -aheads.  They are very useful.


--Dks


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Do BEGIN blocks and END blocks have priority?

2003-09-28 Thread David Storrs
On Fri, Sep 26, 2003 at 11:04:21AM -0400, Dan Anderson wrote:
> > BEGIN blocks do not take precedence over one another--they are all
> > still executed.  They are, however, executed immediately after perl
> > finishes compiling them.  So, if you have the following code:
> 
> Ok, so I'm guessing that the reason that 3214 is displayed is not
> because of precedence, but that every begin block is pushed into a stack
> of some kind, and when perl starts executing code, code on the top of
> the stack ends up being run first.  Is that right?
> 
> So it's sort of like precedence but really isn't.  Which explains when
> things printed off are printed off from innermost begin to outermost
> begin, but a subroutine declared in a begin nested in other begins
> creates an error when called before the sequence in the code.
> 
> Correct?


Close; it's not precedence, it's just order of execution.  Much like
in math, you do multiplacation before addition--here, you do BEGIN
blocks as soon as possible, then you do "normal" code.  Let me clarify
my earlier example, and maybe that will help.

1 { # (Block Outer)
2print "A\n"; # Will be executed at runtime (since not in BEGIN block)
3foo(); # Also a run-time execution; Not defined yet, but that's ok
4
5BEGIN {  # (Block 1)
6print "B\n";  # Not finished compiling block yet, so not printed
7BEGIN {  # (Block 2)
8print "C\n"; # Not finished compiling block yet, so not printed
9sub foo { print "D\n"; }
10   }
 # Finished compiling block 2, so print C
11   }
 # Finished compiling block 1, so print B
12 }
RUNTIME BEGINS HERE: print A, then call foo() which prints D

Another way to say that: When perl goes to compile/execute this, it
does so in the following order (pseudocode):


open block Outer;
compile line 2, to be executed at runtime
compile line 3, to be executed at runtime
open block 1;
compile line 6, to be executed as soon as Block 1 finishes compilation
open block 2;
compile line 6, to be executed as soon as Block 2 finishes compilation
compile line 7, to be executed as soon as Block 1 finishes compilation
compile line 8, to be executed when called
close block 2
execute line 8 -> C
close block 1
execute line 6 -> B
BEGIN RUNTIME
execute line 2 -> A
execute line 3 (call line 'foo()' on line 9)   -> D
close block Outer;



Does that help?

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to get the file name from a path

2003-09-25 Thread David Storrs
On Thu, Sep 25, 2003 at 05:15:56PM +0300, Juris wrote:
> On Thu, 25 Sep 2003 12:10:15 +0530, Chetak Sasalu M 
> <[EMAIL PROTECTED]> wrote:
> 
> >
> >Hi,
> >How can I extract "filename" from /root/dir1/.../filename

> $path_name='/root/dir1/.../filename';
> my $fname=substr($path, 
> rindex($path,"/")+1,length($path)-rindex($path,"/")- 1);


use File::Basename;
my $fname = basename($path);


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Do BEGIN blocks and END blocks have priority?

2003-09-25 Thread David Storrs
On Thu, Sep 25, 2003 at 09:12:48PM -0400, Dan Anderson wrote:
> If I create code with:
> 
> BEGIN
> {
># something
>BEGIN
>{
>  # something else
>}
> }
> 
> Will the inner BEGIN block take precedence over the outer one, and thus
> load any subroutines or whatever is in the inner begin block? 

BEGIN blocks do not take precedence over one another--they are all
still executed.  They are, however, executed immediately after perl
finishes compiling them.  So, if you have the following code:

{
   print "1\n";
   BEGIN { 
   print "2\n";
   BEGIN { 
   print "3\n";
   }
   }
   print "4\n";
}

The output will be:

3
2
1
4

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How do I query whether a toplevel window exists?

2003-09-25 Thread David Storrs
> On Tue, Sep 23, 2003 at 12:04:05PM -0500, Robert Greenway wrote:
> >  have opened 3 additional toplevel windows in my application for input of
> > optional parameters. How can I query if they exist, so I don't recreate
> them
> > and so I can process them when completed?
> >
 On Wed, Sep 24, 2003 at 10:43:47AM -0500, Robert Greenway wrote:
> I am using Perl/tk. I have found out about using the state method. Which has
> the values Normal Iconic, and withdrawn. Using that method to test if it
> exists causes an error if the window hasn't been created yet.

Bob,

I'm not particularly familiar with Perl/Tk, so I can't say for sure.
However, when you say "causes an error", there should be a solution
there.  If the error is that it returned undef, then you can test for
the undef value (with "defined").  If it throws an exception, then you
can trap the exception and test its value.  Something like one of
these, maybe:

eval {$state = $window->state();  };
if ($@ && $@ =~ /Window does not exist/) {}

or 

if (defined $window->state()) { ... }
else { ... }


(Note that the exception will have some text associated with it, but
probably not the literal "Window does not exist" shown above.)


As I said above, this is just a guess on my part, but it's something
to try.

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How do I query whether a toplevel window exists?

2003-09-23 Thread David Storrs
On Tue, Sep 23, 2003 at 12:04:05PM -0500, Robert Greenway wrote:
>  have opened 3 additional toplevel windows in my application for input of
> optional parameters. How can I query if they exist, so I don't recreate them
> and so I can process them when completed?
> 
> Thanks
> Bob


Bob,

When you say 3 additional toplevel windows, what do you mean?  Is this
a Perl/Tk app, a browser-based app, what?


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Need help on Hash Slices. !!

2003-09-23 Thread David Storrs
On Tue, Sep 23, 2003 at 08:49:11PM +0530, Pandey Rajeev-A19514 wrote:
> Hi,
> 
> I have a list like
> 
> @list = ( "key1: Vlan1 :0989\n"
>   "key2: Vlan2 :0989\n"
>   "key3: Vlan3 :0989\n"
>   "key4: Vlan4 :0989\n");
> 
> I wanted to make a hash with keys as key1, key2 , key3, key4.
> 
> I want to write something like this :
> **
>   my @keys, @values, @key_to_value;
>   (@keys, @values) = map /(key\d+): (\w+).*?$/, @list;
>   @[EMAIL PROTECTED] = @values;
> 
> But the problem is the map function just creates one list i.e. @keys .. so I am not 
> able to make the hash.
> 
> ie .. I want to extract @keys and @values in one go and then use the HASH SLICE to 
> get the desired result.
> 
> Can some one help me. The only this is I don't want to extract @keys and @values 
> separately.
> 
> Regards
> Rajeev


Hi Rajeev,

Sounds like you want to create the hash, the @keys list, and the
@values list in one pass, right?  Ok, this should do it:

my (@keys, @values, %hash);
my @list = ( "key1: Vlan1 :0989\n",
 "key2: Vlan2 :0989\n",
 "key3: Vlan3 :0989\n",
 "key4: Vlan4 :0989\n",
 );
do { /(key\d+): (\w+).*?$/; push @keys, $1; push @values, $2; $hash{$1}=$2;  } for 
@list;



You can't use map here, because map always returns a single list, and
you are trying to build up multiple lists at once.  (Or, rather, you
COULD use it, instead of the for, but it would be pointless as you
would simply be throwing out the list that it returns.)

(Oh, and you needed commas after your list elements.)


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: migrating perl from version x to version y

2003-09-23 Thread David Storrs
On Tue, Sep 23, 2003 at 10:56:09AM -0400, Rich Fernandez wrote:
> Can some one tell me if there is an easy way generally speaking to migrate
> one version of perl and the modules installed under it to another version?
> 
> Also, in this specific case I want to go from 5.00503 to 5.8.0 The wrinkle
> is that the box that needs to be upgraded does not have http/ftp access to
> the world and can only be accessed through several hops from inside our
> network. If this were a file system I needed to move, I could simply tar it
> up and move it over. Is there something similar for perl?
> 
> Links/docs are appreciated.
> 
> Thanks!
> 
> richf


Hi Rich,

Well, you can certainly get a 5.8.0 tarball from CPAN, route it
through the necessary hops to the target machine, unroll and install.
If you have non-standard modules installed that you need, then you
will need to copy them from the 5.005 hierarchy to the 5.8.0 hierarchy
and, if any of them include XS (i.e., C code), then you will almost
certainly need to rebuild them.

Just make sure you take a backup snapshot of your existing Perl
directories and environment variables before you do this so that you
can roll back if anything goes wrong.

The following page is to the 5.8.0.  

http://search.cpan.org/~jhi/perl-5.8.0/

If you want the very latest version (5.8.1, a developer release), you
can get it here:

http://search.cpan.org/author/JHI/perl-5.8.1-RC5/

HTH,

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: multiple regex in if() staetment

2003-08-21 Thread David Storrs
On Tue, Aug 19, 2003 at 05:54:42PM -0500, Dan Muey wrote:
> Howdy all:
> 
> I'm trying to figure out the best way to test a string agains a list of regexs like 
> so:
> 
> my @regex = qw(qr(joe$) qr(^mama) qr([abc]));

As was pointed out already, don't use the qw().

Here are some interesting benchmarks.  The 'docs_in_col' file was
about 16k, and the strings I was testing for were right at the bottom.

#!/usr/bin/perl

use warnings;
use strict;
use IO::File;
use Benchmark;

my $fh = new IO::File("docs_in_col") or die $!;
my $str;
{
local $/;
$str = <$fh>;
}

my $alt_re  = qr(Zucker|Zuckerman|Zurrow);
my @grep_re = (qr(Zucker), qr(Zuckerman), qr(Zurrow));

timethese(5000, 
  { 
  match_alts_scalar => \&match_alts_scalar,
  match_alts_array => \&match_alts_array,
  grep_mults => \&grep_mults,
  }
 );

###

sub match_alts_scalar {
my $found = ($str =~ /$alt_re/);
return $found;
}

###

sub grep_mults {
my $found = grep { $str =~ /$_/ } @grep_re;
return $found;
}


And here are the results:

Benchmark: timing 5000 iterations of grep_mults, match_alts_scalar...
grep_mults:  1 wallclock secs ( 0.54 usr +  0.00 sys =  0.54 CPU) @ 9275.36/s (n=5000)
match_alts_scalar: 40 wallclock secs (39.20 usr +  0.02 sys = 39.22 CPU) @ 127.49/s 
(n=5000)


This should only be considered a first approximation, since there are
various things I'm not controlling for (all my strings were at the
end, they were fixed strings, etc).

Still, I would use the grep version.


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: compare date,time

2003-08-14 Thread David Storrs
On Mon, Aug 11, 2003 at 10:25:07PM +0200, awards wrote:
> Hi,
> 
> thank you, Now i have a new question.
> If I do the code myself which I can do.
> would the process of executing the program faster if I do the code myself or
> if I use a Module i.e Date::Calc??
> 
> regards
> awards


There is a small amount of overhead in loading a module, but it's
generally negligible.  After that, the question of "will my
hand-rolled code be faster than the module" comes down to a comparison
between the particular way that you implemented the code versus the
particular way that the module implemented it.  That is something that
only benchmarking can answer for you.

However, I would **strongly** recommend using the module.  It will
save you a lot of time, because the module is already written and
thoroughly tested, it is standard so it will make the lives of future
maintenance programmers (which might include you) easier, and, most
importantly, date math is a lot harder than you might think.  Here
are just a couple examples of why:

Q: If you add 24 hours to 12:10pm, February 28, what do you get?  
A: You can't tell without knowing what year you are working in (leap
year problem). 

Q: If you are working with past dates, how do you handle the fact that
different parts of the world adopted the Julian calendar on different
dates?


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: compare date,time

2003-08-14 Thread David Storrs
On Wed, Aug 13, 2003 at 09:35:20PM -0700, R. Joseph Newton wrote:
> David Storrs wrote:
> 
> > On Mon, Aug 11, 2003 at 10:25:07PM +0200, awards wrote:
[snip]
> > > would the process of executing the program faster if I do the code myself or
> > > if I use a Module i.e Date::Calc??
> >
[snip answer to above question]

> > However, I would **strongly** recommend using the module.
> 
> He might want, though, to choose a different Date module, as Date::Calc was
> created specifically for date-intensive calculations that may require a wide
> variety of conversions.  The documentation for the module indicates that users
> with more moderate date-handling needs should use a less-extensive module.
> 
> Joseph


True enough.  Notice, though, that I didn't say, '...recommend using
Date::Calc'.  I said I would recommend using the module...I was
thinking more generally, in the sense that it's better to have a
tried, tested, and standard module to solve a complex problem than it
is to try to do it yourself.

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: :Telnet

2003-08-14 Thread David Storrs
On Mon, Aug 11, 2003 at 09:42:40AM -0400, Stephen Gilbert wrote:
> > -Original Message-
> > From: SilverFox [mailto:[EMAIL PROTECTED]
> > Sent: Sunday, August 10, 2003 4:58 PM
> > To: [EMAIL PROTECTED]
> > Subject: Net::Telnet
> > 
> > 
> > hey anyone know how to check if a file exists using the Telnet module?
> > 
> > -- 
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > 
> try:
> $file_exists = $t->cmd("perl -e 'print 1 if (-s \"file\")'") or warn "Unable to 
> execute command: $!\n";
> if ($file_exists) {
> # do whatever
> }

'if (-s)' will actually check that the file has non-zero size (-s
returns the size; if the size if 0, it is false).

If you simply want to know whether it exists, but you don't care what
size it is, then use -e.


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Substiuting portions of a string with elements from a hash ...

2003-07-17 Thread David Storrs
Hi Jamie,

On Thu, Jul 17, 2003 at 11:46:08AM -0400, Jamie Risk wrote:
> I'm trying to add HTML anchors to a lines of text.  Quick example would be
> to take the line:
>"Search the internet using an engine like Google."
> and turn it into:
>"Search the internet using an engine like  href="www.google.com">Google."

> I can do this in a hacking sort of manner but it'll be slow and inelegant.
> I apologize for asking for design template/suggestions rather than
> help, 

Don't sweat it--those qualify as help too.  Personally, I think it
shows you're thinking.


As always, TIMTOWDI.  The best way depends on other constraints--where
are you planning on running this?  What kind of load will it see?  How
cross-platform does it need to be?  etc.


One solution is to tokenize your files yourself, then Reduce To
Problem Already Solved. (*) You can do a rough version of that with
something as simple as:

my $delims = qr/[^-\w']+/;
my @tokens = split /$delims/o, $text_of_file;

Or you can check CPAN (there is a Parse::Tokens module, but I haven't
used it; there are probably others too).  


Another way is to do a  s/$token/$replacement/eg on the text of your
file...it may not work perfectly, but it should go a long way.


A heavier-weight solution would be to use a real templating system
like Mason, The Template Toolkit, or Text::Template (to name a few...I
think CPAN has over 20).  Mason and TTT both come very highly
recommended to me.


--Dks

(*) Note that tokenizing English is hard--how do you handle hyphens vs
em-dashes (this sentence offering examples of each)?  You can't forget
the 'apostrophes vs single-quotes' issue either, and remember that
some possessive words' apostrophes are at the end.  How about
abbreviations with embedded punctuation (e.g. i.e.)?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: null fields from split

2003-07-13 Thread David Storrs
On Sun, Jul 13, 2003 at 08:39:16AM -0400, Jeff 'japhy' Pinyan wrote:
> On Jul 13, David Storrs said:
> 
> >Given this code:
> >
> >  my $TOKEN_DELIMS = qr/[^\w\-'\$]/;
> >  my $text = # string containing the contents of an mbox file
> >
> >  my @tokens = split /$TOKEN_DELIMS/o, $text;
> >
> >I end up with a large number of null strings ('') in @tokens.  After
> >RTFMing on split, I assume this is because I am matching at the
> >beginning/end of the string.  I can't quite grok how to eliminate
> >these, however.
> 
> Well, your split pattern is only matching ONE token delimiter at a time.
> That means on a string like "[EMAIL PROTECTED]", you're going to get back the
> elements "this", "", "", "that", because there's an empty string in
> between the ! and @, as well as between the @ and #.  I think you want to
> change your regex to /$TOKEN_DELIMS+/o instead.


Aha!  Great, thank you.


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



null fields from split

2003-07-13 Thread David Storrs
Greetings all,

Given this code:

  my $TOKEN_DELIMS = qr/[^\w\-'\$]/;
  my $text = # string containing the contents of an mbox file

  my @tokens = split /$TOKEN_DELIMS/o, $text;

I end up with a large number of null strings ('') in @tokens.  After
RTFMing on split, I assume this is because I am matching at the
beginning/end of the string.  I can't quite grok how to eliminate
these, however.

I can easily grep them out, but this is a time-sensitive routine, and
I'd rather just not generate them.  How can I modify my pattern (or my
string, without losing real data), so that I don't get these spurious
matches? 


Thanks in advance,

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: convertion bash script to perl

2003-07-09 Thread David Storrs
On Wed, Jul 09, 2003 at 05:59:47AM -0700, thyagarajan k wrote:
> Hello friends,
> 
> i am quite new to perl and currently i have a
> requirement like to re-write the script written in
> unix bash shell to perl.  I would like to know is
> there any convertion tools available so that i can
> convert my shell script to perl script.
> 
> rgds
> thyag

Actually, yes, there is a virtually perfect tool available online.

The interface is simple and works through any newsgroup client. You
just send a "translate" command and shortly thereafter (between a few
hours and a day, depending on load), you will receive a Perl
translation of your bash script.  The translations are of impressively
high quality.

The headers of your "translate" message are fixed and must appear
exactly as shown.  The body of the message can be more loosely formed
(the tool's natural-language processing is quite good), but the
content should be something like the example that follows:

   From:  
   To:comp.lang.perl.advocacy
   Subject: why is Perl better than Java/Python?

   I'm trying to convince my company to adopt Perl as our core
   technology.  My boss doesn't think Perl is good enough, and wants
   us to use either Python or Java.  I thought if I could take one of
   our mission-critical bash scripts and get a really good (i.e.,
   shorter, faster, cleaner) Perl translation of it, that might
   convince him.

   Problem: I don't know enough Perl yet (although I'm learning, and
   really enjoying it!).  Can anyone help me out by translating this,
   so that I don't get stuck having to do Python/Java forever?

  

   
Oh, if it's a rush job, then you can append the "increase priority"
command to the end.  It looks like this:


Many thanks in advance,







Ok, in all seriousness, I'm afraid I don't know of an automatic
translator from Bash to Perl.  There is an a2p which translates awk
scripts, and an s2p which translates sed scripts but those aren't what
you're looking for...although, if you could find a bash-to-awk, you
could chain the tools together.

Anyway, my advice would be to hit google.

--Dks


PS Yes, I stole the "Usenet as translator" joke from Randal Schwartz;
*bows to the master*

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Why doesn't this match

2003-07-09 Thread David Storrs
On Tue, Jul 08, 2003 at 02:48:59PM -0400, Paul Kraus wrote:
> Expression
> /Sales - Freight - Leimkuehler\s+(\d+,?\d+\.?\d+)/
> 
> String
> 410-02-00Sales - Freight - Leimkuehler
> 6.96
> 
> It does match if I remove everything after Leimkuehler.
> This is how it reads to me
> 
> 1 or more spaces
> Open paren to save value to $1
> 1 or more digits
> Maybe a comma but not necessary
> 1 or more digits
> Maybe a period but its not necessary.
> 1 or more digits

Good analysis.


> I am assuming my the problem is with ,? But I don't understand why.

More likely, your problem is the fact that you have a \n before the
6.96.  You can either put a literal \n in your pattern or add the 's'
modifier on your regex (e.g. /foo/s) so that the '.' metacharacter can
match \n as well as anything else.

Rather than trying to parse the numbers yourself, you migth want to
use Regexp::Common, available from CPAN.

http://search.cpan.org/author/ABIGAIL/Regexp-Common-2.113/lib/Regexp/Common.pm

Note that (as with any other precompiled rexex), the regexen supplied
via this module may be compiled into other regexen like so (run the
following to check):

#!/usr/bin/perl

my $a = qr/foo/;
my $b = qr/bar/;

$_ = "foobar";
print "Yes!" if /$a$b/; 




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Rename - readdir - Permission Denied

2003-07-08 Thread David Storrs
On Mon, Jul 07, 2003 at 11:45:34AM -0400, Paul Kraus wrote:
> I am doing a readdir.
> Taking the filenames that are found and trying to rename them.
> 
> Opendir 
> @files = Readdir 
> 
> Foreach @files
>   parse filename
>   $newfile = newfile name based on parsed name
>   push (@rename,[$_,$newfile])
> 
> Foreach @rename
>   rename $$_[0], $$_[1] or die ("NO $!\n")
> 
> I guess I could send all the code but that is pretty much what is taking
> place.
> I have tried to add a closedir before the @rename loop but it does
> nothing.

Well you might try something like this (this would append '.old' to
each filename.  Modify as appropriate):

use File::Basename;

@old = glob('path/to/files/*'); #  don't forget the '*'  !!
@old = map { basename($_} } @old;  # strip off the directory part
@new = map { "$_.old" } @old;  # Get the new versions
while (@old) {
  rename( shift @old, shift @new )  or die "NO  $!";
}


Of course, this can be collapsed a bit:

use File::Basename;
do{ $f=basename($_}; rename( $f, "$f.old" ) or die "NO $!"} 
for glob('path/to/files/*');


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: RegEx stuff

2003-07-07 Thread David Storrs
On Sat, Jul 05, 2003 at 09:06:53AM -0700, Skot wrote:
> Hello,
> 
> I want to search a string for matching content,
> not quite getting it.
> 
> something like:
> 
> if ($myInputLine =~ m/.\123\.222/) { ... }


Your regex consists of 6 atoms, which must be matched in the precise
order in which they appear in order for the match to succeed.  These
atoms are:

atommeaning
---
.   match any single character except a newline 
\123whatever was matched by the 123rd set of
capturing parens earlier in this regex 
[note you don't have such a thing]
\.  a literal period
2   a literal 2
2   a literal 2
2   a literal 2


Not quite sure what you wanted your re to match...you might try one of
these:

/.123\.222/   any non-newline,1,2,3,literal .,2,2,2
/\.123\.222/  literal .,1,2,3,literal .,2,2,2
/.\d{3}\.222/ any character,any three digits,literal .,2,2,2
/.\d*\.222/ any character,zero or more digits,literal .,2,2,2
/.\d+\.222/ any character,one or more digits,literal .,2,2,2


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: match count

2003-07-07 Thread David Storrs
On Sat, Jul 05, 2003 at 11:29:12AM -0400, Jeff 'japhy' Pinyan wrote:
> On Jul 3, George P. said:
> 
> >> while ($string =~ /pattern/g){
> >>   $count++;
> >>   if ($count > $max_count){
> >> $string = substr($string,0,pos($string));
> >> last;
> >>   }
> >> }
> >
> >$string =~ s/((.*?$pattern){$max_count})(.*)/$1/s;
> 
> You don't need to capture the .* at the end of the regex.  This is one of
> those cases where I my \K anchor/assertion idea would really come in
> handy:
> 
>   s/(?:.*?$pattern){$max_count}\K.*//s;
> 
> What the \K does is make the regex think it JUST started matching, so
> instead of replacing a bunch of stuff plus some extra fluff with the
> original bunch of stuff, we just say "after you've matched X, pretend you
> started matching HERE."  It comes in handy in substitutions that look like
> 
>   s/(A)B/$1/;

Honest question: What's the advantage of doing it with the \K anchor
as opposed to simply not matching the C section at all?  (Assuming,
of course, that you don't B to match it, e.g. to disambiguate
your match.)


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: RegEx stuff

2003-07-07 Thread David Storrs
On Mon, Jul 07, 2003 at 11:46:06AM -0700, David Storrs wrote:
> On Sat, Jul 05, 2003 at 09:06:53AM -0700, Skot wrote:
> > 
> > if ($myInputLine =~ m/.\123\.222/) { ... }
> 
> 
> Your regex consists of 6 atoms, which must be matched in the precise
> order in which they appear in order for the match to succeed.  These
> atoms are:
> 
> atommeaning
> ---
[...]
> \123whatever was matched by the 123rd set of
> capturing parens earlier in this regex 
> [note you don't have such a thing]
[...]

My bad; I misremembered.  The atoms \1 to \9 always represent
backreferences but above 9 they are only backreferences if you
actually have that many capturing parens.  So, actually, I believe
\123 will be treated as octal character 123, which is capital S.

The reason for this up-to-9 vs after-9 split is to help the perl
parser figure out whether you wanted the backreference or the octal
character. (If you want the octal character you must give it a leading
zero, like so: \09).

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Newbie and Perl+Mail Questions

2003-07-06 Thread David Storrs
On Sat, Jul 05, 2003 at 05:28:41PM +, Pablo Fischer wrote:
> Hi;
> 
> > cd ~
> > mkdir -p perl5mod/Mail
> > save Sendmail.pm to ~/perl5mod/Mail
> > export PERL5LIB=/home/unmada/perl5mod
> >
> > You probably want to put that last line in your .profile so it gets
> > reset every time you log in.

> But, If I just wanna to have ONE module in my working dir? and the other 
> modules in their correct places (like /usr/lib/perl/modules/blabla).

Not a problem.  It will all Just Work.


> > > >   $i = 100;
> > >
> > > number of mails..
> >
> > Correct; the assumption is that each mail will contain 10 mail adds.
> Well I know how to do this, however, I have 100 mail adds, so I need to send 
> 100 copies (1 copy to each mail), but I think how to do this: Implement in 
> the itereation a way to count each line of the line, so, when its 0, it will 
> read line 1, when its 1, it will read line 2.. but thanks :-).

Well, I don't really get what you're saying, but I'll assume that you
do and that it works.  $i was simply a loop counter--it just means
that you're going to go through the loop 100 times.  Each iteration,
you can send as many mail adds as you want.

But whatever.



> > > Thanks! I was thinking in this option, but also Im looking for a second
> > > option, to know if In sendmail I could manage the threads (to send each
> > > mail, each 5 seconds).. I know this is a perl list, but.. does someone
> > > knows if I can?
> > > thanks!
> > >
> > > Pablo
> >
> > Well, do you really want to do that?  The whole point is to avoid
> > sending the messages too quickly.  If you parallelize it, then the
> > messages go out faster, and you may end up looking like a spammer.
> >
> > If you DO want to do it, here's how:
> >
> > First, you need Perl 5.8 with the threading option selected at compile
> > time.  To see if you do, type 'perl -v'.  If the first line looks
> > something like this:
> >
> > This is perl, v5.8.0 built for i586-linux-thread-multi
> >
> Wait, wait.. I tried to say: mail queus, not thread in perl :-), sorry. So the 
> question is: How to manage the queus in sendmail: For example, I send 100 
> mails in a second to sendmail, but is there a form so that sendmail will take 
> mail 1, and send it now, mail 2 and send it in 3secs, mail 3 and send it in 6 
> secs, in other words how to send one mail in a specified interval (seconds)
> 
> Thanks!
> Pablo

Pablo, I think we're having a communication gap here.  Let me try one
more time.

The original code that I sent you essentially boiled down to this:

while(more mails to send) {
   send next mail;
   sleep 1;
}

That C means to wait one second before sending the next
mail.  If you want it to be 3 seconds, then just say C.  How
is this different from what you want sendmail to do for you? (*)  Don't
complicate things more than they need to be.

--Dks


(*) If your answer is 'because then sendmail is managing the queues
instead of perl', then you're on the wrong list.  Just run the script
in the background if you don't want it tying up a terminal window for
too long.  Or run it in cron.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: testing an argument's type

2003-07-05 Thread David Storrs
On Sat, Jul 05, 2003 at 12:29:46PM -0400, Bob Showalter wrote:
> David Storrs wrote:
> > I've got a function that takes several arguments, the first of which
> > should be a scalar (specifically, a string).  I'd like to have a
> > precondition to verify that the argument is, in fact, a scalar.  Is
> > there a way to do that (preferably without using modules--I'm trying
> > to write an entirely self-contained script).
> >
> > I could make it take a REFERENCE to a scalar, and then do "ref $_[0]
> > eq 'SCALAR'", but that's not really what I'm looking for.
> 
> I'm not sure the suggestions re:prototypes are what you're looking for. That
> will coerce a scalar context onto a call like:
> 
>foo(@INC);
> 
> When your sub is called, @_ contains a list of scalars. So the first
> argument *IS* a scalar. If you don't want it to be reference, then you need
> to use the ref() function:
> 
>sub foo {
>   my $arg = shift;
>   die "First argument must be a simple scalar, not a reference" if ref
> $arg;
>}

Urgh; this is living proof that sometimes a better statement of the
problem is all that's required.  Thank you very much.


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Newbie and Perl+Mail Questions

2003-07-05 Thread David Storrs
On Sat, Jul 05, 2003 at 12:25:47PM +, Pablo Fischer wrote:

> >   use Mail::Sendmail;
> Load Module. Can I save it in my working dir? /home/unmada/MailP/?

Sure.  You need to make sure that it will appear on @INC (the module
include path that Perl uses to find your modules).  Probably the
easiest way to do that is to set the PERL5LIB environment variable to
whatever directory you save it in.  Notice that the Mail::Sendmail
module name means that Perl is looking for a FILE named 'Sendmail.pm'
in a DIRECTORY named 'Mail'.

I would suggest doing the following:

cd ~
mkdir -p perl5mod/Mail
save Sendmail.pm to ~/perl5mod/Mail
export PERL5LIB=/home/unmada/perl5mod

You probably want to put that last line in your .profile so it gets
reset every time you log in.


> >   $i = 100;
> number of mails..

Correct; the assumption is that each mail will contain 10 mail adds.

> >   while ($i--) {
> > $body_of_message = ...;
> iteration..

Yes.  Note that the '...' means "whatever you need to do to compose
the body of your message, do it here.


> > sendmail(%mail) or die $Mail::Sendmail::error;
> send mail and print errors

Yep.  Thinking about it, you might want to change the 'die' to a
'warn'--that way, if one block fails, you can continue.  On the other
hand, if sendmail() fails, the most likely reason is because it can't
connect to your mail server, or somesuch.

Oh, one thing I forgot.  You should specify your mail server in the
constructor, as follows:

> > %mail = ( To  => '[EMAIL PROTECTED]',
> >   From=> '[EMAIL PROTECTED]',
> >   smtp=> 'mail.mycompany.com',  # NOTE THIS LINE
> >   Subject => 'mailadds, group ' . (100 - $i + 1),
> >   Message => $body_of_message,
> > );




> Thanks! I was thinking in this option, but also Im looking for a second 
> option, to know if In sendmail I could manage the threads (to send each mail, 
> each 5 seconds).. I know this is a perl list, but.. does someone knows if I 
> can?
> thanks!
> 
> Pablo

Well, do you really want to do that?  The whole point is to avoid
sending the messages too quickly.  If you parallelize it, then the
messages go out faster, and you may end up looking like a spammer.

If you DO want to do it, here's how: 

First, you need Perl 5.8 with the threading option selected at compile
time.  To see if you do, type 'perl -v'.  If the first line looks
something like this:

This is perl, v5.8.0 built for i586-linux-thread-multi

Then you have threading capability.

The docs on threads are here:

http://www.perldoc.com/perl5.8.0/pod/perlthrtut.html

and also come with Perl:

perldoc perlthrtut

To do this threaded, your script would look (something) like this (I
haven't tested it):


#!/usr/bin/perl 

use warnings;
use threads;
use Mail::Sendmail;


for(1..10) { 
threads->new(\&mail, $_)->detach() ;
} 

sub mail {
my $num = shift;
my $start = $num * 100;   # 0, 100, 200, etc.
my $end   = $num * 100 + 99;  # 99, 199, 299, etc

#  Build the body of the mail here.  $start and $end are the 
#  indexes of the mailadds that should be in the mail
my $body = ???;

%mail = ( To  => '[EMAIL PROTECTED]',
  From=> '[EMAIL PROTECTED]',
  smtp=> 'mail.server.com';
  Subject => 'some subjet',
  Message => $body,
   );

sendmail(%mail) or die $Mail::Sendmail::error;
print "OK. Log says:\n", $Mail::Sendmail::log;
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Newbie and Perl+Mail Questions

2003-07-05 Thread David Storrs
On Sat, Jul 05, 2003 at 12:43:10AM +, Pablo Fischer wrote:
> Hi all!
> 
> My name is Pablo Fischer and Im a little new in Perl, however I have a project 
> where I need to parse mails and send mails:
> 
> I receive 2 files (contacts and arguments), I get 2000 mail adds from the 
> file, so I need to make packages of 100 mails with differents email adds, 
> When I have the package I need to send this mails with sendmail to each 
> address, however, it sounds very easy (just parse and send mails), but the 
> funny thing starts here:
> 
> I Cannot send 15 mails of *.prodigy.net.mx (an ISP of my country) because this  
> ISP will block my IP. So, exists a form to mix sendmail and perl, or how to 
> do it in sendmail to: dont send 100 packages in a second, send them like 
> 10,10,10..1000, in other words: how to work with threads in sendmail.


Pablo,

If I understand correctly, you just want to pause between sending your
mails so that the ISP doesn't interpret you as a spammer, right?  You
can do that like this:

  use Mail::Sendmail;

  $i = 100;
  while ($i--) {
$body_of_message = ...;

%mail = ( To  => '[EMAIL PROTECTED]',
  From=> '[EMAIL PROTECTED]',
  Subject => 'mailadds, group ' . (100 - $i + 1),
  Message => $body_of_message,
);
sendmail(%mail) or die $Mail::Sendmail::error;
print "OK. Log says:\n", $Mail::Sendmail::log;

sleep 1;  # Wait 1 second before sending next batch
  }

You can get Mail::Sendmail from CPAN: 

http://search.cpan.org/author/MIVKOVIC/Mail-Sendmail-0.79/Sendmail.pm

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: testing an argument's type

2003-07-05 Thread David Storrs
On Fri, Jul 04, 2003 at 06:03:55PM -0400, Casey West wrote:
> It was Friday, July 04, 2003 when David Storrs took the soap box, saying:
> : I've got a function that takes several arguments, the first of which
> : should be a scalar (specifically, a string).  I'd like to have a
> : precondition to verify that the argument is, in fact, a scalar.  Is
> : there a way to do that (preferably without using modules--I'm trying
> : to write an entirely self-contained script).
> 
> Provided that you really have a function, that you're calling as a
> function, you can use prototypes.  They are documented in perlsub, and
> have the following syntax.
> 
> sub function($) {
> 
> }
> 
> This will require the first argument to be scalar.  If the first
> argument is mandatory, follow the dollar sign ($) with a semi-colon
> (;).


Thanks Casey,

I was trying to avoid prototypes, because I wanted people to be able
to pass the arguments in as function(@args), but maybe this is the way
to go.  Or maybe I just won't worry about it.  Thanks.


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: testing an argument's type

2003-07-05 Thread David Storrs
On Fri, Jul 04, 2003 at 06:02:21PM -0400, Steve Grazzini wrote:
> On Fri, Jul 04, 2003 at 02:18:40PM -0700, David Storrs wrote:
> > I've got a function that takes several arguments, the first of 
> > which should be a scalar (specifically, a string).  I'd like to 
> > have a precondition to verify that the argument is, in fact, a 
> > scalar.
> 
> I'll recommend that you *don't* try to do this.  If a user of
> your function wants to do
> 
> my @args = (133, $x, $y);
> your_function(@args);
> 
> you shouldn't mind[*].  The array will get flattened into a list
> of scalars, and the numeric value will automatically get converted
> into a string as soon as you use it as a string.

Oh, I wasn't worried about getting a number; that I can deal with
fine.  I was more worried about getting, e.g., a reference (if the
argument were accidentally given out of order).  Take the
length of a ref or trying to run a regex on it tends to not work.

I suppose I could just use named parameters.  

Anyway, thank you.

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



testing an argument's type

2003-07-04 Thread David Storrs
I've got a function that takes several arguments, the first of which
should be a scalar (specifically, a string).  I'd like to have a
precondition to verify that the argument is, in fact, a scalar.  Is
there a way to do that (preferably without using modules--I'm trying
to write an entirely self-contained script).

I could make it take a REFERENCE to a scalar, and then do "ref $_[0]
eq 'SCALAR'", but that's not really what I'm looking for.

The value may have been specified as a literal, so I can't do
anything using the *{foo}{SCALAR} syntax (even if I had the name of
the variable and not its value).

Any thoughts?

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to check Array Uniqueness without Modul Array::unique

2003-07-01 Thread David Storrs
On Tue, Jul 01, 2003 at 01:18:46PM -0400, Robin Norwood wrote:
> David Storrs <[EMAIL PROTECTED]> writes:

> 
> keys_compare might be slightly better for small sets, but for large
> sets, short_circuit is the way to go.
> 
> I initialized my @array like so:
> 
> my @array;
> for (1 .. 5000) {  push @array, int((rand) * 1); }
> 
> And I only ran 10,000 iterations, since I wanted it to finish before I
> got bored with the whole thing.
> 
> Results:
> 
> Benchmark: timing 1 iterations of grep_compare, keys_compare, short_circuit...
> 
> grep_compare: 140 wallclock secs (136.80 usr +  0.37 sys = 137.17 CPU) @ 72.90/s 
> (n=1)
> 
> keys_compare: 127 wallclock secs (126.35 usr +  0.21 sys = 126.56 CPU) @ 79.01/s 
> (n=1)
> 
> short_circuit:  5 wallclock secs ( 4.67 usr +  0.01 sys =  4.68 CPU) @ 2136.75/s 
> (n=1)


Cool.  Thanks; this bears out my original feeling that short_circuit
should be the way to go (and saves me the trouble of futzing with it
when I get home tonight :> ).

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to check Array Uniqueness without Modul Array::unique

2003-07-01 Thread David Storrs

On Tue, Jul 01, 2003 at 06:26:08AM +0200, Janek Schleicher wrote:
> B. Fongo wrote at Mon, 30 Jun 2003 23:46:19 +0200:
> 
> > Is there any other way to check array uniqueness except the Module
> > Array::Unique?
> > I'm having problems in installing these two Modules: Array::Unique and
> > Tie.  So I'm thinking of another way to check array for replicates.
> > 
> > I help will be appreciate. Thanks a lot!
> 
> You can also hardcode it e.g.
> 
> {
>my %seen;
>if (grep {$seen{$_}} @array) {
>   # at least one element is twice
>} else {
>   # unique
>}
> }
> 
> 
> 
> Greetings,
> Janek


Umm...this doesn't actually work.

Here are two versions that do work:

#!/usr/bin/perl 

use Benchmark;

my @array = qw( bar baz foo);

timethese(1_000_000, 
  {
  'short_circuit' => \&short_circuit,
  'keys_compare'  => \&keys_compare,
  }
);

sub short_circuit {
# return 1 if dupe, 0 if unique

my %seen;
my $is_dupe = 0;
for(@array) {
$seen{$_}++; 
if ($seen{$_} > 1) {
$is_dupe++; 
last;
}
} 
return $is_dupe;
}

sub keys_compare {
# return 1 if dupe, 0 if unique

my %seen;
$seen{$_} = 1 for(@array) ;

if (scalar keys %seen != scalar @array) {
return 1;
} else {
return 0;
}
}
__END__

Out of curiousity, I benchmarked them a couple times.  Here are the
results (note that I'm only testing with 4 elements; the timings could
change greatly with large datasets):

The executive summary is that short_circuit has a significantly better
best case, a worse worst case, and considerably more variability than
keys_compare.  So, you're probably better off using keys_compare.



Case #1
(  my @array = qw( foo bar baz jaz);  )


Benchmark: timing 100 iterations of keys_compare, short_circuit...

keys_compare: 19 wallclock secs (17.77 usr +  0.01 sys = 17.77 CPU) @
56263.74/s (n=100)

short_circuit: 19 wallclock secs (19.31 usr +  0.01 sys = 19.32 CPU) @
51759.00/s (n=100)

With a unique array of 4 elements, they run at identical wallclock
speeds.  keys_compare wins on CPU time, though.

---

Case #2
(  my @array = qw( foo bar baz foo);  )

Benchmark: timing 100 iterations of keys_compare, short_circuit...

keys_compare: 16 wallclock secs (15.98 usr +  0.00 sys = 15.98 CPU) @
62561.09/s (n=100)

short_circuit: 20 wallclock secs (16.85 usr +  0.00 sys = 16.85 CPU) @
59341.68/s (n=100)

This one is the worst-case for short_circuit; the duplicate is at the
far end of the array.  In this case, it runs slower than keys_compare
by 25%.  Counterintuitively, the gap between CPU times has gotten smaller.

---

Case #3
(  my @array = qw( foo foo bar baz );  )

Benchmark: timing 100 iterations of keys_compare, short_circuit...

keys_compare: 17 wallclock secs (15.94 usr +  0.00 sys = 15.94 CPU) @
62745.10/s (n=100)

short_circuit: 10 wallclock secs (10.77 usr +  0.00 sys = 10.77 CPU) @
92888.24/s (n=100)

This is the ideal case for short_circuit; the duplicate is immediate
so that it can take advantage of its optimization early on.  In this
case, it wins hands-down, both in wallclock and CPU time.


The really interesting part, to me, is that keys_compare has extremely
stable performance...averaging 17. wallclock seconds and never
straying far from that.  short_circuit, OTOH, varies from 10 to 20
seconds depending on its data...and that was with a tiny data
set. (OTGH, I'm testing with a trivial dataset and only performed
three tests, so my results probably aren't very authoritative.)

In any case, these performance numbers surprise me, because both
algorithms should be dominated by the O(n) step (initializing the
hash), so should run about the same speed except for when
short_circuit gets to quit early.  

Anyone have any thoughts on this?  (I suppose I should deparse it to
see what's really going on; that might answer my question.)


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: context, printing, and concatenation

2003-06-25 Thread David Storrs
On Mon, Jun 23, 2003 at 11:13:00PM -0500, Peter wrote:
> I'm on the first few chapters of "Learning Perl" and came up with a
> question. Given:
> 
> -
> 
> @array = qw ( one two three );
> print @array . "\n";
> print @array;
> 
> -
> 
> Why does the first print statement print "3" (and a carriage return)
> while the second prints "onetwothree"?  I'm guessing that the first
> print sees the array in scalar context while the second sees it in list
> context, but if so I don't understand why.  Can someone break it down
> what the concatenation operator is doing here?

You nailed it; it's a context problem.  C evaluates its
arguments in list context, but C<.> evaluates ITS arguments in scalar
context, and it gets evaluated before the print.  This would be more
clear if we pretended that C<.> was a prefix function like C,
instead of a circumfix.  Then the lines above would look like this:

print(  .(@array, "\n") );
 ^   ^
 |   |--- scalar context starts here
 |
list context starts here

In contrast, when you do this:

print( @array );

...there is no context at work except that enforced by the C.

Did that help?

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: using FIle::Find::name if regex to filter

2003-06-24 Thread David Storrs
On Mon, Jun 23, 2003 at 09:14:25PM +0200, [EMAIL PROTECTED] wrote:
> hi, how can i use a real regex with this:
> 
> File::Find::name if -x?
> 
> the filetestops are working fine, but how can i filter things like
> .gz$? i tried many ways, but all failed. any idea? thanks!!
> 
> bye andreas

Andreas,

I'm not quite clear on what it is you want to do.  It sounds like you
want to use File::Find to run some code on all files in a particular
subtree, but you don't want to run the code on anything ending with
.gz.  If I'm correct, then you can do this:


#!/usr/bin/perl

use warnings;
use strict;
use File::Find;

my $start_dir = "top of your subtree goes here";
find( \&my_func, "$start_dir" );

sub my_func {
return if /\.gz$/;

# ...do whatever you want here...
}


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Rational Clearcase perl and Mail::Send

2003-06-19 Thread David Storrs
On Sat, Jun 14, 2003 at 11:59:55AM -0500, Wiggins d'Anconia wrote:
> David Storrs wrote:

> > Is anyone familiar with Mail::Send and, if so, do you have an answer
> > to this?
> > 
> 
> It appears Mail::Send just wraps Mail::Mailer, which states that in its 
> open call, headers may be specified as either a scalar or as an array 
> reference. Try setting the 'to' parameter to an array reference.


Thanks, we got it working.

--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Rational Clearcase perl and Mail::Send

2003-06-14 Thread David Storrs
At work, we have a dated copy of Rational Clearcase that shipped with
Perl 5.001 (!).  The distribution comes with a module, Mail::Send.
Our data manager is trying to write a script that will parse the logs
after the nightly build and mail the results to several addresses (one
or more of which may be a mailing list).  I've been working with him
on this, and we cannot find a way to make Mail::Send email more than
one person at a time.   (I can do it fine with the Mail::Sendmail
module from CPAN, but that would require bringing the module to
everyone's machine, or doing a network install and then getting
everyone to point at it, or something else--Scott wants a solution
that runs out of the box.)

Is anyone familiar with Mail::Send and, if so, do you have an answer
to this?


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Wanting to send email but have some bold or underlined text in message

2003-06-14 Thread David Storrs
On Fri, Jun 13, 2003 at 04:24:16PM -0700, Wagner, David --- Senior Programmer Analyst 
--- WGO wrote:
>   So used to just sending data and I was wondering if one can send bold or 
> underlined text, etc from straight email message or does it have to be in an 
> attachment?
> 
>   Using w2k on AS Perl build 623 or 635.
> 
>   Thanks.
> 
> Wags ;)


Well, the most straightforward way--assuming that your recipient has
an HTML-enable mail client--is to send the message formatted as HTML,
with appropriate tags for bold and underline.

Certainly, if the person you are emailing uses Outlook, this will
work.


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Best way to return largest of 3 Vars?

2003-01-11 Thread David Storrs
On Fri, Jan 10, 2003 at 10:23:43AM -0600, Jensen Kenneth B SrA AFPC/DPDMPQ wrote:
> In the line
> my $max = (sort {$b<=>$a} ($x, $y, $z))[0];
> 
> What is the "[0]" doing?

This part:

 (sort {$b<=>$a} ($x, $y, $z))

creates a list.  The "[0]" returns the first element in that list.


> Also another question. While messing around with the compact version of if
> statements I tried these.
> 
> $one = 1;
> ($one == 1) ? (print "\$one equals", print "$one") : (print "\$one does not
> ", print "equal 1");
> 
> Returns
> 1
> $one equals 1
> 
> At first I thought the first "1" on a line by itself was a return flag or
> something, 

That's exactly what it is.  To Perl, this:

   print "\$one equals", print "$one"

means this (letters on the left are just for reference):

A)   print a list consisting of:
B)- the string "\$one equals" and 
C)- the value returned from evaluating the expression
  'print "$one"'

Before it can do (A), it needs to figure out the value of (C).  So, it
evaluates (C).  That outputs the value of "$one" (which happens to be
1) to the screen, and returns 1 (because print always returns 1 if
successful).  (A) now has its argumnt list figured out, so it prints
them, which outputs the string "$one equals 1".

Had you done this instead:

$one = 7;
($one == 7) ? 
(print "\$one equals ", print "$one") 
: (print "\$one does not ", print "equal 1")
;

You would have seen this:

7
$one equals 7


However, why there is a newline after the initial 1, I'm not sure...is
there anything in your code that you haven't shown us?  For example,
did you set the $\ variable?


Given the above, you should be able to step through the other examples
you gave and figure out what's going on.  Just work from the inside
out.

--Dks



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]