[31473] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2725 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 16 21:30:20 2009

Date: Wed, 16 Dec 2009 18:29:46 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 16 Dec 2009     Volume: 11 Number: 2725

Today's topics:
    Re: 2-headed strings and RExes <ben@morrow.me.uk>
        Closing a DBI instance <tim@johnsons-web.com>
    Re: Closing a DBI instance <ben@morrow.me.uk>
    Re: FAQ 5.3 How do I count the number of lines in a fil <brian.d.foy@gmail.com>
    Re: push integers from strings sln@netherlands.com
    Re: push integers from strings sln@netherlands.com
    Re: push integers from strings sln@netherlands.com
    Re: push integers from strings <jl_post@hotmail.com>
    Re: Searching all instances of a pattern across multi-l sln@netherlands.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 16 Dec 2009 23:30:39 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: 2-headed strings and RExes
Message-Id: <v57ov6-o4h1.ln1@osiris.mauzo.dyndns.org>


Quoth Ilya Zakharevich <nospam-abuse@ilyaz.org>:
> On 2009-12-10, Ben Morrow <ben@morrow.me.uk> wrote:
> > There is now. I submitted a patch to add 'qr' overloading to 5.12, which
> > is called when an overloaded object is used on the RHS of =~ or is
> > interpolated into a regex. Since 5.12 has true REGEX SVs, it seemed
> > silly not to have a corresponding 'type-cast' overload.
> 
> Ah...  This is probably the reason why FreezeThaw is failing its tests
> on 5.11...  Whoever added "true REx" SV did not fix the modules broken
> by this change...

Yes. Nick Clark did a lot of rejigging of the SvTYPE assignments. As of
5.10, PVBMs are now implemented as SVt_PVGVs (IMHO this was a mistake,
but it's done now); as of 5.12, RVs are implemented as SVt_IVs (mostly;
some are PVMGs, as they always were) with SvROK set and there is a new
SVt_REGEX used for qr//. There is also a new SVt_BIND, which is
currently unused but is intended for read-only aliases.

As for fixing the modules: with the number of modules on CPAN, this is
impossible. People writing XS modules that grovel around in perl's guts
are expected to keep up with p5p. I believe some effort is made to smoke
CPAN and fix breakages (certainly there was a big push to get the CPAN
smokes clean before 5.10) but in the end p5p can't fix everything.

Ben



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

Date: Wed, 16 Dec 2009 19:13:28 -0600
From: Tim Johnson <tim@johnsons-web.com>
Subject: Closing a DBI instance
Message-Id: <slrnhiivlq.hbs.tim@bart.johnson.com>

Platform: Red Hat Linux server, perl 5.8.8, mysql ver 5.0.45

FYI: I am an experienced programmer but _not_ a perl programmer.

I am trouble-shooting a problem on this server where mysql connections
do not seem to be closed by cron jobs

Consider the following perl code snippet in a stand-alone script with
executable privileges that is called by cron:

my $data_Source = "DBI:mysql:".$DATABASE .":".$HOSTNAME;
my $dbh = DBI->connect($data_Source,$USERNAME,$PASSWORD)
    or die "$DBI::errstr\n";

To properly disconnect, should I not see something like the following:
$dbh->disconnect 
somewhere in this script?

TIA

-- 
Tim 
tim@johnsons-web.com
http://www.akwebsoft.com


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

Date: Thu, 17 Dec 2009 01:59:39 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Closing a DBI instance
Message-Id: <btfov6-gsh1.ln1@osiris.mauzo.dyndns.org>


Quoth tim@johnsons-web.com:
> Platform: Red Hat Linux server, perl 5.8.8, mysql ver 5.0.45
> 
> FYI: I am an experienced programmer but _not_ a perl programmer.

What language(s) do you know? It is useful when explaining things to
know which analogies will be useful and which confusing.

> I am trouble-shooting a problem on this server where mysql connections
> do not seem to be closed by cron jobs
> 
> Consider the following perl code snippet in a stand-alone script with
> executable privileges that is called by cron:
> 
> my $data_Source = "DBI:mysql:".$DATABASE .":".$HOSTNAME;
> my $dbh = DBI->connect($data_Source,$USERNAME,$PASSWORD)
>     or die "$DBI::errstr\n";
> 
> To properly disconnect, should I not see something like the following:
> $dbh->disconnect 
> somewhere in this script?

