[19000] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1195 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 26 11:05:32 2001

Date: Tue, 26 Jun 2001 08:05:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <993567909-v10-i1195@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Tue, 26 Jun 2001     Volume: 10 Number: 1195

Today's topics:
        Browser interface to Perl program? <newspost@coppit.org>
    Re: CGI string parameter passing <pne-news-20010626@newton.digitalspace.net>
    Re: CGI string parameter passing <gnarinn@hotmail.com>
    Re: CGI string parameter passing <flavell@mail.cern.ch>
    Re: CGI::CARP (carpout) or (fatalsToBrowser) ? <davsoming@lineone.net>
    Re: counting matches <Jenda@Krynicky.cz>
        First CGI steps <daniel.hendrickx@alcatel.be>
    Re: Help with a regular expression <Jenda@Krynicky.cz>
    Re: How do I cope with pound (currency) sign? <SEE_MY_SIG@nospam.demon.co.uk>
    Re: How do I cope with pound (currency) sign? <flavell@mail.cern.ch>
    Re: How I can use CVS? (isterin)
    Re: How to get env $HOME use perl (Rafael Garcia-Suarez)
    Re: Perl *is* strongly typed (was Re: Perl description) <newspost@coppit.org>
    Re: perl compilation on HPUX-11 (Richard J. Rauenzahn)
    Re: perl compilation on HPUX-11 (Richard J. Rauenzahn)
    Re: perl compilation on HPUX-11 (Jim Garner)
        Perl to Convert Code (Buck Turgidson)
    Re: Perl to Convert Code <pne-news-20010626@newton.digitalspace.net>
    Re: Problem with Archive::Tar <djberge@uswest.com>
    Re: Problem with Archive::Tar <djberge@uswest.com>
        RE problem <joonas.paalasmaa@nokia.com>
    Re: RE problem (Tad McClellan)
    Re: RE problem <ren@tivoli.com>
    Re: RE problem <krahnj@acm.org>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 26 Jun 2001 10:52:30 -0400
From: David Coppit <newspost@coppit.org>
Subject: Browser interface to Perl program?
Message-Id: <Pine.SUN.4.33.0106261044500.433-100000@mamba.cs.Virginia.EDU>

I'd like add a cross-platform GUI interface to my Perl program.
Perl/Tk is... well... ugly. AFAICT, there's no way to get Perl/Tk to
use platform-specific widgets.

So... I'd like to use an HTML interface. Unfortunately, I can't assume
that a web server is running on the machine. If I want an HTML
interface, it seems to me that I need to embed a web server in my
application (**shiver**). Can anyone think of another solution? If
not, has anyone written a simple embedded web server with a CGI
interface?

Thanks,
David



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

Date: Tue, 26 Jun 2001 12:06:12 +0200
From: Philip Newton <pne-news-20010626@newton.digitalspace.net>
Subject: Re: CGI string parameter passing
Message-Id: <65ngjt83o89p2es8tsjf14l4jijfs9nq1t@4ax.com>

On Tue, 26 Jun 2001 10:58:06 +0200, Marvin <glodalec@yahoo.com> wrote:

> Is there any CGI function which translate regular string "bla bla bla" 
> into "bla+bla+bla". There is a lot of conversion needed (some characters 
> need to be translated into %<code> etc..), so I would use regular exp. 
> substitute as a final solution.

I had a quick look through CGI.pm documentation generated from the POD,
but didn't find anything likely. http://stein.cshl.org/WWW/software/CGI/
showed the existence of the 'escape' function (which must, however, be
imported explicitly if desired), which however will turn a space into
'%20' rather than '+' (the two should be equivalent in an URI AIUI).

And there's URI::Escape, which also has an uri_escape sub which does
something similar to CGI.pm's escape.

Of course, you could always tr/ /+/ your input before running it through
uri_escape or escape if you prefer plus signs for spaces.

Cheers,
Philip
-- 
Philip Newton <nospam.newton@gmx.li>
Yes, that really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.


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

Date: Tue, 26 Jun 2001 10:09:56 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: CGI string parameter passing
Message-Id: <993550196.126898195128888.gnarinn@hotmail.com>

