[29827] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1070 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 27 14:09:42 2007

Date: Tue, 27 Nov 2007 11:09:08 -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           Tue, 27 Nov 2007     Volume: 11 Number: 1070

Today's topics:
        Applications Help! <leonard_plummer@hotmail.co.uk>
    Re: Applications Help! <glex_no-spam@qwest-spam-no.invalid>
        How can you embed a function inside a replacement opera <neil@solenttechnology.co.uk>
    Re: How can you embed a function inside a replacement o <peter@makholm.net>
    Re: How can you embed a function inside a replacement o <simon.chao@fmr.com>
    Re: How can you embed a function inside a replacement o <neil@solenttechnology.co.uk>
    Re: How can you embed a function inside a replacement o <simon.chao@fmr.com>
    Re: how to write non-compatible perl scripts to run on  <stoupa@practisoft.cz>
    Re: how to write non-compatible perl scripts to run on  <jurgenex@hotmail.com>
    Re: how to write non-compatible perl scripts to run on  <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: interesting case of data corruption, and its cause <m@rtij.nl.invlalid>
    Re: Omitting data from a single line.. <simon.chao@fmr.com>
    Re: page 124 of the camel book <njus@larshaugseth.com>
    Re: page 124 of the camel book <jl_post@hotmail.com>
    Re: page 124 of the camel book <njus@larshaugseth.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 27 Nov 2007 10:50:54 -0800 (PST)
From: Frieza <leonard_plummer@hotmail.co.uk>
Subject: Applications Help!
Message-Id: <d83cfdde-eea4-405d-b0e0-6bab39133052@d61g2000hsa.googlegroups.com>

Hi can anyone think up of at least five applications that could
benefit from the use of arrays in Perl?


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

Date: Tue, 27 Nov 2007 13:06:05 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Applications Help!
Message-Id: <474c6a9d$0$3579$815e3792@news.qwest.net>

Frieza wrote:
> Hi can anyone think up of at least five applications that could
> benefit from the use of arrays in Perl?

Yes.

Maybe you should do your own homework.


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

Date: Tue, 27 Nov 2007 07:08:01 -0800 (PST)
From: neilsolent <neil@solenttechnology.co.uk>
Subject: How can you embed a function inside a replacement operator ?
Message-Id: <59733503-31d9-4f70-b179-b7075b7a5df4@s36g2000prg.googlegroups.com>

Hi
Sorry if this is a basic question.
How can you embed a function inside a replacement operator ?

e.g.:
####################################
sub conv()
{
    return $_[0] * 2;
}


$test = 'abc125abc';

$test =~ s/(\d+)/&conv($1)/g;

print $test . '\n';
####################################

The output of this script is:

abc&conv(123)abc

Whereas I would like it to be:

abc250abc


Many thanks,
Neil


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

Date: Tue, 27 Nov 2007 15:12:07 +0000
From: Peter Makholm <peter@makholm.net>
Subject: Re: How can you embed a function inside a replacement operator ?
Message-Id: <87fxyrhge0.fsf@hacking.dk>

neilsolent <neil@solenttechnology.co.uk> writes:

> Sorry if this is a basic question.
> How can you embed a function inside a replacement operator ?

Look at the /e modifier. It is documented in 'perldoc perlop'.

//Makholm


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

Date: Tue, 27 Nov 2007 07:13:47 -0800 (PST)
From: nolo contendere <simon.chao@fmr.com>
Subject: Re: How can you embed a function inside a replacement operator ?
Message-Id: <286ca900-ed2f-4280-b6a8-94cbaf5491ac@a39g2000pre.googlegroups.com>

On Nov 27, 10:08 am, neilsolent <n...@solenttechnology.co.uk> wrote:
> Hi
> Sorry if this is a basic question.
> How can you embed a function inside a replacement operator ?
>
> e.g.:
> ####################################
> sub conv()
> {
>     return $_[0] * 2;
>
> }
>
> $test = 'abc125abc';
>
> $test =~ s/(\d+)/&conv($1)/g;
>
> print $test . '\n';
> ####################################
>
> The output of this script is:
>
> abc&conv(123)abc
>
> Whereas I would like it to be:
>
> abc250abc
>

