[16979] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4391 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Sep 20 21:06:03 2000

Date: Wed, 20 Sep 2000 18:05:11 -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: <969498311-v9-i4391@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 20 Sep 2000     Volume: 9 Number: 4391

Today's topics:
    Re: Candidate for the top ten perl mistakes list <glynFOOdwr@FSCKdeleteEmeD.co.uk>
    Re: Candidate for the top ten perl mistakes list <amonotod@netscape.net>
    Re: Candidate for the top ten perl mistakes list <amonotod@netscape.net>
    Re: Candidate for the top ten perl mistakes list (Tim)
    Re: Candidate for the top ten perl mistakes list (Martien Verbruggen)
    Re: Candidate for the top ten perl mistakes list (Abigail)
    Re: Candidate for the top ten perl mistakes list (Abigail)
    Re: Candidate for the top ten perl mistakes list <glynFOOdwr@FSCKdeleteEmeD.co.uk>
    Re: Candidate for the top ten perl mistakes list <glynFOOdwr@FSCKdeleteEmeD.co.uk>
    Re: Candidate for the top ten perl mistakes list <lr@hpl.hp.com>
    Re: Candidate for the top ten perl mistakes list (Martien Verbruggen)
    Re: Candidate for the top ten perl mistakes list (Clinton A. Pierce)
    Re: Hash jelp for Newbie <juex@deja.com>
    Re: Help with complex regexp <yanick@babyl.sympatico.ca>
        help! cookies that work with www.domain.com and .domain <sstalzerNOSPAM@bellatlantic.net>
    Re: How to Capture Specific Whitespace? (Martien Verbruggen)
    Re: Interpolation on the fly (Dave Sherohman)
    Re: Interpolation on the fly <glynFOOdwr@FSCKdeleteEmeD.co.uk>
    Re: Interpolation on the fly (Dave Sherohman)
    Re: Joining strings (Martien Verbruggen)
    Re: Joining strings <anmcguire@ce.mediaone.net>
    Re: literal substitution <lr@hpl.hp.com>
    Re: newbie string chopping question <rickyfusion@nospamfusionserver.com>
    Re: newbie string chopping question <lr@hpl.hp.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Wed, 20 Sep 2000 23:15:19 +0100
From: "Glyndwr" <glynFOOdwr@FSCKdeleteEmeD.co.uk>
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <%Day5.2267$Rk1.38792@news2-win.server.ntlworld.com>

"Henry Hartley" <hartleh1@westat.com> wrote in message
news:39C92D63.DE30C8B2@westat.com...
> Russ Jones wrote:
> >
> > How often have you seen the word "seldomly?"
>
> Seldomly.
> :)

Implied humour ruined ;o)

--
                                           -=G=-
print join " ",reverse split /\s+/,'hacker. Perl another Just',"\n";
Web: http://www.fscked.co.uk                             ICQ: 66545073




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

Date: Wed, 20 Sep 2000 22:09:20 GMT
From: amonotod <amonotod@netscape.net>
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <8qbchu$p0k$1@nnrp1.deja.com>

In article <39C8EFF3.F5FE3BA4@rac.ray.com>,
  Russ Jones <russ_jones@rac.ray.com> wrote:
> Abigail wrote:
> > || Seldomly?
> >
> > How often do you see a program here where the cause of something not
> > working was the use of @array[$index] vs $array[$index]?
> >
>
> How often have you seen the word "seldomly?"

Nonfrequent.

Heh,
amonotod

--
    `\|||/                     amonotod@
      (@@)                     netscape.net
  ooO_(_)_Ooo________________________________
  _____|_____|_____|_____|_____|_____|_____|_____|


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Wed, 20 Sep 2000 22:07:06 GMT
From: amonotod <amonotod@netscape.net>
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <8qbcdo$ovb$1@nnrp1.deja.com>

In article <x7n1h2dcx8.fsf@home.sysarch.com>,
  Uri Guttman <uri@sysarch.com> wrote:
> >>>>> "G" == Godzilla!  <godzilla@stomp.stomp.tokyo> writes:
>   G> Being a Perl 5 Cargo Cultist Copy & Paste Specialist
>   G> and believing yourself to be a programmer.
> 1. being morozilla.
> 2. listening to moronzilla.
> 3. following up to moronzilla (i am guilty)
> 4. some dumbass school that claims moronzilla went there
> 5. comp.lang.perl.moderated for not being popular enough that we could
>    filter out moronzilla
> 6. the web as that is what moronzilla thinks it the net and all perl
>    is good for
<snip>

7.  Troubleshooting modem connections on a server in order to fix memory
problems.

amonotod

--
    `\|||/                     amonotod@
      (@@)                     netscape.net
  ooO_(_)_Ooo________________________________
  _____|_____|_____|_____|_____|_____|_____|_____|


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Wed, 20 Sep 2000 22:30:30 GMT
From: SPAM+indigo@dimensional.com (Tim)
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <8FB5A2FC5indigodimcom@166.93.207.145>