In article <65ngjt83o89p2es8tsjf14l4jijfs9nq1t@4ax.com>,
Philip Newton  <nospam.newton@gmx.li> wrote:
>On Tue, 26 Jun 2001 10:58:06 +0200, Marvin <glodalec@yahoo.com> wrote:
>
>> Is there any CGI function which translate regular string "bla bla bla" 
>> into "bla+bla+bla". There is a lot of conversion needed (some characters 
>> need to be translated into %<code> etc..), so I would use regular exp. 
>> substitute as a final solution.
>
>I had a quick look through CGI.pm documentation generated from the POD,
>but didn't find anything likely. http://stein.cshl.org/WWW/software/CGI/
>showed the existence of the 'escape' function (which must, however, be
>imported explicitly if desired)

Or just called as CGI::escape($string);

Import can be nice, but why clutter your namespace when 5 extra chars
will do?

gnari



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

Date: Tue, 26 Jun 2001 12:43:26 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: CGI string parameter passing
Message-Id: <Pine.LNX.4.30.0106261238530.6364-100000@lxplus013.cern.ch>

On Tue, 26 Jun 2001, Philip Newton wrote:

> Of course, you could always tr/ /+/ your input before running it through
> uri_escape

So they'll then be indistinguishable from any + signs in the original
string. If uri_escape is working to spec, it would then turn the +
into its %xx equivalent, which is _not_ what you want.

I think it would be OK to turn %20 to + _after_ the escape operation,
if that's what you want (the character sequence %20 shouldn't occur
anywhere in an escaped URL for any other reason than a url-escaped
space, unless I'm totally confused; and by then any + signs should
have been escaped into their %xx equivalent, so there's no confusion.
I think.)



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

Date: Thu, 21 Jun 2001 05:37:48 +0100
From: "David Soming" <davsoming@lineone.net>
Subject: Re: CGI::CARP (carpout) or (fatalsToBrowser) ?
Message-Id: <tj2u4pik3gnge0@corp.supernews.co.uk>


"Zur Aougav" <aougav@hotmail.com> wrote in message
news:bccc87cc.0106201850.21dafb3a@posting.google.com...
> "David Soming" <davsoming@lineone.net> wrote in message
news:<tj1pqohv956h52@corp.supernews.co.uk>...
> > Hi,
> > What is the difference between...
> > #use CGI::Carp qw(carpout);
> > #use CGI::Carp qw(fatalsToBrowser);
> > and what is the "qw"? I've looked in perldocs but no wiser.
> >  Thanks.
>
> First, the two lines are commented... :-)
I commented them out :-)
>
> Second, fatalsToBrowser direct errors back to your browser. Carpout
> direct STDERR to your own file/log (and not to the server's log). You
> need much more info in order to use it. To keep things clear and
> simple, you can start using fatalsToBrowser.

I think for simple debbugging, fatalsToBrowser is easiest! Thanks for the
layman's explanation- have you considered re-writing the manpages? ;-)
>
> What is qw? Use qw to put quotes around lists. Why do you need it
> here? Something with exporting names (A package like CGI::Carp can
> have some hidden names, but you are alowed to export (read: expose)
> them to your program and use them freely). In short, read the
> manual... :-)
So.. qw()= quotes?
>
> Zur




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

Date: Tue, 26 Jun 2001 14:07:40 GMT
From: Jenda Krynicky <Jenda@Krynicky.cz>
Subject: Re: counting matches
Message-Id: <1103_993564460@JENDA>

On 25 Jun 2001 15:01:11 -0700, les_ander@yahoo.com (les ander) wrote:
> Hi,
> I have a large string ($string) in which i have to 
> search for number of occurences of a pattern that is in a hash table. I 
> need to count the number of times this pattern occurs in the string
> but the compiler complains. can someone correct my code below.. thanks
> 
> my $count;
> my $sub_string;
> my $len=length($string);
> 
> #i have a hash table %patterns_tbl which has all the patterns
> 
> for my $pattern(%patterns_tbl)
> {
>   for(my $i=0; $i<$len; $i++)
>   {
>      $sub_string=substr($string,$i,$len);
>      $count=$sub_string =~ tr/$pattern/$pattern/; 
>      $patterns_tbl{$pattern}=$count;
>    }
>    count=0;
> }

 for my $pattern(keys %patterns_tbl) {
  my $count = 0;
  while ($string=~ /$pattern/g) {$count++};
  $patterns_tbl{$pattern}=$count;
 }

HTH, Jenda



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