my $test = 'abc125def';
$test =~ s/(\d+)/conv($1)/ge;

print $test, "\n";

sub conv {
    my ( $val ) = @_;
    return $val * 2;
}


__OUTPUT__

bash-2.03$ ./replace_func.pl
abc250def
bash-2.03$


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

Date: Tue, 27 Nov 2007 07:40:18 -0800 (PST)
From: neilsolent <neil@solenttechnology.co.uk>
Subject: Re: How can you embed a function inside a replacement operator ?
Message-Id: <5819999e-36fa-4f09-83e4-6413fc099a47@i29g2000prf.googlegroups.com>

On 27 Nov, 15:12, Peter Makholm <pe...@makholm.net> wrote:
> neilsolent <n...@solenttechnology.co.uk> writes:
> > Sorry if this is a basic question.
> > How can you embed a function inside a replacement operator ?
>
> Look at the /e modifier. It is documented in 'perldoc perlop'.
>
> //Makholm

Thanks for that guys, thought it would be simple. I did do a search
first (honest!)


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

Date: Tue, 27 Nov 2007 07:44:39 -0800 (PST)
From: nolo contendere <simon.chao@fmr.com>
Subject: Re: How can you embed a function inside a replacement operator ?
Message-Id: <a9ed85c7-00e2-439f-a601-7eefb3037655@j20g2000hsi.googlegroups.com>

On Nov 27, 10:40 am, neilsolent <n...@solenttechnology.co.uk> wrote:
> On 27 Nov, 15:12, Peter Makholm <pe...@makholm.net> wrote:
>
> > neilsolent <n...@solenttechnology.co.uk> writes:
> > > Sorry if this is a basic question.
> > > How can you embed a function inside a replacement operator ?
>
> > Look at the /e modifier. It is documented in 'perldoc perlop'.
>
> > //Makholm
>
> Thanks for that guys, thought it would be simple. I did do a search
> first (honest!)

np. sometimes it's hard to phrase your question for a search engine,
especially if you lack the correct key word. also, your post was
exemplary in that you posed a cogent question, showed your attempt
that was as short as possible and as long as it needed to be, showed
your results, and showed what you expected.


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

Date: Tue, 27 Nov 2007 15:17:58 +0100
From: "Petr Vileta" <stoupa@practisoft.cz>
Subject: Re: how to write non-compatible perl scripts to run on differnet operating systems?
Message-Id: <fih9a9$2s3$1@ns.felk.cvut.cz>

Torben wrote:
> Hi to all,
> 
> is it possible to make some kind of pragmas in perl scripts, so for
> example I can run a perl script like this:
> __BEGIN__
> #use Win32::Sound;
> Win32::Sound::Play("SystemExclamation");
> print "sound only plays on windows\n";
> __END__
> 
> on Linux, which there of cause only shows the message?
> 

if($^O eq "MSWin32")
    {
    use Win32::Sound; # not tested, maybe you can uncomment next line
    #require Win32::Sound;
    Win32::Sound::Play("SystemExclamation");
    }
else
    {
    print "sound only plays on windows\n";
    }

-- 
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your
mail from another non-spammer site please.) 

Please reply to <petr AT practisoft DOT cz>



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

Date: Tue, 27 Nov 2007 14:48:53 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: how to write non-compatible perl scripts to run on differnet operating systems?
Message-Id: <p7W2j.14959$r81.2042@trndny05>