logan@cs.utexas.edu (Logan Shaw) wrote in
<8qbb6t$ibl$1@cheddar.cs.utexas.edu>: 

>In article <3U9y5.2124$Rk1.35465@news2-win.server.ntlworld.com>,
>Glyndwr <glynFOOdwr@FSCKdeleteEmeD.co.uk> wrote:
>>Drifting slightly off topic, I always - without fail - get the operands
>>for join(@foo, ",")[*] around the wrong way. Same problem with ln under
>>*Nix. Total mental block.
>
>join() is a bit trickier, but it makes sense if you try to imagine
>implementing join() yourself.  Is this
>
>     sub join
>          {
>          my (@things, $separator) = @_;
>
>          # . . .
>          }
>
>going to work out very well?  Nope -- the arguments will all get sucked
>into @things and $separator will be undef.

Then please explain the mnemonic for push @array, $value :)

I remember join just fine...push and unshift mess me up.

-T


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

Date: Wed, 20 Sep 2000 22:54:35 GMT
From: mgjv@verbruggen.comdyn.com.au (Martien Verbruggen)
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <slrn8sig0r.hc9.mgjv@verbruggen.comdyn.com.au>

\begin{offtopic}

On Wed, 20 Sep 2000 22:59:23 +0100,
	Glyndwr <glynFOOdwr@FSCKdeleteEmeD.co.uk> wrote:
> 
> Yeah, I know, but I still manage to muck it up. In fact (drifting further
> off topic) I did something weird today, which was this:
> 
> bash [~] ln -s Foo /usr/local/lib/site_perl/Foo
> bash [~] cd /usr/local/lib/site_perl/Foo
> bash [/usr/local/lib/site_perl/Foo] ls -la
> (loads of files)
> lrwxrwxrwx    1 rgaywood staff           8 Sep 20 14:34 Foo -> Foo
> 
> "Oopsie," I said, "I've done it the wrong way around. Best get rid of that
> then."
> 
> bash [/usr/local/lib/site_perl/Foo] rm Foo
> bash [/usr/local/lib/site_perl/Foo] ls -la
> total 0

Totally broken behaviour. That should not happen. Are you sure that is
what you did? You weren't actually in /usr/local/lib/site_perl and did 

rm -rf Foo

?

> Somehow, the rm command blew away everything in the directory, not just the
> bogus symlink. Thank heavens for sharp sysadmins with undelete programs
> (thanks, Dafydd, if you're reading this :o))

Maybe the sharp undelete stuff broke the real rm that should be on
your system? rm is not supposed to behave that way, and any rm that
does is severly and utterly broken.

All this is assuming that you don't have rm aliased to something more
destructive, and you were indeed where you said you were.

> I'm still puzzled as to why that happened.

So am I. None of it should happen, and if it did indeed happen (i.e.
you can reproduce it), it should be considered a bug of a major order.

\end{offtopic}

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | Can't say that it is, 'cause it
Commercial Dynamics Pty. Ltd.   | ain't.
NSW, Australia                  | 


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

Date: 20 Sep 2000 22:56:02 GMT
From: abigail@foad.org (Abigail)
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <slrn8sig1q.5fq.abigail@alexandra.foad.org>

Andrew N. McGuire  (anmcguire@ce.mediaone.net) wrote on MMDLXXVII
September MCMXCIII in <URL:news:Pine.LNX.4.21.0009201359140.3145-100000@hawk.ce.mediaone.net>:
\\ On Wed, 20 Sep 2000, Larry Rosler quoth:
\\ 
\\ LR> In article <Pine.GSO.4.21.0009201131470.15465-100000@crusoe.crusoe.net> 
\\ LR> on Wed, 20 Sep 2000 11:32:33 -0400, Jeff Pinyan <jeffp@crusoe.net> 
\\ LR> says...
\\ LR> > >> 1: didn't use -w
\\ LR> > >> 2: didn't use strict
\\ LR> > >> 3: didn't test the results of an "open"
\\ LR> > >> 4: listened to anything godzilla said
\\ LR> > >  5: didn't read perldoc -f localtime
\\ LR> >    6: uses @array[$index] where $array[$index] should be used
\\ LR> 
\\ LR>      1: uses split '|' or (less likely by far) /|/
\\ 
\\ 
\\ A couple more to potentially add:
\\ 
\\          7:  Needlessly quotes ~EVERYTHING~.
\\          8:  Uses s/// where tr/// would be better.


However, in most cases, none of them are actually mistakes.



Abigail
-- 
perl -MLWP::UserAgent -MHTML::TreeBuilder -MHTML::FormatText -wle'print +(
HTML::FormatText -> new -> format (HTML::TreeBuilder -> new -> parse (
LWP::UserAgent -> new -> request (HTTP::Request -> new ("GET",
"http://work.ucsd.edu:5141/cgi-bin/http_webster?isindex=perl")) -> content))
=~ /(.*\))[-\s]+Addition/s) [0]'


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

