[23593] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5800 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 14 06:05:44 2003

Date: Fri, 14 Nov 2003 03:05:07 -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           Fri, 14 Nov 2003     Volume: 10 Number: 5800

Today's topics:
    Re: #!perl instead of #!/usr/bin/perl on a Unix/Apache  <bobx@linuxmail.org>
    Re: #!perl instead of #!/usr/bin/perl on a Unix/Apache  <matthias_weckman@hotmail.com>
    Re: arrange form data in same order as on form <bxtrap01@comcast.net>
    Re: arrange form data in same order as on form <dha@panix.com>
        Can you reliably make a reference to a capture buffer? <clint@0lsen.net>
        Dynamic regexp <winston_smith@linuxmail.org>
    Re: Dynamic regexp (Eric Joanis)
    Re: Dynamic regexp <matthias_weckman@hotmail.com>
        flock or IPC semaphore for atomic appends (jtd)
        Is a value a decimal number or not? <tore@aursand.no>
    Re: Is a value a decimal number or not? <josef.moellers@fujitsu-siemens.com>
    Re: Is a value a decimal number or not? (Anno Siegel)
        ParseRecur  from Date::Manip (Arndt, Tobias)
        Parsing delimiter-separated data. <a24061@yahoo.munged>
    Re: Parsing delimiter-separated data. <abigail@abigail.nl>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 14 Nov 2003 10:32:49 GMT
From: "Bob X" <bobx@linuxmail.org>
Subject: Re: #!perl instead of #!/usr/bin/perl on a Unix/Apache webserver
Message-Id: <lb2tb.733$_i1.537092@news2.news.adelphia.net>


"Louis Erickson" <wwonko@rdwarf.com> wrote in message
news:bp1keo$kbb$1@holly.rdwarf.com...
> Sam Holden <sholden@flexal.cs.usyd.edu.au> wrote:
> : On Fri, 14 Nov 2003 02:39:54 GMT, Bob X <bobx@linuxmail.org> wrote:
> :> "Josef Mollers" <josef.moellers@fujitsu-siemens.com> wrote in message
> :> news:3FB37774.8EA139B9@fujitsu-siemens.com...
> :> joe wrote:
> :>>
> :>> Can anyone help me out here. I run my CGI-scripts locally on a Windows
> :>> machine. In the shebang line I put #!c:\www\Perl.exe, but it's also
> :> possible
> :>> to use only #!perl. On my virtual webserver at my ISP's Unix machine I
> :> have
> :>> to use #!/usr/bin/perl.
> :>
> :>>AFAIK it is irrelevant _what_ command you put into the shebang line on
a
> :>>windows system. Windows doesn't know shebang lines (it maybe knows a
 .pl
> :>>extension, though), but the perl interpreter knows how to extract
> :>>options (e.g. -w) from the shebang line.
> :>
> :> Actually if you are going to doing web work on Windows then the #!perl
is
> :> necessary.
>
> : No it isn't. Some web servers may require that, but that's the web
server
> : not windows.
>
> And, sometimes it varies even within the same web server.
>
> Apache for Windows, for instance, has a way to associate Perl with AS Perl
> that doesn't depend on #! pointing anywhere in particular.  But, if you
> use FastCGI, the #! must be correct.
>
> IIS uses a static mapping of program or ISAPI DLL to extension, so .pl
> and .plx are always handled by ActiveState Perl.
>
> Note that Apache's mapping of .pl files and IIS's mapping of .pl files
> are entirely different.  IIS uses it's own configuration via the IIS MMC,
> and Apache uses a registry entry.  Or maybe a different registry entry,
> depending on your configuration.
>
> Isn't CGI fun?
> -- 
> Louis Erickson - wwonko@rdwarf.com - http://www.rdwarf.com/~wwonko/
>
> If you're not part of the solution, you're part of the precipitate.
I am using Apache on Windows and I have to use the #!perl shebang. Then
again I may not have Apache setup right.

Didn't mean to imply that anything was wrong with the OS though (per a
previous poster).




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

Date: Fri, 14 Nov 2003 11:58:49 +0100
From: Matthias Weckman <matthias_weckman@hotmail.com>
Subject: Re: #!perl instead of #!/usr/bin/perl on a Unix/Apache webserver
Message-Id: <3fb4b568$0$42225$c4fe714e@dreader11.xs4all.nl>

