[25487] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7731 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Feb 3 14:10:27 2005

Date: Thu, 3 Feb 2005 11:10:16 -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           Thu, 3 Feb 2005     Volume: 10 Number: 7731

Today's topics:
        Regex: Selectively choose result group? Prab_kar@hotmail.com
    Re: Regex: Selectively choose result group? <noreply@gunnar.cc>
    Re: Regex: Selectively choose result group? (Anno Siegel)
    Re: Sending/ Receiving SQL arguments to a subroutine <pat@patmail.com>
    Re: Sending/ Receiving SQL arguments to a subroutine <pat@patmail.com>
    Re: Sending/ Receiving SQL arguments to a subroutine <pat@patmail.com>
    Re: sharing perl scripts over domains asdfq213rr23we@yahoo.com
    Re: sharing perl scripts over domains (Anno Siegel)
    Re: sharing perl scripts over domains asdfq213rr23we@yahoo.com
    Re: sharing perl scripts over domains (Anno Siegel)
        Socket programming blunders.... <gdh@getrid.ofthese.parts.acentral.co.uk>
    Re: Socket programming blunders.... <jgibson@mail.arc.nasa.gov>
    Re: Socket programming blunders.... <nobull@mail.com>
    Re: Socket programming blunders.... xhoster@gmail.com
    Re: Whitespace mystery? <jgibson@mail.arc.nasa.gov>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 3 Feb 2005 08:44:06 -0800
From: Prab_kar@hotmail.com
Subject: Regex: Selectively choose result group?
Message-Id: <1107449046.507737.255760@f14g2000cwb.googlegroups.com>

Hi all,
I wonder if this can be done through regex matching.

I have a string of unix path, which can be either
   /home/MyAccount/Proj/SubProj   or
   /home/MyAccount/Proj

>From this I want to find
   1. the "Proj" and "SubProj" portion, when the string has a SubProj

   2. Just the "Proj" protion if there are no SubProjs.

I tried,
-------------------------------------
#!/usr/local/bin/perl -w

use strict ;
my $string = '/home/MyAccount/Proj/SubProj' ;
# my $string = '/home/MyAccount/Proj' ;

if ( $string =~ m/(.*)\/(.*)\/(.*)$/ )
{
   print "Project: $2 \t SubProject: $3\n" ;
}
-------------------------------------

It works fine when the string has Proj and SubProj but, when I
uncomment the later definition of string, I get MyAccount as my
project. Is there a way I can choose Project to $2 or $3 based on the
string?

Or, maybe I'm thinking of a wrong solution.

Could anyone give me some pointers on how to "get just the proj portion
when subproj is absent" from regex.( I dont really want to use splits
and substr.)

Thanks for your time,
Prabh



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

Date: Thu, 03 Feb 2005 18:05:01 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Regex: Selectively choose result group?
Message-Id: <36f3sbF505s4qU1@individual.net>

Prab_kar@hotmail.com wrote:
> I wonder if this can be done through regex matching.
> 
> I have a string of unix path, which can be either
>    /home/MyAccount/Proj/SubProj   or
>    /home/MyAccount/Proj
> 
>>From this I want to find
>    1. the "Proj" and "SubProj" portion, when the string has a SubProj
> 
>    2. Just the "Proj" protion if there are no SubProjs.
> 
> I tried,
> -------------------------------------
> #!/usr/local/bin/perl -w
> 
> use strict ;
> my $string = '/home/MyAccount/Proj/SubProj' ;
> # my $string = '/home/MyAccount/Proj' ;
> 
> if ( $string =~ m/(.*)\/(.*)\/(.*)$/ )
> {
>    print "Project: $2 \t SubProject: $3\n" ;
> }
> -------------------------------------
> 
> It works fine when the string has Proj and SubProj but, when I
> uncomment the later definition of string, I get MyAccount as my
> project. Is there a way I can choose Project to $2 or $3 based on the
> string?
> 
> Or, maybe I'm thinking of a wrong solution.

Try this:

     if ( $string =~ m#/MyAccount/([^/]+)(?:/(.+))?# ) {
         print "Project: $1" . ($2 ? "\tSubProject: $2" : '') . "\n" ;
     }

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


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