Date: Tue, 26 Jun 2001 13:14:50 +0200
From: Danny Hendrickx <daniel.hendrickx@alcatel.be>
Subject: First CGI steps
Message-Id: <3B386EAA.35CA68A1@alcatel.be>

Hello all,

I'm taking my first steps at CGI programming, but I'm a little bit at a
loss.
I started this program to display a number of links and pass variables
when the link is clicked. That worked fine. But now I wanted to adjust
the program so that when a link is clicked (typically the link called
"Add doc without anything", the program asks for the document title and
number via a textfield box. When I click the link, I can type in the doc
number and doc title, but then my program hangs.

I suspect it has something to do with the $action variable not being
there anymore or so, but I can't seem to figure it out. 

Can anyone help me or give me hints how to do this in a better way. I
guess what I want the program to do is somewhat clear if you read the
program and its comments below.

TIA for any help.

Here is the program:

#!/usr/local/bin/perl -w

use strict;

use CGI qw(:standard);

my (
    $TITLE,
    $HEADER
   );

$TITLE = "SS31 Document Overview";
$HEADER = "SS31 Documents";

print header(),start_html($TITLE),h1($HEADER);

my $action = param("action");
my $docnr = param("docnr");
my $doctitle = param("doctitle");
my $keyword = param("keyword");

if ($action &&($action eq "add_doc"))
  {
   if (not ( $docnr && $doctitle))
     {
      
      # print "Not enough arguments to add document.\n";
       print start_form();
       print p("Geef doc nr", textfield("docnr"));
       print p("Geef titel", textfield("doctitle"));
       $action="add_doc";
       print end_form(),hr();
     }
   else
     {
      #in future, document will be added to a document list
      print "Adding doc $docnr with title $doctitle\n";
     }
  }
elsif ($action&&($action eq "look_up"))
   {
    if (not ($keyword))
      {
       #in future user will be queried for a keyword 
       print "Specify keyword";
      }
    else
      {
       # in future program will look up the document with the word in it
       print "Looking up doc with $keyword in it.\n";
      }
   }
else
   {
    print hr();
    print p("<br><center><img SRC=\"picador.gif\" height=66 width=96
align=TEXTTOP></center>");
    print p("<a href=\"whe3.cgi?action=look_up\">Look up without
keyword</a>");
    print p("<a href=\"whe3.cgi?action=look_up&keyword=TETRIS\">Look up
with keyword</a>");
    print p("<a href=\"whe3.cgi?action=add_doc\">Add doc without
anything</a>");
    print p("<a
href=\"whe3.cgi?action=add_doc&doctitle=Playing!tetris\">Add doc without
doc nr</a>");
    print p("<a href=\"whe3.cgi?action=add_doc&docnr=987654321\">Add doc
without title</a>");
    print p("<a
href=\"whe3.cgi?action=add_doc&doctitle=Playing!tetris!correctly&docnr=9876\">Add
doc correctly</a>");
    print end_form(),hr();
   }
print end_html;
-- 
Regards,
                                                ________________
________________________________________________\              /_____
 Feature Development Team Leader WR2A team 7 Services and SAD /
 SW-Engineering - Routing - VJ33                Hendrickx Danny
 phone : +32-3-240 3916                         ALCATEL  TELECOM
 fax   : +32-3-240 9899                         Fr.Wellesplein 1
 mailto:daniel.hendrickx@alcatel.be           2018 Antwerp Belgium
______________________________________________________\  /___________
URL: http://www.se.bel.alcatel.be/CH_sector1/SS31/     \/
*********************************************************************
At the end of my cash, I'm always left with a little month.
*********************************************************************


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

Date: Tue, 26 Jun 2001 14:16:02 GMT
From: Jenda Krynicky <Jenda@Krynicky.cz>
Subject: Re: Help with a regular expression
Message-Id: <1104_993564962@JENDA>

On 25 Jun 2001 12:35:53 -0700, asim@cognizo.com (Asim Beg) wrote:
> I am looking for a regular expression to parse the following:
> its basically part of a US Address.
> 
> <city>, <2 letter state> <zip>
> 
> e.g 
> 
> Redwood City, CA 94065
> 
> 
> Thanks
> Asim

Something like :

($city,$state,$zip) = $line =~ /^([^,]+?)\s*,\s*(\w\w)\s+(\d+)/;

I'm not doing much testing.

Jenda



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

Date: Tue, 26 Jun 2001 12:03:50 +0100
From: James Taylor <SEE_MY_SIG@nospam.demon.co.uk>
Subject: Re: How do I cope with pound (currency) sign?
Message-Id: <ant2611509eefNdQ@oakseed.demon.co.uk>

In article <Pine.LNX.4.30.0106260107380.29639-100000@lxplus003.cern.ch>,
Alan J. Flavell <URL:mailto:flavell@mail.cern.ch> wrote:
>
> On 25 Jun 2001, isterin jeopardized:
> >
> > You would use it's hexidecimal equivalent.
> 
> If you want to make it unnecessarily non-portable between
> different coding systems, you'd probably do that.

So how *should* he match the pound symbol? If you just put it in a
regex directly in the program source (like this /£/) it is still
non-portable between different coding systems. In fact I have no idea
whether people reading this will see a pound sign above, or something
else altogether. Maybe this /#/ looks like a pound sign, or even
this / /. Beats me, but perhaps I'm missing something obvious?

-- 
James Taylor <james (at) oakseed demon co uk>
Based in Southam, Cheltenham, UK.
PGP key available ID: 3FBE1BF9
Fingerprint: F19D803624ED6FE8 370045159F66FD02



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

Date: Tue, 26 Jun 2001 13:41:19 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: How do I cope with pound (currency) sign?
Message-Id: <Pine.LNX.4.30.0106261332420.6364-100000@lxplus013.cern.ch>

On Tue, 26 Jun 2001, James Taylor wrote:

> So how *should* he match the pound symbol?

Surely that's already been posted?  You match it with itself - it has
no special Perl properties that call for it to be expressed in another
way, escaped, or anything.

> If you just put it in a
> regex directly in the program source (like this /£/) it is still
> non-portable between different coding systems.

I can't agree.  Perl source code is text, and as such deserves to be
transcoded at the character stream level when transferred between
systems which use different character storage codings.

> In fact I have no idea
> whether people reading this will see a pound sign above, or something
> else altogether.

Whether they see it or not, _you_ did the right thing: you posted with
headers that said

  MIME-Version: 1.0
  Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-1

and that's what it takes.  You can't force them to see it correctly
(usenet is still _officially_ governed by rfc1036, which says that
only us-ascii i.e 7-bit characters are guaranteed, but hardly anyone
insists on that in practice nowadays.  Well, hardly anyone outside of
the USA, at least.)  But at least you played your proper part in the
MIME concordat, and the rest is the responsibility of the recipient.

-- 

  "There is no actual [127.0.0.2] host and if there were it probably
   would not spam you."   - MAPS database entry




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

Date: 26 Jun 2001 07:49:42 -0700
From: isterin@hotmail.com (isterin)
Subject: Re: How I can use CVS?
Message-Id: <db67a7f3.0106260649.6648755d@posting.google.com>

Mauri Heinonen <mauri@netland.fi> wrote in message news:<3B385674.7B2510E0@netland.fi>...
> Help me!
> 
> How I can use CVS?
> 
> I can't set CVSROOT, how I can do that?

Is this a perl related question?  I don't believe so.  Please refer to
the appropriate news group.  CVS comes with nice documentation, which
you should definitelly read.

Ilya



> 
> -mauri


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

Date: 26 Jun 2001 06:28:38 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: How to get env $HOME use perl
Message-Id: <slrn9jgauf.l3c.rgarciasuarez@rafael.kazibao.net>

Wang Feiyun-Arthur wrote in comp.lang.perl.misc:
} In Perl file.
} 
} How can I get my env $HOME
} 
} I tried
} 
} $home=$HOME;
} and
} $home=`echo $HOME`;
} 
} but does not work.