Bob X wrote:
> "Louis Erickson" <wwonko@rdwarf.com> wrote in message
> news:bp1keo$kbb$1@holly.rdwarf.com...
> 
>>Sam Holden <sholden@flexal.cs.usyd.edu.au> wrote:
>>: On Fri, 14 Nov 2003 02:39:54 GMT, Bob X <bobx@linuxmail.org> wrote:
>>:> "Josef Mollers" <josef.moellers@fujitsu-siemens.com> wrote in message
>>:> news:3FB37774.8EA139B9@fujitsu-siemens.com...
>>:> joe wrote:
>>:>>
>>:>> Can anyone help me out here. I run my CGI-scripts locally on a Windows
>>:>> machine. In the shebang line I put #!c:\www\Perl.exe, but it's also
>>:>> possible
>>:>> to use only #!perl. On my virtual webserver at my ISP's Unix machine I
>>:>> have
>>:>> to use #!/usr/bin/perl.
>>:>
>>:>>AFAIK it is irrelevant _what_ command you put into the shebang line on
> a
>>:>>windows system. Windows doesn't know shebang lines (it maybe knows a
> .pl
>>:>>extension, though), but the perl interpreter knows how to extract
>>:>>options (e.g. -w) from the shebang line.
>>:>

[snip]

> I am using Apache on Windows and I have to use the #!perl shebang. Then
> again I may not have Apache setup right.
> 
> Didn't mean to imply that anything was wrong with the OS though (per a
> previous poster).

I'm using apache too (on my win32 machine for testing), and have this 
line in my httpd.conf:

ScriptInterpreterSource Registry

This way apache doesn't need a perfectly written #! line, since it looks 
up the association in the windows registry.
The upside of this is that you can put the #! line as needed by your 
real web server, and still have a working script in your test environment.
The downside of this is that you have to put every extension you use in 
the registry (.cgi isn't by default associated with perl.exe).
In both cases the perl interpreter honors the switches in the shebang 
line, so that's nice.

Matthias


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

Date: Fri, 14 Nov 2003 06:04:34 GMT
From: "bbxrider" <bxtrap01@comcast.net>
Subject: Re: arrange form data in same order as on form
Message-Id: <Sf_sb.145049$9E1.742137@attbi_s52>

what is 'top-post' ???
actually don't understand how the eric roode and first sinan unur posts were
not subordinated to the post immediately above them,
i simply use reply-to-group and it always subordinates to the post i'm
responding to

"Ben Morrow" <usenet@morrow.me.uk> wrote in message
news:bp1hue$mj7$2@wisteria.csv.warwick.ac.uk...
> [please don't top-post]
>
> "bbxrider" <bxtrap01@comcast.net> wrote:
> > thanks for all the help and opinions i'm just self learning perl and
> > found some code at http://www.cgi101.com/class/ and some other
> > searching google groups actually i dont even know what cgi.pm and
> > cgi lite are but will surely find out
>
> For CGI, type 'perldoc CGI' at a command prompt. CGI::Lite you would
> need to install. Note that the names of Perl modules are case-sensitive.
>
> > i dont' mean to try and just steal code, but have found that seeing,
> > using and understanding examples really accelerates my learing curve
>
> Absolutely. Reading decent code is one of the best ways to learn. You
> do have to be sure your source is reliable, though: there is one hell
> of a lot of very bad Perl floating around the web.
>
> > what i've since found is that the variable containing the form input
> > is in fact in the same order as the form this code keeps the
> > original order
>
> I don't think this is guaranteed, by which I mean that it may happen
> to work for you with your browser during this phase of the moon, but
> under other circumstances it may well not. If you need to keep
> separate track of the different paramaters, give them different
> names. Change whatever generates them to put a number on the end, or
> something.
>
> Ben
>
> --
> For the last month, a large number of PSNs in the Arpa[Inter-]net have
been
> reporting symptoms of congestion ... These reports have been accompanied
by an
> increasing number of user complaints ... As of June,... the Arpanet
contained
> 47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] *
ben@morrow.me.uk




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

Date: Fri, 14 Nov 2003 08:37:23 +0000 (UTC)
From: "David H. Adler" <dha@panix.com>
Subject: Re: arrange form data in same order as on form
Message-Id: <slrnbr9523.agq.dha@panix2.panix.com>

In article <bp1c15$1in3ke$1@ID-184292.news.uni-berlin.de>, Gunnar
Hjalmarsson wrote:

> Eric Schwartz wrote:

[re: CGI.pm]

>> No, but it removes one axis of variability from the list of things 
>> that could be buggy.  Given the option of using known-good code and
>> hacking something up yourself, why (other than learning
>> excercises, which are surely valuable) would you not use the tested
>> and verified code?
> 
> I very much dislike the aggressive way in which some people here
> advocate the use of CGI, and the lack of faith that is shown in
> people's own judge. The described attitude makes me suspicious and
> less inclined to listen. How about that for a reason? :)
 