Date: 3 Feb 2005 17:29:08 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Regex: Selectively choose result group?
Message-Id: <cttn14$4ov$1@mamenchi.zrz.TU-Berlin.DE>

 <Prab_kar@hotmail.com> wrote in comp.lang.perl.misc:
> Hi all,
> I wonder if this can be done through regex matching.
> 
> I have a string of unix path, which can be either
>    /home/MyAccount/Proj/SubProj   or
>    /home/MyAccount/Proj
> 
> >From this I want to find
>    1. the "Proj" and "SubProj" portion, when the string has a SubProj
> 
>    2. Just the "Proj" protion if there are no SubProjs.
> 
> I tried,
> -------------------------------------
> #!/usr/local/bin/perl -w
> 
> use strict ;
> my $string = '/home/MyAccount/Proj/SubProj' ;
> # my $string = '/home/MyAccount/Proj' ;
> 
> if ( $string =~ m/(.*)\/(.*)\/(.*)$/ )
> {
>    print "Project: $2 \t SubProject: $3\n" ;
> }
> -------------------------------------
> 
> It works fine when the string has Proj and SubProj but, when I
> uncomment the later definition of string, I get MyAccount as my
> project. Is there a way I can choose Project to $2 or $3 based on the
> string?
> 
> Or, maybe I'm thinking of a wrong solution.