The environment variables are stored in the %ENV hash, as described in
the perlvar manpage. So you want to use $ENV{'HOME'}. The environment
variables are not imported into the main namespace (unlike PHP does)
because this can clutter the program's variables and introduce security
risks.

-- 
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/


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

Date: Tue, 26 Jun 2001 10:42:37 -0400
From: David Coppit <newspost@coppit.org>
Subject: Re: Perl *is* strongly typed (was Re: Perl description)
Message-Id: <Pine.SUN.4.33.0106261021180.29023-100000@mamba.cs.Virginia.EDU>

On 25 Jun 2001, Joe Schaefer wrote:

> David Coppit <newspost@coppit.org> writes:
>
> > The code I posted illustrates this. Two ostensibly equivalent scalars
> > behaved differently under the same "closure of operations" on scalars.
> > e.g. " 3 . '' " is a different scalar subtype than " 3 ".
>
> I doubt you can find any Perl that treats ("3") different than (3).
> Your prior code shows that (1/3 . '') is slightly different from (1/3),
> but not in any meaningful sense:
>
>   % perl -wle 'printf "%s\n%.16g\n", 1/3, 1/3;'
>   0.333333333333333
>   0.3333333333333333
>   % perl -wle 'print 3 * sprintf "%.16g", 1/3'
>   1
>   % perl -wle 'printf "%.14g\n", 3 * (1/3 . "")'
>   1
>   % perl -wle 'print 3 * (1/3 . "3")'
>   1