Date: 20 Sep 2000 22:58:59 GMT
From: abigail@foad.org (Abigail)
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <slrn8sig7d.5fq.abigail@alexandra.foad.org>

Tim (SPAM+indigo@dimensional.com) wrote on MMDLXXVII September MCMXCIII
in <URL:news:8FB5A2FC5indigodimcom@166.93.207.145>:
'' 
'' Then please explain the mnemonic for push @array, $value :)

push works on arrays, join on lists.

mnemonic: one can join mailistslist, but you can't push them. ;-)


Abigail
-- 
package Just_another_Perl_Hacker; sub print {($_=$_[0])=~ s/_/ /g;
                                      print } sub __PACKAGE__ { &
                                      print (     __PACKAGE__)} &
                                                  __PACKAGE__
                                            (                )


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

Date: Thu, 21 Sep 2000 00:18:00 +0100
From: "Glyndwr" <glynFOOdwr@FSCKdeleteEmeD.co.uk>
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <Lyby5.2393$Rk1.42298@news2-win.server.ntlworld.com>

"Martien Verbruggen" <mgjv@verbruggen.comdyn.com.au> wrote in message
news:slrn8sig0r.hc9.mgjv@verbruggen.comdyn.com.au...
> \begin{offtopic}

Ah, left that bit out.

> On Wed, 20 Sep 2000 22:59:23 +0100,
> Glyndwr <glynFOOdwr@FSCKdeleteEmeD.co.uk> wrote:
> >
> > Yeah, I know, but I still manage to muck it up. In fact (drifting
further
> > off topic) I did something weird today, which was this:
> >
> > bash [~] ln -s Foo /usr/local/lib/site_perl/Foo
> > bash [~] cd /usr/local/lib/site_perl/Foo
> > bash [/usr/local/lib/site_perl/Foo] ls -la
> > (loads of files)
> > lrwxrwxrwx    1 rgaywood staff           8 Sep 20 14:34 Foo -> Foo
> >
> > "Oopsie," I said, "I've done it the wrong way around. Best get rid of
that
> > then."
> >
> > bash [/usr/local/lib/site_perl/Foo] rm Foo
> > bash [/usr/local/lib/site_perl/Foo] ls -la
> > total 0
>
> Totally broken behaviour. That should not happen. Are you sure that is
> what you did? You weren't actually in /usr/local/lib/site_perl and did

Yes. I went back through the PuTTY windows afterwards and cut&pasted the
code.

> rm -rf Foo
> ?

I *definitely* only typed "rm Foo".

> > Somehow, the rm command blew away everything in the directory, not just
the
> > bogus symlink. Thank heavens for sharp sysadmins with undelete programs
> > (thanks, Dafydd, if you're reading this :o))
>
> Maybe the sharp undelete stuff broke the real rm that should be on
> your system? rm is not supposed to behave that way, and any rm that
> does is severly and utterly broken.

Yes, I tend to agree. The system is Debian 2.2, BTW (I think that's the
right version - Potato).

> All this is assuming that you don't have rm aliased to something more
> destructive, and you were indeed where you said you were.

I was, and I have rm aliased to "rm -i". I left out the "delete Foo, y/n"
bit when I retyped the log I have.

> > I'm still puzzled as to why that happened.
>
> So am I. None of it should happen, and if it did indeed happen (i.e.
> you can reproduce it), it should be considered a bug of a major order.

Yes. I tried briefly, but couldn't get it to happen again; I wonder in
hindsight if it's related to the fact that the symlink crossed a filesystem.
I might try again (a bit harder) tomorrow. I'll post back, if anyone's
interested (and doesn't think it's too off-topic :o))