Not necessarily. $dbh is a DBI::dbh object, and has a DESTROY method
(like a C++ destructor) that will call ->disconnect when the object goes
out of scope. At that point any transactions in prgress will be rolled
back, so you *should* see a ->commit in the appropriate place (assuming
you are using transactions).

Ben



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

Date: Wed, 16 Dec 2009 15:10:47 -0600
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 5.3 How do I count the number of lines in a file?
Message-Id: <161220091510475551%brian.d.foy@gmail.com>

In article
<48ffe7ef-f5da-4ec4-a7c2-e2e8fa19c201@u36g2000prn.googlegroups.com>,
C.DeRykus <derykus@gmail.com> wrote:

> > 5.3: How do I count the number of lines in a file?


> Wasn't one of these one-liners included once?
> 
> perl -lne '}print $.;{'    file   # Abigail's trick
> perl -lne 'END {print $.}' file

Those seem like worthy additions. I'll add them to my queue.


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

Date: Wed, 16 Dec 2009 13:19:21 -0800
From: sln@netherlands.com
Subject: Re: push integers from strings
Message-Id: <mhjii5tesli1o4ntaoulgblhrpkcle2aoe@4ax.com>

On Tue, 15 Dec 2009 20:27:37 -0500, monkeys paw <user@example.net> wrote:

>Following code has quoted numbers in it, but i want
>just integers. What's easiest way?
>
[snip]
>            push @allnums, $numstr;
                             ^^
                           1*$numstr;

This works because of context, didn't read other responces
which I will later.
-sln


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

Date: Wed, 16 Dec 2009 13:53:40 -0800
From: sln@netherlands.com
Subject: Re: push integers from strings
Message-Id: <jikii59rbe1tkp6u76dh03vk4ps058r5rk@4ax.com>

On Wed, 16 Dec 2009 00:54:09 -0500, monkeys paw <user@example.net> wrote:

>John Bokma wrote:
>> monkeys paw <user@example.net> writes:
>> 
>>> Following code has quoted numbers in it, but i want
>>> just integers. What's easiest way?
>> 
>>>     } else {
>>>            push @allnums, $numstr;
>> 
>>              push @allnums,$numstr * 1
>> 
>>>     }
>>> }
>> 
>> Note that instead of
>> 
>>> my 
>
>
>Users enter it this way, if i changed the string
>as you suggest i guess i could use an eval:
>
>$nums ='30-32,34,50,54-56';
>$nums =~ s/\-/\s\.\.\s/;  # Spaces important
>
>now $nums = 30 .. 32,34,50,54 .. 156
>
>How could i eval $nums into an array so it
>would equal 30,31,32,34, etc...
>
>eval { @allnums = $nums }; # This definitely won't work
>
>> 
>> and processing this, you could do:
>> 
>> my @allnums = (30..32,34,50,54..56);
>> 
>> if you're just looking for an easier notation.
>> 

Eval would work. If you don't care about validation,
I guess you could do it a couple of ways:

use strict;
use warnings;

my $input = '30-32,34,50,54-56';
$input =~ s/-/../g;

#my @allnums;
#push @allnums, eval $input;
# or
 
my @allnums = (eval $input);

use Data::Dumper;die 'DDDEBUG' .  Dumper(\@allnums);

__END__
DDDEBUG$VAR1 = [
          30,
          31,
          32,
          34,
          50,
          54,
          55,
          56
        ];

-sln


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

Date: Wed, 16 Dec 2009 14:19:55 -0800
From: sln@netherlands.com
Subject: Re: push integers from strings
Message-Id: <ovmii59jed7mm083t5r05l70kemjb17hjm@4ax.com>

On Wed, 16 Dec 2009 13:53:40 -0800, sln@netherlands.com wrote:

>On Wed, 16 Dec 2009 00:54:09 -0500, monkeys paw <user@example.net> wrote:
>
>my @allnums = (eval $input);
                ^^
Note that eval's can execute code, so user input can cause harm:
  my $input = 'print "shit happens\n";30-32,34,50,54-56';
  my @allnums = (eval $input);

*> shit happens

-sln


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