Looking at the question and the answer, in isolation at least, I'm going
to assume the use of "reason" there is irony. :-)

dha

-- 
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
'Don't be tempted to veer off!'
        - Paul McGann


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

Date: Fri, 14 Nov 2003 09:28:59 GMT
From: Clint Olsen <clint@0lsen.net>
Subject: Can you reliably make a reference to a capture buffer?
Message-Id: <slrnbr982r.vd4.clint@poly.0lsen.net>

I tried to return a reference to a capture buffer ($1), and it looks like
the contents are destroyed out from under me.  Is there something special
about a capture buffer that makes this impossible?

#!/usr/bin/perl -w

use strict;
use warnings;

sub foo {
    my $foo = "barbell";
    if ($foo =~ /(bar)/) {
        print "$1\n";
        \$1;
    }
}

print "output of foo is ", ${&foo}, "\n";

results in:

bar
Use of uninitialized value in print at /tmp/reftest line 14.
output of foo is 


Thanks,

-Clint


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

Date: Thu, 13 Nov 2003 23:54:23 -0500
From: Winston Smith <winston_smith@linuxmail.org>
Subject: Dynamic regexp
Message-Id: <2eZsb.44803$xI2.981453@news20.bellglobal.com>

Hi everybody,

I'm looking for a way to make a batch of s/// substitutions. As a code 
sample is worth a thousand words, let see what me code is presently :

---

my @rules = (
    ['^HELLO (.*)$', 'BONJOUR $1'],
    # ... lots of other rules
);

foreach my $rule (@rules) {
    if ($string =~ s/$rule->[0]/$rule->[1]/ei) {
       last;
    }
}

---

With this code, 'HELLO WINSTON' becomes 'BONJOUR $1' and not 'BONJOUR 
WINSTON' as I'd like.

I tried to put double quotes in the table of strings but it makes no 
difference.

I also tried to put a second e option to the s/// operator so that the 
string 'BONJOUR $1' is reinterpolated. Then I get the message 'Use of 
uninitialized value in substitution iterator ...' as if $1 is not 
defined. But if I put a
    print $1;
instruction just before the instruction
    last;
it works and actually print 'WINSTON' if I use the same exemple than 
previously.

Thank you in advance for your help.


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

Date: 14 Nov 2003 10:28:07 GMT
From: joanis@cs.toronto.edu (Eric Joanis)
Subject: Re: Dynamic regexp
Message-Id: <2003Nov14.052807.18944@jarvis.cs.toronto.edu>

Dear Winston,