--
                                           -=G=-
print join " ",reverse split /\s+/,'hacker. Perl another Just',"\n";
Web: http://www.fscked.co.uk                             ICQ: 66545073




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

Date: Thu, 21 Sep 2000 00:19:13 +0100
From: "Glyndwr" <glynFOOdwr@FSCKdeleteEmeD.co.uk>
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <Uzby5.2397$Rk1.42336@news2-win.server.ntlworld.com>

"Tim" <SPAM+indigo@dimensional.com> wrote in message
news:8FB5A2FC5indigodimcom@166.93.207.145...
> logan@cs.utexas.edu (Logan Shaw) wrote in
> Then please explain the mnemonic for push @array, $value :)
>
> I remember join just fine...push and unshift mess me up.

Now, I've never had a problem with push/pop/shift/unshift. I guess that's
just the way I find logical, which explains why I have problems with join.

Course, Murphy's Law says I'll forget how to do those tomorrow :o)

--
                                           -=G=-
print join " ",reverse split /\s+/,'hacker. Perl another Just',"\n";
Web: http://www.fscked.co.uk                             ICQ: 66545073




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

Date: Wed, 20 Sep 2000 16:17:45 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <MPG.1432e5732eb2f63e98ada4@nntp.hpl.hp.com>

In article <8FB5A2FC5indigodimcom@166.93.207.145> on Wed, 20 Sep 2000 
22:30:30 GMT, Tim <SPAM+indigo@dimensional.com> says...
> logan@cs.utexas.edu (Logan Shaw) wrote in
> <8qbb6t$ibl$1@cheddar.cs.utexas.edu>: 
> 
> >In article <3U9y5.2124$Rk1.35465@news2-win.server.ntlworld.com>,
> >Glyndwr <glynFOOdwr@FSCKdeleteEmeD.co.uk> wrote:
> >>Drifting slightly off topic, I always - without fail - get the operands
> >>for join(@foo, ",")[*] around the wrong way.

You won't go wrong if you use the fat comma to separate 
operator+specifier from operands, as Abigail and I promote (also for 
pack/unpack, sprintf, et al):

    join ',' => @foo;

> Then please explain the mnemonic for push @array, $value :)
> 
> I remember join just fine...push and unshift mess me up.

That's why I would never write (as I have seen written here):

    push @array => $value; # UGH!!!

The arrow points the wrong way.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Wed, 20 Sep 2000 23:35:41 GMT
From: mgjv@verbruggen.comdyn.com.au (Martien Verbruggen)
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <slrn8siidt.hc9.mgjv@verbruggen.comdyn.com.au>

On Wed, 20 Sep 2000 16:17:45 -0700,
	Larry Rosler <lr@hpl.hp.com> wrote:
>
> You won't go wrong if you use the fat comma to separate 
> operator+specifier from operands, as Abigail and I promote (also for 
> pack/unpack, sprintf, et al):
> 
>     join ',' => @foo;
> 
> > Then please explain the mnemonic for push @array, $value :)
> > 
> > I remember join just fine...push and unshift mess me up.
> 
> That's why I would never write (as I have seen written here):
> 
>     push @array => $value; # UGH!!!
> 
> The arrow points the wrong way.

Which means that people might remember the mnemonic incorrectly, and
start doing things like

push $value => @array;

Of course, this generates errors, and they'll quickly fix those.

While I agree that the fat comma works in many cases, and makes some
code look more elegant, it doesn't actually help in remembering the
order of arguments in a generic case, or does it? I think realising
that one works with lists, and the other with arrays is a more
important thing, although, for beginners, it also is going to be
harder to grasp.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | Hi, John here, what's the root
Commercial Dynamics Pty. Ltd.   | password?
NSW, Australia                  | 


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

Date: Thu, 21 Sep 2000 00:55:52 GMT
From: clintp@geeksalad.org (Clinton A. Pierce)
Subject: Re: Candidate for the top ten perl mistakes list
Message-Id: <s0dy5.1995$hD4.575937@news1.rdc1.mi.home.com>

[Posted and mailed]

In article <Pine.GSO.4.21.0009201131470.15465-100000@crusoe.crusoe.net>,
	Jeff Pinyan <jeffp@crusoe.net> writes:
>>> 1: didn't use -w
>>> 2: didn't use strict
>>> 3: didn't test the results of an "open"
>>> 4: listened to anything godzilla said
>>  5: didn't read perldoc -f localtime
>    6: uses @array[$index] where $array[$index] should be used