Date: Wed, 16 Dec 2009 16:24:48 -0800 (PST)
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: push integers from strings
Message-Id: <4f93cb2c-3dd1-47d8-91f4-e09eb46ec60a@u18g2000pro.googlegroups.com>

On Dec 15, 6:27=A0pm, monkeys paw <u...@example.net> wrote:
> Following code has quoted numbers in it, but i want
> just integers. What's easiest way?
>
> # Translate the var $nums into an integer list
>
> use strict;
> my @allnums;
> my $nums =3D'30-32,34,50,54-56';
> my @nums =3D split /,/, $nums;
>
> for my $numstr (@nums) {
> =A0 =A0 =A0if ($numstr =3D~ /-/) {
> =A0 =A0 =A0 =A0 my ($lower, $upper) =3D split /-/, $numstr ;
> =A0 =A0 =A0 =A0 for my $anum ($lower .. $upper) {
> =A0 =A0 =A0 =A0 =A0 =A0 push @allnums, $anum;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0} else {
> =A0 =A0 =A0 =A0 =A0 =A0 push @allnums, $numstr;
> =A0 =A0 =A0}
>
> }
>
> use Data::Dumper;die 'DDDEBUG' . =A0Dumper(\@allnums);
>
> #Numbers 34 and 50 below have quote around them but i dont want it.
>
> DDDEBUG$VAR1 =3D [
> =A0 =A0 =A0 =A0 =A0 =A030,
> =A0 =A0 =A0 =A0 =A0 =A031,
> =A0 =A0 =A0 =A0 =A0 =A032,
> =A0 =A0 =A0 =A0 =A0 =A0'34',
> =A0 =A0 =A0 =A0 =A0 =A0'50',
> =A0 =A0 =A0 =A0 =A0 =A054,
> =A0 =A0 =A0 =A0 =A0 =A055,
> =A0 =A0 =A0 =A0 =A0 =A056
> =A0 =A0 =A0 =A0 =A0];


   The easy answer is to say:  Don't use Data::Dumper.  There's no way
to tell if a number is a string (or rather, it's difficult) if you
don't use Data::Dumper;

   Rewrite your die() line like this:

die "DDDEBUG: @allnums\n";

   Seriously, apart from using Data::Dumper, there is pretty much no
simple or straightforward way to tell if a number is a string.  Your
code won't even know.  So you won't have to worry about if your code
will fail with a "stringed" number, as all strings that can be numbers
are converted seamlessly when used as a number.

   But if you still want Data::Dumper to refuse to see the number as a
string, you can use this line:

   $_ +=3D 0  foreach @allnums;  # "numerate" each element

   Remember, unless you print out Data::Dumper's output, your program
won't behave any differently with that line or without.  And since you
seem to be using Data::Dumper just for inspection purposes, there
doesn't seem to be any reason to "numerate" your strings.

   So I have to ask:  Why is it so important that Data::Dumper not
return the numbers as strings, when the rest of Perl can't even tell?

   -- Jean-Luc


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

Date: Wed, 16 Dec 2009 12:29:07 -0800
From: sln@netherlands.com
Subject: Re: Searching all instances of a pattern across multi-lines
Message-Id: <kefii5tjb7l8u7n69nprd6ucomk4msvcmo@4ax.com>

On Sun, 13 Dec 2009 15:43:13 -0800, sln@netherlands.com wrote:

>On Sun, 13 Dec 2009 13:22:15 -0800 (PST), laredotornado <laredotornado@zipmail.com> wrote:
>
>>Hi,
>>
>>I'm using Perl 5.8.8 on Mac 10.5.6.  I found this script online for
>>matching a pattern across multiple lines.  The problem is, it only
>>prints out one instance of the expression, and I would like it to
>>print out all instances.  What can I change so that it will print out
>>all instances?
>>
>>
>>#!/usr/bin/perl
>>use strict;
>>use warnings;
>>
>>open(my $file, "<", "myfile.txt")
>>    or die "Can't open file: $!";
>>my $text = do { local $/; <$file> };
>>
>>if ($text =~ /(<\s*script[^<]*>.*?<\/script>)/gs) {
>>    print $1;
>>}
>>
>>
>>
>>Thanks,  - Dave
>
>'while()' should work as others have said.
>
>The above regex should take into account these forms:
> <tag>
> <tag/>
> <tag attr> content </tag>
> <tag attr/>
>
>Try this. It takes into account all the above forms
>plus handles attributes fairly well, without the need for
>[^<]*, where the actual character '<' can exist in the value
>part. Handling attrib/vals correctly and taking acccount of all
>valid forms are important, it all goes toward partitioning the
>data.
>
>Also, this is a complex parse. It includes multiple atomic
>markup units, which is debatably <tag> style and content.
>Content being the current state that is not markup.
>Ideally, the unit is parsed to find the start element 'script',
>recording is turned on, then off at the end element 'script'.
>
>As it is now, the regex you are using won't correctly parse the
>$text string below.
>

Late addition:

But alas, no simple regex is going to handle nesting correctly
unless there is recursion. Below handles recursive tags, but
requires Perl 5.10 or better.

usage: html_rx.pl  [<tag name> [file name]]  - default, if no params

Cmd line examples:

*> html_rx.pl form junk.html    - Finds 'form' blocks in html file

*> html_rx.pl script junk.html  - Finds 'script' blocks in html file

*> html_rx.pl "(?i)script|object" junk.html
      - Finds either 'script' or 'object' blocks, case insensitive
        (good little markups will be properly nested, ie. those that have terminators)

-sln
-------------

*> perl html_rx.pl "(?i)script|object"

File name:  __DATA__
Tag  name:  (?i)script|object

-------------------- ** type 1
<script attr = "asdf" attr = 'wafsd' />
-------------------- ** type 2
<script>
    use strict;
    use warnings;
    print "hello world, I'm a <tag>\n";
    <script a = "it's" b= 'terminated'/>
    <object></object>
    <script>
        // comment me out c++ style
        /* now c style
        */
    </script>
</script>
-------------------- ** type 1
<script />

====================
Summary
 File name:  __DATA__
 Tag  name:  (?i)script|object
 type1  <tag, tag-attr />       = 2
 type2  <tag, tag-attr>..</tag> = 1

*>
-------------

## html_rx.pl
## -sln

use strict;
use warnings;

require 5.010_000;

# usage: html_rx.pl  [<tag name> [file name]]
# ---------------------------------------------
my ($tag,$fname) = @ARGV;
my $text;

$tag = 'script' unless defined $tag;
if (defined $fname) {
	open my $fh, '<', $fname or die "Can't open file '$fname' : $!";
	$text = join '',<$fh>;
	close $fh;
} else {
	$fname = '__DATA__';
	$text = join '',<DATA>;
}

my ($terminated, $open, $close) =
(
  qr {<  (?:$tag) (?:\s+[^>]*)? />}x,
  qr {<  (?:$tag) (?:\s+[^>]*? \s*[^/]> | \s*>) }x,
  qr {</ (?:$tag) \s*> }x
);

my $rx = qr {
   (
     (?: $terminated )        # <tag [attr] />
   )
  |                       # OR ...
   (
     (?: $open )             # <tag [attr] >
        (?:
             (?: (?!$open|$close) . )++  # possessive
           |
             (?2)             # recurse group 2
        )*
     (?: $close )             # </tag>
   )
}xs;

print "\n",<<INFO;
File name:  $fname
Tag  name:  $tag\n
INFO

my ($cnt1,$cnt2) = (0,0);

while ( $text =~ /$rx/g) {
    print '-'x20;
    if (defined $1) {
	print " ** type 1\n",$1,"\n" ;
	$cnt1++;
    } else {
	print " ** type 2\n",$2,"\n" ;
	$cnt2++;
    }
}

print "\n",'='x20,"\nSummary\n",<<SUMMARY;
 File name:  $fname
 Tag  name:  $tag
 type1  <tag, tag-attr />       = $cnt1
 type2  <tag, tag-attr>..</tag> = $cnt2\n
SUMMARY

__DATA__

<script attr = "asdf" attr = 'wafsd' />
</script>
<script>
<script>
<script>
    use strict;
    use warnings;
    print "hello world, I'm a <tag>\n";
    <script a = "it's" b= 'terminated'/>
    <object></object>
    <script>
	// comment me out c++ style
	/* now c style
	*/
    </script>
</script>
<script>
<script />
<notme>



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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 V11 Issue 2725
***************************************


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