Winston Smith  <winston_smith@linuxmail.org> wrote:
>I'm looking for a way to make a batch of s/// substitutions. As a code
>sample is worth a thousand words, let see what me code is presently :
>
> my @rules = ( ['^HELLO (.*)$', 'BONJOUR $1'], ... );
> foreach my $rule (@rules) {
>    if ($string =~ s/$rule->[0]/$rule->[1]/ei) {

The problem with this code is that $rule->[1] is only interpolated once
and thus the "$1" it contains is not itself interpolated to the contents of the
$1 variable.  To fix this, you need to get Perl to evaluation the replacement
string twice.  After some trial and error, I found that this works:

    if ($string =~ s/$rule->[0]/eval qq("$rule->[1]") /ei) {

If $rule->[1] is 'BONJOUR $1', then qq("$rule->[1]") yields "BONJOUR $1".
When this is evaluated again using eval, $1 is interpolate as you want it
to be.

Note that
    eval '"' . $rule->[1] . '"'
would be equivalent to
    eval qq("$rule->[1]")

Warning:  I expect this code to be fairly slow, because Perl has to
recompile the expression at every iteration.  I'd be happy to see a more
elegant solution to force perl to perform interpolation twice on a string,
if anyone has one, but I couldn't come up with one myself.

Cheers,

Eric


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

Date: Fri, 14 Nov 2003 12:00:54 +0100
From: Matthias Weckman <matthias_weckman@hotmail.com>
Subject: Re: Dynamic regexp
Message-Id: <3fb4b5e5$0$42225$c4fe714e@dreader11.xs4all.nl>

Eric Joanis wrote:

> Dear Winston,
> 
> Winston Smith  <winston_smith@linuxmail.org> wrote:
> 
>>I'm looking for a way to make a batch of s/// substitutions. As a code
>>sample is worth a thousand words, let see what me code is presently :
>>
>>my @rules = ( ['^HELLO (.*)$', 'BONJOUR $1'], ... );
>>foreach my $rule (@rules) {
>>   if ($string =~ s/$rule->[0]/$rule->[1]/ei) {
> 
> 
> The problem with this code is that $rule->[1] is only interpolated once
> and thus the "$1" it contains is not itself interpolated to the contents of the
> $1 variable.  To fix this, you need to get Perl to evaluation the replacement
> string twice.  After some trial and error, I found that this works:
> 
>     if ($string =~ s/$rule->[0]/eval qq("$rule->[1]") /ei) {
> 

wouldn't doubling the e at the end work as well? like so:

	if ($string =~ s/$rule->[0]/$rule->[1]/eei)
                                           -----^^


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

Date: 14 Nov 2003 00:15:16 -0800
From: adwser@hotmail.com (jtd)
Subject: flock or IPC semaphore for atomic appends
Message-Id: <c57c103.0311140015.7052583b@posting.google.com>

Hi all,

I'm running linux 2.4.20. I have 50-100 processes appending to a
single file. Each process appends a block of binary data between 1
byte to 100KB in size, at the rate of at least 1 per second. Using
flock appears to ensure that the appends are atomic, but it does use
up a lot of CPU time compared to not locking at all.

My questions:
1) Is my assumption that flock will ensure that appends are atomic
correct?
2) Why is flock processor intensive? Does flock write something to
disk when locking/unlocking?
3) Would IPC semaphores be a better idea since it is an in-memory
structure?

open(F, ">> append.txt");
binmode(F);
while(1) {
  flock(F, LOCK_EX);
  print F (large block of binary data)
  flock(F, LOCK_UN);
  sleep(rand(1));
}
close(F);

Thanks for any suggestions,
jtd


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

Date: Fri, 14 Nov 2003 11:09:43 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Is a value a decimal number or not?
Message-Id: <pan.2003.11.14.10.09.32.637024@aursand.no>

Hi!

Not having written many tests myself, I started yesterday looking av some
of the Test modules.  Boring stuff, but an excellent way of keeping your
code clean from those annoying bugs.

However.  In one of the modules, there was a 'is_decimal' function, and it
failed some of my tests.  Not surprisingly, though.  But.  Is there a way
to check if a number is a decimal number even when you declare the value
as a "plain number"?

Example:

  my $nr = 5.00;

  if ( is_decimal($nr) ) {
      print "is_decimal\n";
  }

  sub is_decimal {
      my $nr = shift;
      return ( defined $nr && $nr =~ m,^[+-]?(\d+)?\.\d+$, ) ? 1 : 0;
  }

This always fails, of course, as Perl seems to evaluate _the number_ 5.00
as the number 5 as soon as the variable $nr gets set.  Setting $nr to a
string - ie. '5.00' - works fine.

Is there a way round this "problem"?  No big deal, really, but just out of
curiousity. :-)


-- 
Tore Aursand <tore@aursand.no>


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

Date: Fri, 14 Nov 2003 11:29:41 +0100
From: Josef =?iso-8859-1?Q?M=F6llers?= <josef.moellers@fujitsu-siemens.com>
Subject: Re: Is a value a decimal number or not?
Message-Id: <3FB4AE95.831B3CE5@fujitsu-siemens.com>

Tore Aursand wrote:
> =

> Hi!
> =

> Not having written many tests myself, I started yesterday looking av so=
me
> of the Test modules.  Boring stuff, but an excellent way of keeping you=
r
> code clean from those annoying bugs.
> =

> However.  In one of the modules, there was a 'is_decimal' function, and=
 it
> failed some of my tests.  Not surprisingly, though.  But.  Is there a w=
ay
> to check if a number is a decimal number even when you declare the valu=
e
> as a "plain number"?
> =

> Example:
> =

>   my $nr =3D 5.00;
> =

>   if ( is_decimal($nr) ) {
>       print "is_decimal\n";
>   }
> =

>   sub is_decimal {
>       my $nr =3D shift;
>       return ( defined $nr && $nr =3D~ m,^[+-]?(\d+)?\.\d+$, ) ? 1 : 0;=

>   }
> =

> This always fails, of course, as Perl seems to evaluate _the number_ 5.=
00
> as the number 5 as soon as the variable $nr gets set.  Setting $nr to a=

> string - ie. '5.00' - works fine.
> =

> Is there a way round this "problem"?  No big deal, really, but just out=
 of
> curiousity. :-)

It's not really a "problem", but you give the solution yourself: you're
mixing apples with oranges.
You assign a number to a variable, but do a string test on it. Perl does
a lot of stringification for you (as I have learned only yesterday), but
when it sees "$nr =3D 5.00", it first parses the string "5.00" itself and=

converts it to its internal representation of a number. As this number
has no decimals, stringification of it will return "5", as this is the
simplest representation of 5.00.


[X] I don't know Perl
=2E.. but then ... who does?
-- =

Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett


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

Date: 14 Nov 2003 10:51:38 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Is a value a decimal number or not?
Message-Id: <bp2c3q$dja$1@mamenchi.zrz.TU-Berlin.DE>

Tore Aursand  <tore@aursand.no> wrote in comp.lang.perl.misc:
> Hi!
> 
> Not having written many tests myself, I started yesterday looking av some
> of the Test modules.  Boring stuff, but an excellent way of keeping your
> code clean from those annoying bugs.
> 
> However.  In one of the modules, there was a 'is_decimal' function, and it
> failed some of my tests.  Not surprisingly, though.  But.  Is there a way
> to check if a number is a decimal number even when you declare the value
> as a "plain number"?

What do you consider a decimal number?

Numbers as such aren't decimal or binary or anything, they're numbers.
Various systems can be used to represent numbers *as strings*, and that
is what properties like "hex", "binary" and "decimal" are talking about.
So the distinction is about strings, not the numbers they represent.

> Example:
> 
>   my $nr = 5.00;
> 
>   if ( is_decimal($nr) ) {
>       print "is_decimal\n";
>   }
> 
>   sub is_decimal {
>       my $nr = shift;
>       return ( defined $nr && $nr =~ m,^[+-]?(\d+)?\.\d+$, ) ? 1 : 0;

Why the "?:" operator?  It doesn't change the boolean result.

>   }

It seems you require a period in a string for it to count as a "decimal
number".  That is not the usual definition, though I know one participant
on clpm who would agree with you.  In the usual definition a string is
a decimal number if it consists of an optional sign, one or more digits
and at most one period among the digits.  I suppose that's what is_decimal()
checks, though I haven't looked.

> This always fails, of course, as Perl seems to evaluate _the number_ 5.00
> as the number 5 as soon as the variable $nr gets set.  Setting $nr to a
> string - ie. '5.00' - works fine.
> 
> Is there a way round this "problem"?  No big deal, really, but just out of
> curiousity. :-)