Based on several years of teaching perl, looking at code written by 
newbies, feedback from a book, and posting to Usenet...

"purl, clintp's top ten list?"

	Top ten mistakes newbies make 

	10. Forgot chomp() 
	9. debugging the wrong thing (XY problem) 
	8. didn't read FAQ or TFM 
	7. confused =/==/eq 
	6. wrong quotes 
	5. spelling errors 
	4. wrong career choice 
	3. forgot "use strict"  
	2. ignored return value 
	1. forgot -w

-- 
    Clinton A. Pierce              Teach Yourself Perl in 24 Hours! 
  clintp@geeksalad.org         for details see http://www.geeksalad.org
"If you rush a Miracle Man, 
	you get rotten Miracles." --Miracle Max, The Princess Bride


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

Date: Wed, 20 Sep 2000 16:21:03 -0700
From: "Jürgen Exner" <juex@deja.com>
Subject: Re: Hash jelp for Newbie
Message-Id: <39c94660$1@news.microsoft.com>

"Shawn and Francine" <flsq@home.com> wrote in message
news:39F04F0C.3C5830E5@home.com...
> >           $Info{$Number} = $Name;
> >
> > [snip]

> Ok thanks that worked, but doesn't that creat3 a scalar instead of a hash?

But that is what you want! You want to add a scalar value to the hash, you
don't want to add a hash to the hash, do you?

jue





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

Date: Wed, 20 Sep 2000 22:14:59 GMT
From: Yanick Champoux <yanick@babyl.sympatico.ca>
Subject: Re: Help with complex regexp
Message-Id: <DFay5.255844$Gh.6391088@news20.bellglobal.com>

In comp.lang.perl.misc Jeroen Konijn <news@jeroen.org> wrote:
:> : Yeah, I already noticed it. But I have to put in somewhere in a PHP
: script.
:> : Like:
:>
:> :   $dummy = ereg_replace($ereg_string, $text);
:>
:> : The question remains how, since I know almost nothing about perl.
:>
:> I will probably ask something very stupid here, and you have my
:> permission to laugh at my naivete, but in a PHP script shouldn't you
:> program in, well, PHP?

: Sure. As I said, I'm now how it can be done in Perl. But since I have to use
: a regexp-string because of PHP, I'm looking for a way to do this. How do I
: transform the Perl-snippet into a nice regexp string that will do the same
: thing - if this is possible.

For some reason, just a hunch, really, I suspect the PHP newsgroup would 
better equipped to answer you on that...

Good luck,
Yanick

PS: there is always Plan B: install HTML::Embperl. :)


-- 
eval" use 'that poor Yanick' ";
print map{ (sort keys %{{ map({$_=>1}split'',$@) }})[hex] }
qw/8 b 15 1 9 10 11 15 c b 13 1 12 b 13 f 1 c 9 a e b 13 0/;


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

Date: Wed, 20 Sep 2000 23:22:57 GMT
From: Steve <sstalzerNOSPAM@bellatlantic.net>
Subject: help! cookies that work with www.domain.com and .domain.com in Netscape??
Message-Id: <200920001923157002%sstalzerNOSPAM@bellatlantic.net>

I need to set a cookie (a user id) which will be passed along whether a
user visits us as "www.townvalues.com" or "townvalues.com". If I set
the cookie using a link to a script such as 

http://townvalues.com/cgi/cgibin/cookieset2.cgi

then I can get the cookie easily with 

http://townvalues.com/cgi/cgibin/cookieget2.cgi

Unfortunately, in Netscape I CAN'T seem to be able to read that same
cookie with this:
http://www.townvalues.com/cgi/cgibin/cookieget2.cgi

Now, I thought the way to do this was simply to set the domain of the
cookie to the "tail" that is common to both domains, e.g.
".townvalues.com". However, if you test out my scripts, this does not
seem to work in Netscape, although it works fine in Internet Explorer.

I've been working on this for countless hours, and can't seem to find a
solution - any help would be appreciated!!

Here is the code I'm using for setting and getting the cookie:

# cookieset2.cgi
$expDate = "Sunday, 31-Dec-00 12:00:00 GMT";
$domain = ".townvalues.com";
$path = "/";
$uid=99999;
$cookie_string = "Set-Cookie: townvalues_uid=$uid; expires=$expDate;
domain=$domain";
print $cookie_string,"\n";
print "Content-type: text/html\n\n";