How many decimal places before a difference becomes "meaningful"? Perl
thinks my original example is "meaningful", and that's good enough for
me:

  $a = 1/3;
  $b = 1/3 . '';

  print "different\n" if $a * 3 != $b * 3;

Sure, you can "fix" this by doing:

  $a = 1/3;
  $b = (sprintf "%.16g", 1/3) . '';

  print "different\n" if $a * 3 != $b * 3;

But by doing this you are coercing stringified 1/3 into a type
class ("stringified numbers with necessary accuracy") that is
compatible with the "numeric" type class.

> > More recently, I ran into this problem trying to compare data
> > structures using Storable. One data structure had a stringified
> > version of a number. Of course the frozen data were not equivalent,
> > even though they were both of the "scalar" type.
>
> Were the numbers floats or ints?

Either:

  use Storable qw (freeze);

  $a = 1/3;
  $b = 1/3 . '';

  $fa = freeze \$a;
  $fb = freeze \$b;

  print "different\n" if $fa ne $fb;

  $a = 1;
  $b = 1 . '';

  $fa = freeze \$a;
  $fb = freeze \$b;

  print "different\n" if $fa ne $fb;

David



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

Date: 25 Jun 2001 20:12:33 GMT
From: nospam@hairball.cup.hp.com (Richard J. Rauenzahn)
Subject: Re: perl compilation on HPUX-11
Message-Id: <993499953.16596@hpvablab.cup.hp.com>

Sandeep Grover <sgrover@delsoft.com> writes:
>I need to install Perl on HPUX-11, 64-bit operating system as we have to
>build our software
>on the HPUX-11, that software uses Perl internally. Is the Perl's latest
>version
>64-bit compliant? Can I get the config.sh to compile the Perl on HPUX-11
>in 64-bit mode?

Why do you believe it needs to be compiled in 64bit mode?  You can run
32 bit applications on that 64bit operating system.  

Are you saying you need to link against libperl and that your
application is a 64bit application?

>Another problem is I don't have ANSI C compiler on this machine. Is it
>possible
>to compile Perl successfully using HP's native compiler aCC(C++
>Compiler) in 64-bit mode?

No, aCC only compiles ANSI C++.

Rich
-- 
Rich Rauenzahn ----------+xrrauenza@cup.hp.comx+ Hewlett-Packard Company
Technical Consultant     | I speak for me,     |   19055 Pruneridge Ave. 
Development Alliances Lab|            *not* HP |                MS 46TU2
ESPD / E-Serv. Partner Division +--------------+---- Cupertino, CA 95014


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

Date: 25 Jun 2001 20:22:49 GMT
From: nospam@hairball.cup.hp.com (Richard J. Rauenzahn)
Subject: Re: perl compilation on HPUX-11
Message-Id: <993500568.940386@hpvablab.cup.hp.com>

garnerjr@ci.richmond.va.us (Jim Garner) writes:
>I believe the term "native compiler" is used to refer to "cc", the compiler 
>that comes free with HP-UX.  You probably won't be able to compile perl with 

Yes.  We call it affectionately the 'brain dead' one.  It only
understands K&R and lacks optimization.  It's main purpose as I
understand it is to compile the config file linked into the kernel.

>cc.  I've never used aCC but my guess would be that it should work, try it.  I 
>don't know if the Configure script knows to look for aCC on HP, you might have 
>to specify it.

aCC only does C++ and is probably too strict.  It might work, but it
will produce C++ object files (name mangling, etc.)

