[25739] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7979 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Apr 15 14:10:21 2005

Date: Fri, 15 Apr 2005 11:10:14 -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, 15 Apr 2005     Volume: 10 Number: 7979

Today's topics:
    Re: s/(.)/($1)/g vs s/(.)/$to/eg where $to='($1)' <nobull@mail.com>
    Re: trouble with very simple regexp <thundergnat@hotmail.com>
    Re: trouble with very simple regexp <spamtrap@Astrumtech.com>
    Re: trouble with very simple regexp ioneabu@yahoo.com
        trouble writting to file <alexj@floor.ch>
    Re: trouble writting to file <scobloke2@infotop.co.uk>
    Re: trouble writting to file <tadmc@augustmail.com>
        What is going on? giraffe6044@gmail.com
    Re: while loop into database insert not working <darkon.tdo@gmail.com>
    Re: while loop into database insert not working <nobull@mail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 15 Apr 2005 10:22:05 -0700
From: "nobull@mail.com" <nobull@mail.com>
Subject: Re: s/(.)/($1)/g vs s/(.)/$to/eg where $to='($1)'
Message-Id: <1113585725.450924.182670@o13g2000cwo.googlegroups.com>

jilerner@yahoo.com wrote:
> > http://birmingham.pm.org/talks/faq/Hello.html
>
> Thanks a lot for the link and the answers therein.
>
> Can you explain what are shortcomings
> of the following solution that you mention ?:
>
>   my $search = 'foo(.*?)bar';
>   my $replace = 'wibble$1wobble';
>
>   s/$search/qq!"$replace"!/eeg;
>   die $@ if $@;

The words I spoke to this slide were something along the lines:

It has calls eval() inside a loop (s///g is a looping construct) but
tests $@ outside the loop so will ignore errors from anything but the
last iteration.

It uses a single character quoting character (the double quote
character in this case) so will be confused by that character in
$replace.

The solution on the next slide correctly tests $@ inside the loop.

It uses a multi-character delimiter which is much less likely to
crop-up by chance.

Both solutions, of course, suffer from the usual hazards of using
eval() as described on the slide after.



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

Date: Fri, 15 Apr 2005 08:27:37 -0400
From: thundergnat <thundergnat@hotmail.com>
Subject: Re: trouble with very simple regexp
Message-Id: <ieCdnWx7Ip2kLsLfRVn-oA@rcn.net>

ioneabu@yahoo.com wrote:
> BerkHolz, Steven wrote:
> 
>>OK, I assume this is simple, but I haven't figured it out yet.
>>
>>I want to find joeblow, joe-blow or joe_blow.
> 
> 
> /joe.*blow/
> 
> Will match all of the above.  What do you not want to find?
> 
> wana
> 

Well, I rather suspect he would *not* want to match things like:

my $txt = 'Mary, joeseph and jesus! What happened to my leaf-blower?';
print "Match" if $txt =~ /joe.*blow/;


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

Date: Fri, 15 Apr 2005 10:51:02 -0400
From: "BerkHolz, Steven" <spamtrap@Astrumtech.com>
Subject: Re: trouble with very simple regexp
Message-Id: <425fd7a2@x-privat.org>

I don't think that I tried the joe[_-]?blow or  joe(_|-)?blow.
I tried the question marks inside like (?:_|-), but it didn't work.
The catch is that this is a word list and I do not have access to the actual 
code, so not all regexps might work.

I will try the joe[_-]?blow or  joe(_|-)?blow.
Is one more efficient than the other since it is a single character and not 
a string?
I know I would have to use (_|-) for stings.

-- 
Steven BerkHolz
Send to Domain TESCOGroup dot com, username SB

Note: you may also want to know that you should never send mail to:
blacklist-my-ip@admins.ws
info@dautrap.uceprotect.net
listme@sorbs.net
spamtrap@sandes.dk
spamtrap@stop.mail-abuse.org
spamtrap@frankenbiker.de
spamtrap@blars.org
"Jeff Stampes" <Jeff.Stampes@xilinx.com> wrote in message 
news:d3meoj$jaq3@cliff.xsj.xilinx.com...
> peter pilsl wrote:
>> BerkHolz, Steven wrote:
>>> I want to find joeblow, joe-blow or joe_blow.
>>>
>>> I am using two regexps:
>>> joeblow
>>> joe[_-]blow
>>>
>>> What do I use to match this in one regexp?
>>
>> joe[_-]?blow
>> or
>> joe(_|-)?blow
>
> Or assuming you're not really needing to capture what comes between them, 
> and you want to be more careful about getting exactly what you're looking 
> for (instead of 'gottagojoe-blowhard')
>
> /\bjoe(?:_|-)?blow\b/ 




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

Date: 15 Apr 2005 10:55:34 -0700
From: ioneabu@yahoo.com
Subject: Re: trouble with very simple regexp
Message-Id: <1113587734.528839.44650@l41g2000cwc.googlegroups.com>


thundergnat wrote:
> ioneabu@yahoo.com wrote:
> > BerkHolz, Steven wrote:
> >
> >>OK, I assume this is simple, but I haven't figured it out yet.
> >>
> >>I want to find joeblow, joe-blow or joe_blow.
> >
> >
> > /joe.*blow/
> >
> > Will match all of the above.  What do you not want to find?
> >
> > wana
> >
>
> Well, I rather suspect he would *not* want to match things like:
>
> my $txt = 'Mary, joeseph and jesus! What happened to my
leaf-blower?';
> print "Match" if $txt =~ /joe.*blow/;

:^)