Petr Vileta wrote:
> if($^O eq "MSWin32")
>    {
>    use Win32::Sound; # not tested, maybe you can uncomment next line

This does not do what you probably expect it to do. The 'use' is executed at 
compile time already regardless of the evaluation of the 'if' which happens 
much later at runtime.

You should use 'require' instead as mentioned in the docs for 'use'.

jue




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

Date: Tue, 27 Nov 2007 10:51:45 -0800
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: how to write non-compatible perl scripts to run on differnet operating systems?
Message-Id: <1j5u15x8i9.ln2@goaway.wombat.san-francisco.ca.us>

On 2007-11-27, Jürgen Exner <jurgenex@hotmail.com> wrote:
> Petr Vileta wrote:
>> if($^O eq "MSWin32")
>>    {
>>    use Win32::Sound; # not tested, maybe you can uncomment next line
>
> This does not do what you probably expect it to do. The 'use' is executed at 
> compile time already regardless of the evaluation of the 'if' which happens 
> much later at runtime.
>
> You should use 'require' instead as mentioned in the docs for 'use'.

There is also the "use if" pragma, available in recent versions of Perl;
something like (completely untested)

use if ($^O eq 'MSWin32') , 'Win32::Sound' ;

--keith

-- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information



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

Date: Tue, 27 Nov 2007 19:45:14 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: interesting case of data corruption, and its cause
Message-Id: <pan.2007.11.27.18.45.14@rtij.nl.invlalid>

On Tue, 27 Nov 2007 12:37:53 +0000, bugbear wrote:

> Ben Morrow wrote:
>> Attempting to fix the problem by localising $_ is tricky, as well,
>> because C<local $_> doesn't always work due to some very boring bugs in
>> some versions of perl when $_ is an element of a tied array. The
>> workaround (local *_) doesn't buy you much, because you lose your sub
>> arguments. About the only time I use while (<>) is at the very top
>> level with -n, when it can't do any harm.
> 
> Thanks to all for the discussion.
> 
> Do I take it "best practice" is (simply) not to assign to $_, either
> explicitly (rare) or implicitly (while(<>) or while(<H>)) ?

Well, yes and no. As with all things, it depends. F.i. I find the 
following perfectly acceptable:

SWITCH: {
  for ($x) {
    /something/ and do {
       ...
       last SWITCH;
    };
    ...
};

But YMMV. In general, one should avoid $_, unless there is a clear 
advantage.

HTH,
M4


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

Date: Tue, 27 Nov 2007 07:05:34 -0800 (PST)
From: nolo contendere <simon.chao@fmr.com>
Subject: Re: Omitting data from a single line..
Message-Id: <b9defba7-ff39-45d9-abc5-d7b94aaf7952@e6g2000prf.googlegroups.com>

On Nov 27, 12:49 am, xhos...@gmail.com wrote:
> Cranky <pr...@private.com> wrote:
> > I have files that are 1 liners that have data that I would like to
> > sed, grep, or awk out.  Here is an example:
>
> Are you aware that this is Perl newsgroup, not an sed, awk, or grep one?
>
> ...
>
> > Basically I would like to omit everything between the <description>
> > and </description> tags.  As far as the "description" tags themselves,
> > it really doesn't matter whether those go or not so long as everything
> > in between them does.
>
> In Perl:
>
> $line =~ s/<description>.*?<\/description>//g;
>
> There are various gotcha that occur with this.  In general it is a good
> idea to use an XML processor to process XML, but I do do things like this
> occasionally and generally get away with it.


This is probably the simplest method of removing the description,
although, as you hint at, there are issues--nested descriptions for
instance. However, based on the OP's example, that doesn't appear to
be a concern. I'm not absolutely certain about that, but am reasonably
confident.


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

Date: 27 Nov 2007 15:46:54 +0100
From: Lars Haugseth <njus@larshaugseth.com>
Subject: Re: page 124 of the camel book
Message-Id: <m14pf7d9up.fsf@vader.eiendomsnett.no>


* "jl_post@hotmail.com" <jl_post@hotmail.com> wrote:
> 
> On Nov 25, 10:14 pm, "Wade Ward" <w...@zaxfuuq.net> wrote:
> >
> > I think that the syntax I'm looking for is on page 124 in the first
> > two code snippets:
> >
> >     {
> >     do {
> >           last if $x = $y ** 2;
> >           # do something here
> >     }  while $x++ <= $z;
> >
> > }
> >
>
[snip]
>
> 
> > 2)  What are the outside curly braces doing here?  What do they
> >     belong to?
> 
> The outside curly braces are to provide a way of controlling where the
> "last" keyword will jump to if/when it is invoked.  "last" will NOT
> jump to the end of the innermost braces since they belong to a
> do{}while loop.  Therefore, an extra set of curly braces (surrounding
> the do{}while loop) is needed to completely jump out of the loop.

I find it more readable to put the extra set of curly braces
inside the do-while:

  do {{ # allow early exit using 'last'
      last if $x = $y ** 2;
      # do something here
  }} while $x++ <= $z;

Then again, I find it even more readable to avoid using do-while
altogether...

-- 
Lars Haugseth

"If anyone disagrees with anything I say, I am quite prepared not only to
 retract it, but also to deny under oath that I ever said it." -Tom Lehrer


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

Date: Tue, 27 Nov 2007 08:24:18 -0800 (PST)
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: page 124 of the camel book
Message-Id: <0adffdf2-93fb-4867-bca7-23d460e606c1@s19g2000prg.googlegroups.com>

On Nov 27, 7:46 am, Lars Haugseth <n...@larshaugseth.com> wrote:
>
> I find it more readable to put the extra set of curly braces
> inside the do-while:
>
>   do {{ # allow early exit using 'last'
>       last if $x = $y ** 2;
>       # do something here
>   }} while $x++ <= $z;


   That won't work, Lars.  With "last", the braces have to be outside
the do{}while loop, or else the do{}while loop will keep iterating.
Let me explain with an example:

      my $x = 0;
      do {{
             last  if $x == 2;
             print $x;
         }} while $x++ < 5;

This looks like we want to break out of the loop when $x equals 2, but
in fact we just jump to the end of it, and let the while() condition
"re-loops" us back to the top of the loop, setting $x to 3.  In this
case, "last" can be replaced with "next" and nothing will change.
Basically, the above code will print "01345".  ("345" is printed
because the loop is never completely broken out of when $x == 2.)

   If we want to completely break out of the loop when $x is 2, we
would write something like this instead:

      my $x = 0;
      {
         do {
               last  if $x == 2;
               print $x;
            } while $x++ < 5;
      }

This code prints only "01", showing that the loop is fully exited when
$x is 2.


> Then again, I find it even more readable to avoid using do-while
> altogether...


   Yes, do{}while loops can be a bit confusing, but they're not that
bad if you avoid using "next" and "last" (and "redo") in them.  But if
you really want to use them inside a do{}while loop, you can; you just
have to use one of the work-arounds mentioned on page 124 of the Camel
book.

   -- Jean-Luc


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

Date: 27 Nov 2007 17:45:22 +0100
From: Lars Haugseth <njus@larshaugseth.com>
Subject: Re: page 124 of the camel book
Message-Id: <m1zlwzbpst.fsf@vader.eiendomsnett.no>


* "jl_post@hotmail.com" <jl_post@hotmail.com> wrote:
> 
> On Nov 27, 7:46 am, Lars Haugseth <n...@larshaugseth.com> wrote:
> >
> > I find it more readable to put the extra set of curly braces
> > inside the do-while:
> >
> >   do {{ # allow early exit using 'last'
> >       last if $x = $y ** 2;
> >       # do something here
> >   }} while $x++ <= $z;
> 
> 
>    That won't work, Lars.  With "last", the braces have to be outside
> the do{}while loop, or else the do{}while loop will keep iterating.

You are right. I wrote a quick test before I posted, modifying a
condition "next if $x > 2" to use last instead of next, and as the
rest of the loop wasn't executed I assumed it work. My bad, sorry!

-- 
Lars Haugseth

"If anyone disagrees with anything I say, I am quite prepared not only to
 retract it, but also to deny under oath that I ever said it." -Tom Lehrer


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

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 V11 Issue 1070
***************************************


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