There is no problem.  A number is a number, and the question whether it
is decimal makes no sense.  You can write the same number in different
ways, and with these routines like is_decimal() give reasonable answers.

Anno


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

Date: 14 Nov 2003 02:47:19 -0800
From: tobias.arndt@fcit.fichtner.de (Arndt, Tobias)
Subject: ParseRecur  from Date::Manip
Message-Id: <a794cf27.0311140247.7b599606@posting.google.com>

Hi NG,

trying to understand the ParseRecur function. Seems to be too complex
for me. My goal is to get the first Monday from each month of the year
2003. My code:

@date = ParseRecur("0:1*1:1:0:0:0","1/1/03","1/1/03","31/12/03"); 

print $date[3];

# that should print out the First Monday in April 2003

But I get 2003040700:00:00 !! That's the second Monday in this
month.... :-(
Maybe I didn't get the meaning of the three ($base etc..)variables
from the example;

# To find the 2nd tuesday of every month
#  @date = ParseRecur("0:1*2:2:0:0:0",$base,$start,$stop);

Any help or hint would be greatly appreciated !

Thanks Tobias


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

Date: Fri, 14 Nov 2003 09:14:35 GMT
From: Adam <a24061@yahoo.munged>
Subject: Parsing delimiter-separated data.
Message-Id: <%11tb.3616$4a5.30444640@news-text.cableinet.net>

I'm writing a hash (string keys, string values) to a text file (by STDOUT) 
for reading later, and I decided on the following format:

key|value|
another key|another value|

to make the file clearly human-readable (the values and keys can contain 
spaces).  I've also provided for escaping ``|'' and ``\'' in the data with 
``\|'' and ``\\'' respectively.

Here's the output routine

    foreach $key (keys(%table) ) {
        $value = $table{$key} ;
        $key   =~ s/\\/\\\\/g ;
        $key   =~ s/\|/\\\|/g ;
        $value =~ s/\\/\\\\/g ;
        $value =~ s/\|/\\\|/g ;
        print($key . "|" . $value . "|\n") ;

and here's the input routine

    while ($line = <>) {
        chomp($line) ;
        $line =~ /^(.*([^\\]|\\\\))\|(.*)\|$/ ;
        $key   =  $1 ;
        $value =  $3 ;
        $key   =~ s/\\\|/\|/g ;
        $key   =~ s/\\\\/\\/g ;
        $value =~ s/\\\|/\|/g ;
        $value =~ s/\\\\/\\/g ;
        $table{$key} = $value ;
    }


They seem to work, but I'm not sure how efficient they are (in particular 
I have doubts about the regexp), so I'd appreciate any suggestions for 
improvement.

I've also just noticed that the input routine would not correctly handle a 
line like this:

blah\\|blahblah|

What's the best way to reverse the escapes?




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

Date: 14 Nov 2003 09:35:01 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Parsing delimiter-separated data.
Message-Id: <slrnbr98e5.mjl.abigail@alexandra.abigail.nl>

Adam (a24061@yahoo.munged) wrote on MMMDCCXXVII September MCMXCIII in
<URL:news:%11tb.3616$4a5.30444640@news-text.cableinet.net>:
//  I'm writing a hash (string keys, string values) to a text file (by STDOUT) 
//  for reading later, and I decided on the following format:
//  
//  key|value|
//  another key|another value|
//  
//  to make the file clearly human-readable (the values and keys can contain 
//  spaces).  I've also provided for escaping ``|'' and ``\'' in the data with 
//  ``\|'' and ``\\'' respectively.
//  
//  Here's the output routine
//  
//      foreach $key (keys(%table) ) {
//          $value = $table{$key} ;
//          $key   =~ s/\\/\\\\/g ;
//          $key   =~ s/\|/\\\|/g ;
//          $value =~ s/\\/\\\\/g ;
//          $value =~ s/\|/\\\|/g ;
//          print($key . "|" . $value . "|\n") ;
//  
//  and here's the input routine
//  
//      while ($line = <>) {
//          chomp($line) ;
//          $line =~ /^(.*([^\\]|\\\\))\|(.*)\|$/ ;
//          $key   =  $1 ;
//          $value =  $3 ;
//          $key   =~ s/\\\|/\|/g ;
//          $key   =~ s/\\\\/\\/g ;
//          $value =~ s/\\\|/\|/g ;
//          $value =~ s/\\\\/\\/g ;
//          $table{$key} = $value ;
//      }
//  
//  
//  They seem to work, but I'm not sure how efficient they are (in particular 
//  I have doubts about the regexp), so I'd appreciate any suggestions for 
//  improvement.
//  
//  I've also just noticed that the input routine would not correctly handle a 
//  line like this:
//  
//  blah\\|blahblah|
//  
//  What's the best way to reverse the escapes?


What is the best way? Here is *a* way of dealing with it:

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
    chomp;
    my ($key, $value) = /^([^\\|]*(?:\\.[^\\|]*)*)\|([^\\|]*(?:\\.[^\\|]*)*)\|$/
         or next;
    map {s/\\(.)/$1/g} $key, $value;
    print "[$key] [$value]\n";
}

__DATA__
hello|world|
he\|\|o|wor\|d|
blah\\|blahblah|


Running this gives:

[hello] [world]
[he||o] [wor|d]
[blah\] [blahblah]



Abigail
-- 
sub _'_{$_'_=~s/$a/$_/}map{$$_=$Z++}Y,a..z,A..X;*{($_::_=sprintf+q=%X==>"$A$Y".
"$b$r$T$u")=~s~0~O~g;map+_::_,U=>T=>L=>$Z;$_::_}=*_;sub _{print+/.*::(.*)/s};;;
*_'_=*{chr($b*$e)};*__=*{chr(1<<$e)};                # Perl 5.6.0 broke this...
_::_(r(e(k(c(a(H(__(l(r(e(P(__(r(e(h(t(o(n(a(__(t(us(J())))))))))))))))))))))))


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

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


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