#cookieget2.cgi
print "<p>HTTP_COOKIE is: $ENV{'HTTP_COOKIE'}\n";


MANY THANKS!!

Steve
------
www.newmediacreations.com
"Online Advertising Solutions for The Smaller Business"



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

Date: Wed, 20 Sep 2000 22:27:47 GMT
From: mgjv@verbruggen.comdyn.com.au (Martien Verbruggen)
Subject: Re: How to Capture Specific Whitespace?
Message-Id: <slrn8sieej.hc9.mgjv@verbruggen.comdyn.com.au>

On Wed, 20 Sep 2000 17:47:28 GMT,
	beach9000@hotmail.com <beach9000@hotmail.com> wrote:
> I'm looking to 'capture' to amount of white space before a piece of
> text.
> 
> ie
>    garbagetext      markertext
>               ^----|
> or
>    [start of file]           markertext
>    ^------------------------|
> or
>    blah blah blah    blah    markertext
>                          ^--|
> 
> so that I could reproduce the whitespace, either by simply copying the
> space, or know how many characters of whitespace exists.

The other answer in this thread is probably what you're looking for,
but maybe using split with capturing parens around the split regex
would be helpful as well. The following will split a string in things
separated by whitespace, and capture the whitespace itself, resulting
in an alternating list:

#!/usr/local/bin/perl -w
use strict;

$/ = "";

while (<DATA>)
{
	my @f = split /(\s+)/;
	print join ":", @f;
	print "---\n";
}


__DATA__
  A paragraph with some leading whitespace     and
  some trailing as well, which you won't see   

And some other text
---CUT HERE---

$  /tmp/foo.pl
:  :A: :paragraph: :with: :some: :leading: :whitespace:     :and:
  :some: :trailing: :as: :well,: :which: :you: :won't: :see:   

---
And: :some: :other: :text:
---

The Null initial field is there because split just works that way (see
perldoc -f split), which means you can rely on the fact that the odd
elements (i.e. the ones with an even index) are always the data you
split out with a possible leading empty field, and the even (i.e. odd
index) are the separating fields.

As you can see you end up with all the whitespace preserved in the
spots where it should appear.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | Little girls, like butterflies, need
Commercial Dynamics Pty. Ltd.   | no excuse - Lazarus Long
NSW, Australia                  | 


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

Date: Wed, 20 Sep 2000 22:08:33 GMT
From: esper@news.visi.com (Dave Sherohman)
Subject: Re: Interpolation on the fly
Message-Id: <slrn8sidb1.npj.esper@pchan.dojo>

On Wed, 20 Sep 2000 21:44:44 GMT, Gwyn Judd <tjla@guvfybir.qlaqaf.bet> wrote:
> No you should be right to use interpolation. The magic you are looking
> for is called "eval". See the documentation:
> 
> perldoc -f eval
> 
> $fmt = '"User: $user\n"';
> print eval $fmt;
> $user = 'tom';
> print eval $fmt;