>If you succeed in compiling on HP-UX 11.0, let me know your experience with the 
>following: I've had a problem with the perl-5.6.1 getpwent() function when 
>compiled under HP-UX 11.0 -- it dumps core if I call it at eof.  The pwent() 
>test that comes with perl calls the function a fixed number of times (25), so 
>if your /etc/passwd is 25 or more entries, the test appears to pass.  I can 
>write a C program to call getpwent() past eof and it returns properly, so the 
>problem is not with the HP library call.  I'd like confirmation of this problem 
>before reporting it as a bug.

I had problems installing 5.6.1 on my system -- but didn't look into
which test failed.

Trying make test again -- this is the only failure:

op/grent.............FAILED at test 0

Rich
-- 
Rich Rauenzahn ----------+xrrauenza@cup.hp.comx+ Hewlett-Packard Company
Technical Consultant     | I speak for me,     |   19055 Pruneridge Ave. 
Development Alliances Lab|            *not* HP |                MS 46TU2
ESPD / E-Serv. Partner Division +--------------+---- Cupertino, CA 95014


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

Date: 26 Jun 2001 12:40:58 GMT
From: garnerjr@ci.richmond.va.us (Jim Garner)
Subject: Re: perl compilation on HPUX-11
Message-Id: <9h9vsq$gf9$1@bob.news.rcn.net>

In article <993500568.940386@hpvablab.cup.hp.com>, nospam@hairball.cup.hp.com 
says...
>garnerjr@ci.richmond.va.us (Jim Garner) writes:
>>If you succeed in compiling on HP-UX 11.0, let me know your experience with 
the 
>>following: I've had a problem with the perl-5.6.1 getpwent() function when 
>>compiled under HP-UX 11.0 -- it dumps core if I call it at eof.  The pwent() 
>>test that comes with perl calls the function a fixed number of times (25), so 
>>if your /etc/passwd is 25 or more entries, the test appears to pass. 
>
>Trying make test again -- this is the only failure:
>
>op/grent.............FAILED at test 0
>
>Rich
---

Yes!  Function getgrent() has the same problem.  Your /etc/passwd probably has 
25 or more entries so the pwent appeared to pass.  Try this test script:

   #/your_path_to/perl-5.6.1/perl
   $,=":";
   $\="\n";
   setpwent();
   do {
      @entries=getpwent();
      print @entries;
   } until (@entries==0);
   endpwent();

Then change all occurences of 'pwent' to 'grent' and rerun it.  I bet it core 
dumps both times.

-Jim Garner
garnerjr at ci.richmond.va.us



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

Date: 26 Jun 2001 07:26:51 -0700
From: jc_va@hotmail.com (Buck Turgidson)
Subject: Perl to Convert Code
Message-Id: <f98999c8.0106260626.1bd5aadc@posting.google.com>

I have a task in which I need to take source code, and globally change
some strings.  E.g. I need to change references to EMPL_RCD# TO
EMPL_RCD and BENEFITS_RCD# TO BENEFITS_RCD, and other such
conversions.
 
Ideally, I'd like to use one directory as input, and write out to
another directory.
 
I don't know too much about Perl, but I understand that it lends
itself to this task very well.
 
Could someone provide me with a very brief shell of what I need to do
to accomplish this?

Thanks.


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

Date: Tue, 26 Jun 2001 16:34:29 +0200
From: Philip Newton <pne-news-20010626@newton.digitalspace.net>
Subject: Re: Perl to Convert Code
Message-Id: <dv6hjtoenrhic82binuupji9nolaho70jo@4ax.com>

On 26 Jun 2001 07:26:51 -0700, jc_va@hotmail.com (Buck Turgidson) wrote:

> I have a task in which I need to take source code, and globally change
> some strings.  E.g. I need to change references to EMPL_RCD# TO
> EMPL_RCD and BENEFITS_RCD# TO BENEFITS_RCD, and other such
> conversions.
>  
> Ideally, I'd like to use one directory as input, and write out to
> another directory.
>  
> I don't know too much about Perl, but I understand that it lends
> itself to this task very well.
>  
> Could someone provide me with a very brief shell of what I need to do
> to accomplish this?

Something like this should do what you want:

    #!/usr/bin/perl -wp
    s/\bEMPL_RCD#/EMPL_RCD/g;
    s/\bBENEFITS_RCD#/BENEFITS_RCD/g;
    # ... add more if you want ...
    # pattern:
    # s/ original / replacement /g;