That's true.

I guess I could have just said:

/.*/

It's a matter of specificity and sensitivity, but don't ask me which
one.  I always get them mixed up.

wana



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

Date: Fri, 15 Apr 2005 14:24:35 +0200
From: Alexandre Jaquet <alexj@floor.ch>
Subject: trouble writting to file
Message-Id: <d3obq3$765$1@news.hispeed.ch>

Hi I could not find why, I can't write data
to my file :

open (FILE, ">$interfacePath/$interfaceFile") or die "cannot open
chmod (0775, "$interfacePath/$interfaceFile");
$interfacePath/$interfaceFile\n" ;

print FILE "test,test,test";

In my file : FILE : 
/web/floor.ch/cms/interface/disquesoffice/Library/category/interface/test-save.icf

ls -al :


-rwxrwxr-x  1 www    web    0 Apr 15 14:21 test-save.icf


thx in advance


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

Date: Fri, 15 Apr 2005 14:47:08 +0000 (UTC)
From: Ian Wilson <scobloke2@infotop.co.uk>
Subject: Re: trouble writting to file
Message-Id: <d3ok5a$gqf$1@sparta.btinternet.com>

Alexandre Jaquet wrote:
> Hi I could not find why, I can't write data
> to my file :

You should read the posting guideleines before posting a question.

> 
> open (FILE, ">$interfacePath/$interfaceFile") or die "cannot open
> chmod (0775, "$interfacePath/$interfaceFile");
> $interfacePath/$interfaceFile\n" ;
> 
> print FILE "test,test,test";

The above isn't valid Perl, looks like the chmod has been inserted in 
the middle of the "open ... die ..." statement.

A perl program should start
#!perl
use strict;
use warnings;

You shoulc close the file you opened.

> 
> In my file : FILE : 
> /web/floor.ch/cms/interface/disquesoffice/Library/category/interface/test-save.icf 
> 
> 
> ls -al :
> -rwxrwxr-x  1 www    web    0 Apr 15 14:21 test-save.icf

Your "die" should take care to print the error details: include $! in 
the output.

e.g.
#!perl
use strict;
use warnings;
open (FILE, ">$interfacePath/$interfaceFile")
   or die "cannot open $interfacePath/$interfaceFile because $!" ;
chmod (0775, "$interfacePath/$interfaceFile");
print FILE 'test,test,test';
close FILE;


You should read about lexical filehandles and 3-way opens:
   open my $fh, '>', $filename or die ...

Understand the difference between "test,test,test" and 'test,test,test'.

If you do all the above at least you'll have an informative error message.


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

Date: Fri, 15 Apr 2005 09:20:01 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: trouble writting to file
Message-Id: <slrnd5vjch.bqr.tadmc@magna.augustmail.com>

Alexandre Jaquet <alexj@floor.ch> wrote:

> Hi I could not find why, 


Because you have a syntax error.


> I can't write data
> to my file :


Because the program never executes.


> open (FILE, ">$interfacePath/$interfaceFile") or die "cannot open
> chmod (0775, "$interfacePath/$interfaceFile");
               ^
               ^

There is the end of the argument to die()...


> $interfacePath/$interfaceFile\n" ;


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


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

Date: 15 Apr 2005 08:07:18 -0700
From: giraffe6044@gmail.com
Subject: What is going on?
Message-Id: <1113575996.933420.146800@l41g2000cwc.googlegroups.com>