Took a little rearranging (I'm actually reading $fmt from a config file, so
adding the quotes and newline to it doesn't work out that well...), but I
finally came up with

eval "print \"$fmt\n\"";

which works.  Thanks!

-- 
"Two words: Windows survives." - Craig Mundie, Microsoft senior strategist
"So does syphillis. Good thing we have penicillin." - Matthew Alton
Geek Code 3.1:  GCS d- s+: a- C++ UL++$ P+>+++ L+++>++++ E- W--(++) N+ o+
!K w---$ O M- V? PS+ PE Y+ PGP t 5++ X+ R++ tv b+ DI++++ D G e* h+ r++ y+


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

Date: Wed, 20 Sep 2000 23:17:50 +0100
From: "Glyndwr" <glynFOOdwr@FSCKdeleteEmeD.co.uk>
Subject: Re: Interpolation on the fly
Message-Id: <lGay5.2275$Rk1.38673@news2-win.server.ntlworld.com>

"Dave Sherohman" <esper@news.visi.com> wrote in message
news:slrn8sidb1.npj.esper@pchan.dojo...
> Took a little rearranging (I'm actually reading $fmt from a config file,
so
> adding the quotes and newline to it doesn't work out that well...), but I
> finally came up with
>
> eval "print \"$fmt\n\"";

Something I do a lot I'd like some feedback on from people: I'd write that
as

eval qq[print "$frm\n"];

because I find that more readable. Any reason not to do that?

--
                                           -=G=-
print join " ",reverse split /\s+/,'hacker. Perl another Just',"\n";
Web: http://www.fscked.co.uk                             ICQ: 66545073




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

Date: Wed, 20 Sep 2000 22:18:09 GMT
From: esper@news.visi.com (Dave Sherohman)
Subject: Re: Interpolation on the fly
Message-Id: <slrn8sidt0.npj.esper@pchan.dojo>

On Wed, 20 Sep 2000 21:55:44 GMT, Jerome O'Neil <jerome@activeindexing.com> wrote:
> Which is exactly what you should get.  Single quotes are not
> interpolated, so your scalar $user is not being treated as such.
>
> #!/usr/local/bin/perl -w
> use strict;
> 
> my $singles = 'User: $user';

Using double-quotes attempts to interpolate on assignment, not on use.
With "use strict", that results in an error ($user hasn't been declared
yet) if you do it here.

> my $user = 'joe';
> my $dubs = "User: $user";

Again, this is interpolating on assignment and changing $user will leave
$dubs untouched.

> print $dubs;

The entire point of my question was to get this to print different things if
$user was changed without having to redo the assignment of $dubs.

> > Can this be done using variable interpolation or will I have to resort to
> > copying $fmt and using s/// to explicitly insert the name each time?
> 
> Nope.  You should read up on Perl syntax, though.  

Yes, it can be done, using eval.  And I certainly tried reading up on perl
syntax, but was only able to find information on performing interpolation
when a value is assigned, not on interpolating when the value is referenced.

-- 
"Two words: Windows survives." - Craig Mundie, Microsoft senior strategist
"So does syphillis. Good thing we have penicillin." - Matthew Alton
Geek Code 3.1:  GCS d- s+: a- C++ UL++$ P+>+++ L+++>++++ E- W--(++) N+ o+
!K w---$ O M- V? PS+ PE Y+ PGP t 5++ X+ R++ tv b+ DI++++ D G e* h+ r++ y+


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

Date: Wed, 20 Sep 2000 22:32:41 GMT
From: mgjv@verbruggen.comdyn.com.au (Martien Verbruggen)
Subject: Re: Joining strings
Message-Id: <slrn8sienp.hc9.mgjv@verbruggen.comdyn.com.au>

On Wed, 20 Sep 2000 13:51:53 -0700,
	Larry Rosler <lr@hpl.hp.com> wrote:
> In article <4a01294e63g.soper@soundhouse.co.uk> on Wed, 20 Sep 2000 
> 21:30:41 +0100, Geoff Soper <g.soper@soundhouse.co.uk> says...
> > I have a string which contains the location of a directory:
> > 
> > $location = "/home/database/";

[snip]

> > Similarly to open the first file will the following work?
> > 
> > open (FILE, ">$location . 'file'");
> > 
> > What's the correct way to join two strings to achieve what I require?
> 
>   open FILE, ">${location}file" or
>     die "Couldn't open '${location}file'. $!\n";
> 
> The curly brackets separate the symbol $location from the desired 
> suffix.  Many would leave the trailing '/' off of $location, and use it 
> as a separator:
> 
>          ... ">$location/file" ...

Also note that it almost never is a problem to double up slashes in a
path on Unix. The Unixes and derivates I've worked on all are happy to
accept "$location/file" as a path where the contents of $location end
with a slash. I don't know what happens on Windows/DOS or others, but
any portable solution would use File::Spec anyway.

Now.. I haven't actually worked on more than about 5 or 6 different
Unixes, so in general this might not be true. I don't have a copy of
POSIX here.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | This matter is best disposed of from
Commercial Dynamics Pty. Ltd.   | a great height, over water.
NSW, Australia                  | 


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

Date: Wed, 20 Sep 2000 17:38:37 -0500
From: "Andrew N. McGuire " <anmcguire@ce.mediaone.net>
Subject: Re: Joining strings
Message-Id: <Pine.LNX.4.21.0009201723060.3179-100000@hawk.ce.mediaone.net>

On Wed, 20 Sep 2000, Geoff Soper quoth:

GS> I have a string which contains the location of a directory:
GS> 
GS> $location = "/home/database/";
GS> 
GS> Later on I want to take the file '/home/database/file' and rename it to
GS> '/home/database/new_name'. I was under the impression that:
GS> 
GS> rename ($location . "file",$location . "new_name");

That works perfectly, at least the following script works on my
machine:

#!/usr/bin/perl -w
use strict;

my $dir = '/home/anm/';
rename($dir . "test", $dir . "newfile");
__END__

If you are getting your filename from some file, or via backticks,
remember to chomp off the newline, like so:

#!/usr/bin/perl -w
use strict;

my $dir    = '/home/anm/';
my $file   = `ls test`;
my $target = 'newfile';

chomp $file;
rename($dir . $file, $dir . $target);
__END__

[ snip ]

GS> Similarly to open the first file will the following work?
GS> 
GS> open (FILE, ">$location . 'file'");

No, that will create a horrible file in $location named:

 . 'file'

You could say:

open FILE, ">${location}file" or die $!;

but for style purposes I would go through the trouble of one more
line of code that concatenated $location and 'file' into one,
hopefully more nmeumonic identifier.  That is a matter of preference,
I suppose.

More importantly, note that the '>' in the open() call opens
the file for writing, and will clobber your file.  Since you
are renaming THAT file, I don't think that is what you want.

perldoc -f open     # for details
perldoc perlopentut # for more details

[ snip ]

anm
-- 
<(@)> ; $/ = q;;; for $" ( map $_ && chr() => split m~[\D+ <(@)>
<(@)> ]~ => <DATA> ) { print "@{ [ '' => '' ] }" } __END__ <(@)>
<(@)>   74 117 115 116 32 97 110 111 116 104 101 114 32    <(@)>
<(@)>      80 101 114 108 32 72 97 99 107 101 114 10       <(@)>



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