Then go into the directory with the input files and call the script
(say, 'change.plx') something like this:

    perl -i '/otherdir/*' change.plx *

This will change the strings in each file "in-place" and leave the
original, unmodified file in 'otherdir' (the other way around from what
you said, but easier to implement quickly in Perl).

The \b before the pattern matches a non-word/word boundary. This way, it
will only match if EMPL_RCD# is not preceded by a letter, digit, or
underscore (so that e.g. ALL_EMPL_RCD# would not be matched). If that's
not what you want, remove the \b.

Hope this helps.

Cheers,
Philip
-- 
Philip Newton <nospam.newton@gmx.li>
Yes, that really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.


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

Date: Tue, 26 Jun 2001 08:07:59 -0500
From: "Mr. Sunray" <djberge@uswest.com>
Subject: Re: Problem with Archive::Tar
Message-Id: <3B38892F.9F61D642@uswest.com>

Bart Lateur wrote:

> Mr. Sunray wrote:
>
> >However, when I try and extract the tar file on the
> >command line, all I get is: "tar: Record size = 2 blocks", presumably
> >because there were 2 files in my test (though I'm guessing).
>
> No. That doesn't seem right. You seem like a Windows user, try and open
> the tar file with WinZip or a similar utility.

Actually, the .xls files were created via the Spreadsheet::WriteExcel
module.
The .zip files (of the .xls files) were created via the Archive::Zip
utility.  They
were created for managers who use nothing but NT, but I myself prefer
Solaris 8. :)

>
> I've had cases with an Archive::Tar version creating a corrupt tar file,
> in some cases. However, this seems to work alright for me, although with
> another (older) version of Archive::Tar. It could be one I patched,
> myself, though.  ;-)
>
> Check out if this creates a valid archive for you.

<program snipped>

It worked fine, so now I have to check if it's a problem creating tarballs
with files created via Spreadsheet::WriteExcel and/or Archive::Zip.

>
> p.s. If you want to run this by cron, remember to use fully qualified
> file paths, or at least, chdir to the proper directory at the start of
> the script.
>
> --
>         Bart.

Thanks for the reminder. :)




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

Date: Tue, 26 Jun 2001 09:49:25 -0500
From: "Mr. Sunray" <djberge@uswest.com>
Subject: Re: Problem with Archive::Tar
Message-Id: <3B38A0F5.40B08539@uswest.com>


--------------105BF2DF94C537304759B9CB
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

>    }
>    if($file =~ /^.*?\.zip$/i){
>       system("mv","$file","Archive"); # Move .zip files
>    }
> }
>

Near as I could tell, this 'system' call above was the culprit, though
I'm not sure why.  I eliminated every other possibility by recreating
this program from scratch, adding things in until it broke (though I
used globs instead of opendir/closedir 'cuz it was shorter).  The system
call was the last thing I added, and that's when it broke.

I changed the logic somewhat to perform all the operations on the files
first, *then* removing or moving the files as necessary via 'system' and
that fixed everything.

Peculiar.  Very peculiar.

Regards,

Mr. Sunray



--------------105BF2DF94C537304759B9CB
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>

<blockquote TYPE=CITE><font size=+1>&nbsp;&nbsp; }</font>
<br><font size=+1>&nbsp;&nbsp; if($file =~ /^.*?\.zip$/i){</font>
<br><font size=+1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; system("mv","$file","Archive");
# Move .zip files</font>
<br><font size=+1>&nbsp;&nbsp; }</font>
<br><font size=+1>}</font>
<br><font size=+1></font>&nbsp;</blockquote>
<tt><font size=+1>Near as I could tell, this 'system' call above was the
culprit, though I'm not sure why.&nbsp; I&nbsp;eliminated every other possibility
by recreating this program from scratch, adding things in until it broke&nbsp;(though
I used globs instead of opendir/closedir 'cuz it was shorter).&nbsp; The
system call was the last thing I added, and that's when it broke.</font></tt><tt><font size=+1></font></tt>
<p><tt><font size=+1>I changed the logic somewhat to perform all the operations
on the files first, *then* removing or moving the files as necessary via
'system' and that fixed everything.</font></tt><tt><font size=+1></font></tt>
<p><tt><font size=+1>Peculiar.&nbsp; Very peculiar.</font></tt><tt><font size=+1></font></tt>
<p><tt><font size=+1>Regards,</font></tt><tt><font size=+1></font></tt>
<p><tt><font size=+1>Mr. Sunray</font></tt>
<br><tt><font size=+1></font></tt>&nbsp;
<br>&nbsp;</html>

--------------105BF2DF94C537304759B9CB--



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

Date: Tue, 26 Jun 2001 12:14:16 GMT
From: Joonas Paalasmaa <joonas.paalasmaa@nokia.com>
Subject: RE problem
Message-Id: <3B387BF4.35D9DD8B@nokia.com>

Why doesn't this regular expression change all links but only first.
Any improvement suggestions?


$lines = "<a href='foo'><a href='foo'><a href='foo'>";
$lines =~ s/(\shref=)(['"]?)([^\2]+)(\2)/\1\2server\3\4/ig;
print $lines;


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

Date: Tue, 26 Jun 2001 08:51:23 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: RE problem
Message-Id: <slrn9jh1aa.ffd.tadmc@tadmc26.august.net>

Joonas Paalasmaa <joonas.paalasmaa@nokia.com> wrote:
>Why doesn't this regular expression change all links but only first.


It only substitutes once because it only matches once.

\2 in a char class doesn't do what I think you think it does.


>Any improvement suggestions?


1) enable warnings

2) use an HTML-parsing module for parsing HTML

3) you need more and better test cases