Not exactly wrong, but why insist in using a single regex to do
it all?  First isolate the part of the path name that interests you:

    m{^/home/MyAccount/(.*)/

That captures everything after "/home/MyAccount/".  Splitting on '/' will
give you a two-element list if a subproject is present, otherwise just one
element.  So:

    my ( $pro, $sub_pro);
    ( $pro, $sub_pro) = split m{/} for $string =~ m{^/home/MyAccount/(.*)};

$pro will always be set when a project specification is found at all.
$sub_pro will be the subproject if there is one, undefined otherwise.

Anno


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

Date: Thu, 03 Feb 2005 17:25:11 GMT
From: "PatrickG" <pat@patmail.com>
Subject: Re: Sending/ Receiving SQL arguments to a subroutine
Message-Id: <X7tMd.264$Nc.112@tornado.tampabay.rr.com>

You have some good suggestions and questions - I wish that I had answers for
them! Most of my code is what I gleened from threads I found searching the
PerlMonks archives. I can't defend the logic, esp. since I probably
bastardized it somewhat to fit my needs (or what I thought I needed to do).

You were right on though... I have extensive experience with korn/ bourne
shell scripting, but now that I'm in a Win environment, I'm trying to learn
Perl for about a week now. Ahh.... the growing pains.

Thanks for your suggestions - I've got a few things to RTFM on now!


<nobull@mail.com> wrote in message
news:1107378439.872156.208730@l41g2000cwc.googlegroups.com...
> PatrickG wrote:
> > Just Another Perl Newbie here - any help much appreciated...
> >
> > I'm trying to write a subroutines that will call the $dbh,
> > prepare, and execute SQL statements so that I don't have to
> > keep rewriting the same code.
>
> A laudable aim but DBI provides those already, you know.
> (selectall_arrayref etc)
>
> > The problem is that each call to the subroutine(s) will
> > have a different number of arguments passed and results
> > returned. Is there a way to call a subroutine not knowing
> > how many arguments to expect??
>
> This is the default behaiour in Perl unless you specify a prototype to
> disable it.
>
> > For example: I want to get a list of all the tables for the current
> db user
> > and then truncate those tables.
> >
> > My code for a known number of arguments is as follows:
> >
> > my $sqlTables = qq{ SELECT table_name FROM user_tables WHERE
> table_name not
> > like 'BIN%'};
> >
> > my @tables = &getObj($dbh, $sqlTables); # Call subroutine to get the
> list of
> > user tables
> > &runSQL($dbh, q{truncate table }, \@tables, q{}); # Call subroutine
> to
> > truncate tables
> >
> > $dbh->disconnect();
> >
> > sub getObj ($$) { ##################################################
> >
> >   my ($DBH, $SQL) = @_;
> >   my ($obj);
> >   my (@result) ;
> >   my $sth = $DBH->prepare($SQL);
> >   $sth->execute();
> >   $sth->bind_columns( undef, \$obj );
> >
> >   while( $sth->fetch() ) {
> >      push @result, $obj;
> >   }
> >   #print "Result: \n @result\n";
> >   $sth->finish();
> >   return @result;
> >
> > }
>
> The above is basically selectcol_arrayref - except it returns a list
> rather than an arrayref.  Generally it is better to return arrays from
> subroutines as references rather than unrolling them into lists.  (See
> 'perldoc perlsub').
>
> > sub runSQL ($$\@) {
> ######################################################
> >
> >   my ($DBH, $preObj, $ref1, $postObj) = @_;
>
> Your prototype has only 3 arguments yet here you expect 4.  $ref1 is a
> bad name for a variable.
>
> >   my @object = @$ref1;
>
> What do you imagine is the purpose of the @object variable?
>
> >   my $sth;
> >   my $sql;
>
> You are suffering from premature declaration.  You should declare all
> variables in the smallest applicable scope.
>
> >   foreach (@object) {
>
> Why didn't you just iterate over @$ref1 directly?
>
> >    $sql = $preObj.$_.$postObj;
>
> This is the first place you use $sql so this is the place you should
> declare it.
>
> >    #print "$sql\n";
> >    $sth = $DBH->prepare($sql);
>
> This is the first place you use $sth so this is the place you should
> declare it.
>
> >    $sth->execute();
> >    $sth->finish();
> >   }
> > }
>
> Since you are not fetching anything it would be simpler to use the do()
> method.
>
> $sql->do("$preObj$_$postObj") for @$ref1;
>
> >
> > The above works fine for that purpose (truncating tables), but I also
> > want to disable constraints, which means that for the getObj routine,
> > I will need to return 2 results (table_name and constraint_name), and
> > pass those two arguments to runSQL. For another situation, I may need
> > more arguments passed/ returned. Any idea how to have a single
> routine
> > to do this without  knowing the number of args??
>
> The general solution to cope with variable number of things is to pass
> about array reference and iterate over arrays.  If you want a list of
> tuples the natural data type is an array of arrays or an array of
> hashes.  DBI's selectall_arrayref can return the rowset result of an
> SQL statement in either of these forms.
>
> > I was thinking to concat the args with a delimiter
>
> Oh for $DEITY's sake no! This is Perl not bourne shell!  Perl has higer
> level data strucutres (arrays and hashes) - use them, do not simulate
> them with string manipulations.
>
> > TIA for any advice!
>
> Less is more.
>




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

Date: Thu, 03 Feb 2005 17:25:43 GMT
From: "PatrickG" <pat@patmail.com>
Subject: Re: Sending/ Receiving SQL arguments to a subroutine
Message-Id: <r8tMd.265$Nc.18@tornado.tampabay.rr.com>

Uhhh... I didn't realize I was ;)


"Sherm Pendley" <spamtrap@dot-app.org> wrote in message
news:ev-dnShkDJiB2pzfRVn-sQ@adelphia.com...
> You've gotten some good suggestions, but I had to ask:
>
> PatrickG wrote:
>
> > my @tables = &getObj
> ...
> > &runSQL
>
> > sub getObj ($$)
> ...
> > sub runSQL ($$\@)
>
> What's the point in prototyping subs, if you're simply going to tell Perl
to
> ignore the prototypes?
>
> sherm--
>
> -- 
> Cocoa programming in Perl: http://camelbones.sourceforge.net
> Hire me! My resume: http://www.dot-app.org




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

Date: Thu, 03 Feb 2005 17:20:53 GMT
From: "PatrickG" <pat@patmail.com>
Subject: Re: Sending/ Receiving SQL arguments to a subroutine
Message-Id: <V3tMd.263$Nc.253@tornado.tampabay.rr.com>

Thanks for the tip - looks like that might be a viable option!


"Jim Gibson" <jgibson@mail.arc.nasa.gov> wrote in message
news:020220051255338425%jgibson@mail.arc.nasa.gov...
> In article <gfaMd.17141$Q72.1859@tornado.tampabay.rr.com>, PatrickG
> <pat@patmail.com> wrote:
>
> > Just Another Perl Newbie here - any help much appreciated...
> >
> > I'm trying to write a subroutines that will call the $dbh, prepare, and
> > execute SQL statements so that I don't have to keep rewriting the same
code.
> > The problem is that each call to the subroutine(s) will have a different
> > number of arguments passed and results returned. Is there a way to call
a
> > subroutine not knowing how many arguments to expect??
>
> Yes. See perldoc perlsub and don't use prototypes to declare the number
> of arguments. Then, any number of arguments you pass will show up in
> the @_ array. $#_ gives the number of arguments, but you can just shift
> arguments from the front of the @_ array until it is empty.
>
> For example, you can write your subroutine thusly if you are always
> passing it two arguments followed by zero or more optional arguments:
>
> sub getObj
> {
>    my $DBH = shift;
>    my $SQL = shift:
>    while( @_ ) {
>       my $table = shift;
>       # process table
>    }
> }
>
>
> ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet
News==----
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
> ---= East/West-Coast Server Farms - Total Privacy via Encryption =---




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

Date: 3 Feb 2005 06:05:03 -0800
From: asdfq213rr23we@yahoo.com
Subject: Re: sharing perl scripts over domains
Message-Id: <1107439503.379240.317880@f14g2000cwb.googlegroups.com>

If I wanted to use the symbolic link solution, how would that be
carried out to make the domain2's cgi-bin dir be a virtual mirror of
the domain1's cgi-bin? Thanks !



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

Date: 3 Feb 2005 18:40:56 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: sharing perl scripts over domains
Message-Id: <cttr7o$6au$4@mamenchi.zrz.TU-Berlin.DE>

 <asdfq213rr23we@yahoo.com> wrote in comp.lang.perl.misc:

Give an attribution and quote some context when you reply.

> If I wanted to use the symbolic link solution, how would that be
> carried out to make the domain2's cgi-bin dir be a virtual mirror of
> the domain1's cgi-bin? Thanks !

That's off topic, and you have been told so.  Why do you insist in
discussing this in clpm?

Anno


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

Date: 3 Feb 2005 10:43:41 -0800
From: asdfq213rr23we@yahoo.com
Subject: Re: sharing perl scripts over domains
Message-Id: <1107456221.602219.51930@f14g2000cwb.googlegroups.com>

I tried this:

in the shell I went to /home/httpd/vhosts/domain2 and entered:

ln -s /home/httpd/vhosts/domain1

thinking this would create a mirror of anything under domain1

Didnt succeed..



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

Date: 3 Feb 2005 18:48:37 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: sharing perl scripts over domains
Message-Id: <cttrm5$6au$5@mamenchi.zrz.TU-Berlin.DE>

 <asdfq213rr23we@yahoo.com> wrote in comp.lang.perl.misc:
> I tried this:
> 
> in the shell I went to /home/httpd/vhosts/domain2 and entered:
> 
> ln -s /home/httpd/vhosts/domain1
> 
> thinking this would create a mirror of anything under domain1

Then read the man page of "ln" instead of posting to an inappropriate
newsgroup.  Stop it already!

Anno


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

Date: Thu, 03 Feb 2005 15:45:21 +0000
From: Gavin Hamill <gdh@getrid.ofthese.parts.acentral.co.uk>
Subject: Socket programming blunders....
Message-Id: <42024710$0$19161$ed9e5944@reading.news.pipex.net>

Hullo - I'm trying to communicate with our office PBX, and whilst I have the
full protocol documentation, I'm failing on basic socket programming...
I've ripped most of an example from the Perl Cookbook and modified it so I
have a basic interactive client that works fine:

#!/usr/bin/perl

# Message format is 32-bit little-endian length followed by
# the message, terminated with 0x0d0a incoming and 0x0d outgoing.

use IO::Socket;
$sock = new IO::Socket::INET (PeerAddr => '10.0.0.246',
                              PeerPort => 4000,
                              Proto    => 'tcp',
                             );
die "Socket could not be created. Reason: $!\n" unless $sock;

$sock->autoflush(1);
print STDERR "[Connected]\n";
# split the program into two processes, identical twins
die "can't fork: $!" unless defined($kidpid = fork());

if ($kidpid) {
    while (sysread($sock, $header,4) == 4) {
        # We got a 4-byte header!
        $paylen= unpack("V",$header);
        print "Got message length of $paylen payload bytes\n";

        if(sysread($sock, $payload, $paylen) == $paylen) {
        print $payload;
        }
        else {
        print "ERR! Bytes read didn't match value in header - received this:
-=$payload=-\n";
        }
    }
    kill("TERM" => $kidpid);        # send SIGTERM to child
}
else {
    # Magic login sequence
    print $sock "\x02\x00\x00\x00\x87\x00";
    while (defined ($line = <STDIN>)) {
        chop $line; # Remove the 0x0a LF
        print $sock pack("V",length($line)+1);
        print $sock $line . "\x0d"; # Suffix a CR
    }
}
exit;


My problem is this:

The PBX will be sending me events, and I will need to send responses to the
PBX, parse the responses and perhaps send different replies...

If this script has fork()ed ... how on earth do I get the two halves to talk
to each other? named pipes? UNIX domain sockets?

After much reading I've drawn a blank - does anyone have suggestions or some
short code fragments I could work from?

Cheers,
Gavin,.



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

Date: Thu, 03 Feb 2005 09:28:16 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Socket programming blunders....
Message-Id: <030220050928169975%jgibson@mail.arc.nasa.gov>

In article <42024710$0$19161$ed9e5944@reading.news.pipex.net>, Gavin
Hamill <gdh@getrid.ofthese.parts.acentral.co.uk> wrote:

[problem description snipped]

[program snipped]

> 
> If this script has fork()ed ... how on earth do I get the two halves to talk
> to each other? named pipes? UNIX domain sockets?

Yes and yes. Also shared memory might work.

> 
> After much reading I've drawn a blank - does anyone have suggestions or some
> short code fragments I could work from?

perldoc perlipc


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---


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

Date: 3 Feb 2005 10:00:51 -0800
From: "nobull@mail.com" <nobull@mail.com>
Subject: Re: Socket programming blunders....
Message-Id: <1107453651.310978.219070@f14g2000cwb.googlegroups.com>

Gavin Hamill wrote:

> I'm trying to communicate with our office PBX

> I've ripped most of an example from the Perl
> Cookbook and modified it so I have a basic interactive
> client that works fine:

> if ($kidpid) {
>     while (sysread($sock, $header,4) == 4) {
>     }
> } else {

>     while (defined ($line = <STDIN>)) {

>         print $sock $line . "\x0d"; # Suffix a CR
>     }
> }

> The PBX will be sending me events, and I will need to send
> responses to the PBX, parse the responses and perhaps send
> different replies...
>
> If this script has fork()ed ... how on earth do I get the two halves
to talk
> to each other? named pipes? UNIX domain sockets?

Why did you fork?  It make be a quick way to handle an interactive
client where the two directions don't interact but as soon as you need
interaction you run right back into the problems that you forked to
avoid - and then some.

Sounds to me like you may be better off not forking so.  If you always
wait for a message from the PBX and then respond and yo can assume that
the PBX will read that response before you need to process the next
requestthen life is simple.  If not you probably need to go for an
event driven approach like IO::Select or POE.



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

Date: 03 Feb 2005 18:13:30 GMT
From: xhoster@gmail.com
Subject: Re: Socket programming blunders....
Message-Id: <20050203131330.817$Ka@newsreader.com>

Gavin Hamill <gdh@getrid.ofthese.parts.acentral.co.uk> wrote:
> Hullo - I'm trying to communicate with our office PBX, and whilst I have
> the full protocol documentation, I'm failing on basic socket
> programming... I've ripped most of an example from the Perl Cookbook and
> modified it so I have a basic interactive client that works fine:

Why did Perl Cookbook tell you to fork?  Are there non-forking examples
in the Cookbook?

Xho

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


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

Date: Thu, 03 Feb 2005 09:36:38 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Whitespace mystery?
Message-Id: <030220050936380079%jgibson@mail.arc.nasa.gov>

In article <1107399716.253060.180880@z14g2000cwz.googlegroups.com>,
<squash@peoriadesignweb.com> wrote:

> Thanks, I have figured it out. I added in this line to remove the ASCII
> 160 characeter for &nbsp:
> 
> $string =~ s/\xA0//; ## Remove html &nbsp blank space (A0 hex for 160)
> Thx!
> 

The tr operator might be more efficient than a substitution for this
purpose. Plus, you can delete a range of characters:

$string =~ tr/\x80-\xff//d;


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---


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

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


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