Date: Wed, 20 Sep 2000 16:33:56 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: literal substitution
Message-Id: <MPG.1432e93ddd3b45d98ada6@nntp.hpl.hp.com>

In article <969485551.107820@hpvablab.cup.hp.com> on 20 Sep 2000 
21:32:31 GMT, Richard J. Rauenzahn <nospam@hairball.cup.hp.com> says...
> 
> derf_up@yahoo.com writes:
> >for ($counter = 0; $counter <100; $counter++) {
> >	@array???$counter???
> >}
> >
> >I'm sure there are other ways around my "real" problem, but this is the
> >approach I want to take.  Any suggestions on the syntax for this
> >approach?  Thanks
> 
> Would you mind taking a different approach?  A list of lists would be
> the best way to do this.  'man perllol'.

Indeed.

> If you insist on using symbolic references (ick!!), then read up on the
> perlref man page.
> 
> Rich
> p.s. An easier way to express that for loop would be for (1..100).

Well, not exactly.

    for my $counter (0 .. 99) {

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Wed, 20 Sep 2000 17:45:52 -0400
From: "Ric Smith" <rickyfusion@nospamfusionserver.com>
Subject: Re: newbie string chopping question
Message-Id: <8qbb92$gf1$1@server.cntfl.com>


Thanks for the fishing lesson rather than giving me
the fish.

I came up with:

$string = "this is a long string of text";
$newstring = substr $string,0,10;

returns "this is a "

Does this look ok?

--
Ric Smith





> > Subject: newbie string chopping question
>
> "Newbie question"... OK I'll give you a fishing lesson.
>
> > Is there a function...
>
> Perl functions are listed in perldoc perlfunc.
>
> > to chop the characters off a string after a specified character?
>
> So you want to do string manuplation... so look in perlfunc and find
> the appropriate group.
>
>        Functions for SCALARs or strings
>             chomp, chop, chr, crypt, hex, index, lc, lcfirst,
>             length, oct, ord, pack, q/STRING/, qq/STRING/,
>             reverse, rindex, sprintf, substr, tr///, uc, ucfirst,
>             y///
>
> Now guess which one you need.  chop() and chomp() sound hopeful so you
> go RTFM and find that they don't do what you want.  So go back to the
> list and find the next best sounding candidate... "substr".  Now you
> RTFM for substr and discover it is the function you need.









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

Date: Wed, 20 Sep 2000 16:20:05 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: newbie string chopping question
Message-Id: <MPG.1432e5ff377472a298ada5@nntp.hpl.hp.com>

In article <8qbb92$gf1$1@server.cntfl.com> on Wed, 20 Sep 2000 17:45:52 
-0400, Ric Smith <rickyfusion@nospamfusionserver.com> says...
> 
> Thanks for the fishing lesson rather than giving me
> the fish.
> 
> I came up with:
> 
> $string = "this is a long string of text";
> $newstring = substr $string,0,10;
> 
> returns "this is a "
> 
> Does this look ok?

Do you want a grade, or will a gold star suffice?

But you don't get the gold star, because you posted Jeopardy style, 
contrary to the norms of this and many other newsgroups.

<Jeopardectomy>

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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 V9 Issue 4391
**************************************


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