>$lines = "<a href='foo'><a href='foo'><a href='foo'>";

   my $lines = q(<a href='foo'><a href="foo"><a href=foo>)
             . q(<a href='foo"bar'><a href="foo'bar"><a href=foobar>);


>$lines =~ s/(\shref=)(['"]?)([^\2]+)(\2)/\1\2server\3\4/ig;


Try this (after fixing warnings) and see what it does:

   $lines =~ s/(\shref=)(['"]?)([^\2]+)(\2)/$1$2server$3:$4/ig;
                                                        ^
                                                        ^ see where this
                                                        ^ ends up

You can also put:

   use re 'debug';

near the top of your program, and note what it says for your [^\2]
character class:

     27:     ANYOF[\0\1\3-\377](0)



Using regexs to parse HTML is fragile in the extreme. Use a module.


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


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

Date: 26 Jun 2001 08:58:01 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: RE problem
Message-Id: <m3ithj7386.fsf@dhcp9-173.support.tivoli.com>

On Tue, 26 Jun 2001, joonas.paalasmaa@nokia.com wrote:

> Why doesn't this regular expression change all links but only first.
> Any improvement suggestions?
> 
> 
> $lines = "<a href='foo'><a href='foo'><a href='foo'>";
> $lines =~ s/(\shref=)(['"]?)([^\2]+)(\2)/\1\2server\3\4/ig;
> print $lines;

Because you cannot do "[^\2]".  That ends up matching the rest of the
string as $3.  You cannot refer to backrefs inside a character class.
Also, using the \1 syntax in the REPLACMENT part is deprecated.  Use
$1 instead.

You could do something like:

  s/(\shref=)(['"]?)((?:(?!\2).)+)(\2)/$1$2server$3$4/ig;

to achieve the same effect as the character class, but depending upon
your actual goal it may make more sense to just ignore the rest of the
href:

  s/(\shref=)(['"]?)/$1$2server/ig;

I would probably use:

  s/(?<=\shref=)(['"]?)/$1server/ig;

just because I like to replace as little as conveniently possible.
Except that for anything at all complicated I would end up using one
of the HTML parsing modules.

-- 
Ren Maddox
ren@tivoli.com


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

Date: Tue, 26 Jun 2001 14:53:11 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: RE problem
Message-Id: <3B38A211.1311865A@acm.org>

Joonas Paalasmaa wrote:
> 
> Why doesn't this regular expression change all links but only first.
> Any improvement suggestions?
> 
> $lines = "<a href='foo'><a href='foo'><a href='foo'>";
> $lines =~ s/(\shref=)(['"]?)([^\2]+)(\2)/\1\2server\3\4/ig;
                                 ^^
This won't work in a character class, try:

$lines =~ s/(\shref=)(['"]?)(.+?)(\2)/$1$2server$3$4/ig;

> print $lines;


John
-- 
use Perl;
program
fulfillment


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

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


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

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

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

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


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


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