I  was given this perl script as a example of a program to control our
remote powerdevices. Problem is i do not know Perl. I am creating the
site in Coldfusion
Can someone help explain to me what this script is doing to get the
STATUS of the RPS.

is it a form post? or is it requesting a URL and if so what URL.

here is the device.
http://www.digital-loggers.com/EPC.html

Thank you very much for any help

#!/usr/bin/perl -w
#----------------------------------------------------------------------
use LWP::UserAgent;
#----------------------------------------------------------------------
$ua = LWP::UserAgent->new();
#----------------------------------------------------------------------

if ($#ARGV <= 1)
    {
    print STDERR 'Usage: UserUtil <Host>[:port] <login:password>
<[n]{on|off|pulse|status}> ...'."\n";
    exit -1;
    }
($epc, $auth)=splice(@ARGV,0,2);
$base='http://'.$auth.'@'.$epc.'/';

foreach (@ARGV)
{
    $_=lc;
    s/(^[^1-8])/a$1/;
    if (/^([1-8a])on$/)
	{
	RelLink('outleton?'.$1);
	}
    elsif (/^([1-8a])off$/)
	{
	RelLink('outletoff?'.$1);
	}
    elsif (/^([1-8a])pulse$/)
	{
	RelLink('outletgl?'.$1);
	}
    elsif (/^([1-8a])status$/)
	{
	$n=$1;
	defined($response) && ($response->content =~/<a href=outleto/) ||
RelLink('');
	$content=$response->content;
	while ($content =~ /<a href=outlet(on|off)\?([1-8])>/ig)
	    {
	    if (($2 eq $n) || ($n eq 'a'))
		{
		if ($1 eq "on")
		    {print $2," OFF\n";}
		else
		    {print $2," ON\n";}
		}
	    }
	}
    else
	{
	die "Unknown command $_\n";
	}
}

sub RelLink
{
local ($_) = @_;
#print STDERR $base.$_,"\n";
$response = $ua->get($base.$_);
$response->is_error() && die $response->status_line;
}



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

Date: Fri, 15 Apr 2005 15:25:16 -0000
From: "David K. Wall" <darkon.tdo@gmail.com>
Subject: Re: while loop into database insert not working
Message-Id: <Xns9639742E77E7Ddkwwashere@216.168.3.30>

Unknown <perlguru2b@bobotheclown.org> wrote:

>  I am working on this script which will parse several Backup Exec
>  logs and place key details into a database for viewing as
>  statistics on a web based front end.  I posted all the code, but
>  I am only having trouble with the while loop (inside the
>  foreach). It is not behaving the way I expect it to in that it
>  prints several copies of each record instea of just one record
>  for one text file. 

I can't tell what might be causing that behaviour, but I do have a 
few comments that I hope will be helpful.

By the way, I do hope your code is formatted better than it appears 
in this post.  Proper indentation makes code much easier to read
and debug -- and makes it more likely that someone in this newsgroup
will bother to try and help.

> #!/usr/bin/perl
> use warnings;
> use strict;
> use DBI;
> use DBD::Pg;
> 
> my $filedir = '/var/backuplogs';
> my $servername;
> my $jobname;
> my $jobstarted;
> my $medialabel;
> my $logfile;
> my $jobended;
> my $status;
> my @inuse;
> my @skipped;
> my @corrupt;
> my @skippednames;
> my $skipped;
> my $corrupt;
> my $inuse;

Most of these variables are only used inside the foreach loop, so 
it's best to declare them at the top of the loop, confining their 
scope to ONLY the foreach loop.  That way you don't have to worry 
about re-initializing them at the end of the loop.  (Maybe this is
the problem?)

> opendir(DIR, $filedir) or die "could not open logfile directory: $!";
> my @filestoparse =  grep { /\.txt$/ } readdir(DIR);
> closedir(DIR);
> 
> foreach my $filename (@filestoparse)    {

Why not just say 

    foreach my $logname (@filestoparse) {

Then you would have one fewer variable to keep track of.

> my $complete = "$filedir/" . "$filename";

This is slightly odd.  The double quotes around $filename are 
unnecessary. See 'perldoc -q quoting'.  If you really insist 
on using the string concatenation operator, I'd probably 
write it as

    my $complete = $filedir . '/' . $filename;

but it's probably easiest to read and write as 

    my $complete = "$filedir/$filename";

Or, since I recommended getting rid of an extra variable, 

    my $complete = "$filedir/$logname";


> open(FH, "$complete") or die "Cannot find file $complete: $!";

The parentheses aren't really necessary when you use the 
low-precedence or (instead of ||).  

> while (<FH>)    {
> if ($_ =~ /^Job\sserver:\s+(.*)$/)              { $servername = $1;}

You can shorten this.  Pattern matches automatically look at $_
if you don't specify a string.

    if ( /^Job\sserver:\s+(.*)$/ )       { $servername = $1;}


> if ($_ =~ /^Job\sname:\s+(.*)$/)                { $jobname = $1;}
> if ($_ =~ /^Job\sstarted:\s+(.*)$/)             { $jobstarted = $1;}
> if ($_ =~ /^Media.*Label:\s+(.*)$/)             { $medialabel = $1;}
> if ($_ =~ /^Job\sended:\s+(.*)$/)               { $jobended = $1;}
> if ($_ =~ /^Job\scompletion\sstatus:\s+(.*)$/)  { $status = $1;}
> if ($_ =~ /^(\d+)\s+files\swere\sin.*$/)        {push(@inuse, $1);}

Later on you just add up the numbers collected in @inuse, so why not
eliminate the array and add them up as you go?

    if ( /^(\d+)\s+files\swere\sin.*$/)        { $inuse += $1 }

> if ($_ =~ /^(\d+)\s+items/)                     {push(@skipped, $1);}

Ditto....

> if ($_ =~ /^(\d+)\s+corrupt/)                   {push(@corrupt, $1);}
> if ($_ =~ /^.*item(.*\s-\sskipped.*)$/)         {push(@skippednames, $1);}

I'm not sure why this line is even here.  You don't use @skippednames
anywhere else, so why bother to collect it?

> $corrupt = &addnums(@corrupt);

No need for the & on the call to addnums().  If you don't need the effect
that the & gives you, don't bother with it.  See 'perldoc perlsub'.


> $skipped = &addnums(@skipped);
> $inuse = &addnums(@inuse);
> $logfile = $filename;
> 
>         }
> close(FH);
> print "$jobname,$jobstarted,$logfile,$servername\n";
> 
> my $dbh = DBI->connect( "dbi:Pg:dbname=backup", "[snip]", "[snip]") or die
> "Cannot connect to PGSQL DB\n"; 

I'd put this outside the foreach loop. Connect ONCE, then insert new
rows as necessary.


> my $sth = $dbh->prepare("INSERT INTO
> backuplogs(jobname,jobstarted,logfile,servername,status,inuse,jobended,skipped,corrupt,medialabel,testlogfile)
> VALUES
> ('$jobname','$jobstarted','$logfile','$servername','$status','$inuse','$jobended','$skipped','$corrupt','$medialabel','<a
> href=http://[snip]/oldlogs/$logfile>$logfile</a>')")
> or die "Error preparing row: $DBI::errstr\n"; $sth->execute() or die "
> $DBI::errstr\n"; 
> $dbh->disconnect;

And I'd move the disconnect to be after the foreach loop.

> $servername = "";
> $jobname = "";
> $jobstarted = "";
> $medialabel = "";
> $jobended = "";
> $status = "";

These re-initializations can be dropped when you confine the variables
to the smallest possible scope (as mentioned above).


> }
> 
> 
> sub addnums {
> my @number = @_;
> my $total;
> my $digit;
> foreach $digit (@number)        {
> $total += $digit;
> }
> 
> if (defined($total))    {
> $total  = $total;
> }
> else
> {$total = 0;}
> return $total;
> }

This sub becomes unnecessary if you accumulate the sum as you go.  But 
even so, it could be shorter (but not *too* short, I hope).

sub addnums {
    return 0 unless @_;
    my $total = 0;
    local $_;              # don't step on $_
    $total += $_ for @_;
    return $total;
}



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

Date: 15 Apr 2005 10:45:45 -0700
From: "nobull@mail.com" <nobull@mail.com>
Subject: Re: while loop into database insert not working
Message-Id: <1113587145.034531.53830@f14g2000cwb.googlegroups.com>


David K. Wall wrote:

> sub addnums {
>     return 0 unless @_;

Don't create unecessary special cases

>     my $total = 0;
>     local $_;              # don't step on $_

The for statement qualifier will localize $_ properly anyhow.

local($_) on the other hand, will do bad things if $_ happens to be an
alias for an element of a tied agregate.

>     $total += $_ for @_;
>     return $total;
> }


sub addnums {
     my $total = 0;
     $total += $_ for @_;
     return $total;
}



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

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


Administrivia:

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

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

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

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

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


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


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