[23207] in Perl-Users-Digest
Perl-Users Digest, Issue: 5428 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Sep 2 14:07:52 2003
Date: Tue, 2 Sep 2003 11:05:16 -0700 (PDT)
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, 2 Sep 2003 Volume: 10 Number: 5428
Today's topics:
Re: Arbitrarily Complex Data Structure (JR)
Re: Calling perl built-in functions <uri@stemsystems.com>
can't redirect output to a file?! <mattei@unice.fr>
Re: can't redirect output to a file?! <shondell@cis.ohio-state.edu>
Re: Effeciency question - perl scripts v's perl exe's <bigj@kamelfreund.de>
Re: Help -- how to execute "computed Perl code" <simon@unisolve.com.au>
Re: Help -- how to execute "computed Perl code" <uri@stemsystems.com>
Re: Help -- how to execute "computed Perl code" <uri@stemsystems.com>
Re: Help -- how to execute "computed Perl code" <uri@stemsystems.com>
Installing LWP fails (Lothar Fuhlrott)
Re: looking for a nedit Syntax Pattern file for perl <jf505@yahoo.de>
Reading a scalar line by line <dillon@SpamMinuSaccessdenied.darktech.org>
Re: Reading a scalar line by line <tore@aursand.no>
Re: Reading a scalar line by line <bharnish@technologist.com>
Re: Reading a scalar line by line <print split /!/"d!a!v!i!d!o!@!p!a!c!i!f!i!e!r!.!c!o!m!\n">
Re: Redirecting via LWP <someone@somewhere.com>
Re: Redirecting via LWP <flavell@mail.cern.ch>
Re: Replacing IP's in ./etc/hosts (Alex)
Re: Replacing IP's in ./etc/hosts (Alex)
Re: Replacing IP's in ./etc/hosts <bharnish@technologist.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 2 Sep 2003 07:24:16 -0700
From: jrolandumuc@yahoo.com (JR)
Subject: Re: Arbitrarily Complex Data Structure
Message-Id: <b386d54b.0309020624.3a45c47f@posting.google.com>
jrolandumuc@yahoo.com (JR) wrote in message news:<b386d54b.0308291231.4ca368cd@posting.google.com>...
> Brian McCauley <nobull@mail.com> wrote in message news:<u97k4xen8c.fsf@wcl-l.bham.ac.uk>...
> > jrolandumuc@yahoo.com (JR) writes:
> >
> > > Hi. Is it possible to create a subroutine to handle an arbitrarily
> > > complex data structure (for my purposes, complex only refers to hashes
> > > and arrays)?
> >
> > Yes, the ref operator and recursion.
> >
> > But looking at your code you knew that. You just executed it
> > exteemly carelessly.
> >
> > > ## The '|| "@_" =~ /HASH/' statement is needed because
> > > ## perl does not recognize the apparent address of
> > > ## HASH(0x1ab2eb4) with the ref function---this
> > > ## code can easily break.
> > > if (ref(@_) eq 'HASH' || "@_" =~ /HASH/) {
> >
> > ref(@_) will always be ''. The _number_ of elements in an array will
> > never be a reference (it will always be an integer).
> >
> > I suggest that you try looking at your code and following the logic.
> >
> > > sub T {
> > > if ( "@_" =~ /HASH/) {
> > > my $x = shift;
> > > while (my($k, $v) = each %$x) {
> > > if (ref($v) eq 'HASH') {
> > > print "\n$k\n";
> > > T($v);
> > > }
> > > else {
> > > print "$k=$v\n";
> > > }
> > > }
> > > }
> > > else {
> > > for (0..$#_) {
> > > my $e = $_[$_];
> > > if (ref($e) eq 'ARRAY') {
> > > T(@$e);
> > > }
> > > else {
> > > print "$e\n";
> > > }
> > > }
> > > }
> > > }
> >
> > So if _any_ of the arguments passed to T are hashrefs (or strings
> > containing 'HASH' you try to interpret the first argument as a hashref
> > and ignore the rest?
> >
> > Surely that's not what you intended!
> >
> > You probably meant something like.
> >
> > sub T {
> > for ( @_ ) {
> > if ( ref eq 'HASH' ) {
> > # Do stuff with %$_
> > } elsif ( ref eq 'ARRAY' ) {
> > T(@$_);
> > } else {
> > # Do stuff with $_
> > }
> > }
> > }
>
> Ahhh, I think I understand now how to do what I'm attempting. I
> couldn't get my head around it before, but after my last post, I think
> I now understand how to do it. It won't be easy, but it is definitely
> doable.
>
> Thanks for the response, Brian.
### I came up with and tested the below code on a few dozens complex data
### structures. As long as the subroutine was passed a reference, and
### the data structures contained either a scalar, code, hash, array,
### or ref, or references to these, the subroutine returned the contents
### of the data structure. This could be factored and improved upon, but
### is a decent second attempt, and seems to do the job I need it to do well.
### The Data::Dumper module does the same thing, but returns more
### information than I really need---I only need the contents
### (there's probably a way to turn off this extra info., I'm guessing).
#!/perl -w
use strict;
use diagnostics;
### Traverse data structures of varying complexity
my $c = 0;
sub traverse {
my $arg = shift;
## Initial argument must be a reference.
## If argument is passed a reference data structure,
## but not as a reference, this block will be circumvented,
## and the passed reference may not be completely traversed.
if (!$c && ref($arg) ne 'REF' &&
ref($arg) ne 'HASH' &&
ref($arg) ne 'ARRAY' &&
ref($arg) ne 'CODE' &&
ref($arg) ne 'SCALAR') {
die "Data structure must be passed as a reference.\n";
}
## Handle references to a reference
if (ref($arg) eq 'REF') {
## Try hash
eval { %$arg; };
traverse(%$arg) if (!$@);
## Try array
eval { @$arg; };
traverse(@$arg) if (!$@);
## Try scalar
eval { $$arg; };
traverse($$arg) if (!$@);
## Try code
eval { &$arg; };
traverse(&$arg) if (!$@);
}
elsif (ref($arg) eq 'HASH') {
for (%$arg) {
if (ref($_) eq 'HASH' ||
ref($_) eq 'ARRAY' ||
ref($_) eq 'SCALAR' ||
ref($_) eq 'CODE' ||
ref($_) eq 'REF' ) {
traverse($_);
}
else {
print $_, "\n";
}
}
}
elsif (ref($arg) eq 'ARRAY') {
for (@$arg) {
if (ref($_) eq 'HASH' ||
ref($_) eq 'ARRAY' ||
ref($_) eq 'SCALAR' ||
ref($_) eq 'CODE' ||
ref($_) eq 'REF' ) {
traverse($_);
}
else {
print $_, "\n";
}
}
}
elsif (ref($arg) eq 'SCALAR') {
if (ref($$arg) eq 'HASH' ||
ref($$arg) eq 'ARRAY' ||
ref($$arg) eq 'SCALAR' ||
ref($$arg) eq 'CODE' ||
ref($$arg) eq 'REF' ) {
traverse($$arg);
}
else {
print $$arg, "\n";
}
}
elsif (ref($arg) eq 'CODE') {
if (ref(&$arg()) eq 'HASH' ||
ref(&$arg()) eq 'ARRAY' ||
ref(&$arg()) eq 'SCALAR'||
ref(&$arg()) eq 'CODE' ||
ref(&$arg()) eq 'REF') {
traverse(&$arg);
}
else {
&$arg();
}
}
else {
warn "EXCEPTION: $arg\n";
}
$c++ if $c < 2;
}
my @nums = (10..20);
my %langs = (e=>"English", s=>"Spanish", r=>"Russian");
my $greeting = "Hello, World!";
sub greeting { my $arg = shift; return "Hello, $arg!"; }
my @matrix = (
[ 0, 1, 2 ],
[ 3, 4, 5 ],
[ 6, 7, 8 ],
);
my %hoh = (
hash1 => {
h1k1 => h1v1,
h1k2 => h1v2,
},
hash2 => {
h2k1 => h2v1,
h2k2 => h2v2,
},
hash3 => {
h3k1 => h3v1,
h3k2 => h3v2,
s3k3 => [@matrix],
},
);
my @aoh = (
{
key1 => val1,
key2 => val2,
},
{
key3 => val3,
key4 => val4,
},
{
key5 => val5,
key6 => val6,
},
);
my %hoa = (
hoa_key1 => [ "hoa_anon1", [ "hoa_int_anon1" ], "hoa_anon1a" ],
hoa_key2 => [ "hoa_anon2", [ "hoa_int_anon2" ], "hoa_anon2a" ],
hoa_key3 => [ "hoa_anon3", [ "hoa_int_anon3" ], "hoa_anon3a" ],
);
my %ds = (
scalar1 => \\\\\\\\\\\\\\\\$greeting,
sub1 => \\\\\\\\\\\\\\\\greeting("John"),
anon1 => [@nums],
hash1 => [%langs],
combo => [\\\\$greeting,[\\\\@nums,[\\\\%langs,[\\\\&greeting
("John")],],],],
aoa => [@matrix],
hoh => \\\\%hoh,
aoh => \\\\[@aoh],
hoa => \\\\[%hoa],
norefscalar => $greeting,
layeredstuct => [[%hoa, \\\[@aoh, \\\\[[%hoh],], [@matrix],],],],
);
traverse(\%ds);
------------------------------
Date: Tue, 02 Sep 2003 15:41:00 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Calling perl built-in functions
Message-Id: <x77k4rurjn.fsf@mail.sysarch.com>
>>>>> "SC" == Shawn Corey <shawn@magma.ca> writes:
SC> From my bag of dirty tricks:
very dirty.
SC> #!/usr/bin/perl
SC> $function='print';
SC> &$function("Hello, world\n");
SC> sub print {
SC> print @_;
SC> }
try that under use strict.
next!!
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
------------------------------
Date: Tue, 02 Sep 2003 17:30:51 +0200
From: Damien Mattei <mattei@unice.fr>
Subject: can't redirect output to a file?!
Message-Id: <bj2dh5$1be4$1@malibu.unice.fr>
i wrote a simple program that read data from STDIN but redirected in a
pipe from a file parse it and wrote output to STDOUT,here is the code:
#!/usr/bin/perl -w
# read the of ethernet vendor codes on STDIN
# and convert it in a 2 columns file
# with Ethernet code and constructor
# separator of output file is tab
use strict; # it's always better to be strict when coding ;-)
# split1line (read split first line)
# read this: 00-00-00 (hex) CORPORATION NAME
# return that: 000000 CORPORATION NAME
sub split1line() {
my @betwtab = split /\t/ , <STDIN>;
my @betwspc = split / / , $betwtab[0];
my @hexaddr = split /-/ , $betwspc[0];
my $vendor = $betwtab[2];
my $ethercode = $hexaddr[0] . $hexaddr[1] . $hexaddr[2] ;
return $ethercode . "\t" . $vendor ;
}
# skip the 6 first lines that do not contain any ethernet code
<STDIN>;
<STDIN>;
<STDIN>;
<STDIN>;
<STDIN>;
<STDIN>;
while (1) {
my $pair = split1line();
print $pair;
my $oneline;
# stop after the next empty line
while (($oneline = <STDIN>) && ($oneline !~ /^\n$/)) {
}
exit(0) if not defined $oneline;
}
it works like that:
cat ../var/oui.txt | ./preformat.pl
but when i try this:
cat ../var/oui.txt | ./preformat.pl > ../var/ieee_ethercodes.dat
Value of <HANDLE> construct can be "0"; test with defined() at
./preformat.pl line 56.
line 56 is this one : while (($oneline = <STDIN>) && ($oneline !~
/^\n$/)) {
the file oui.txt could be downloaded here:
http://standards.ieee.org/regauth/oui/oui.txt
i can, of course, modify the program to write directly the result in a
given file but i want to learn more about this strange error.
any help would be appreciated.
------------------------------
Date: 02 Sep 2003 13:55:08 -0400
From: Ryan Shondell <shondell@cis.ohio-state.edu>
Subject: Re: can't redirect output to a file?!
Message-Id: <xcwy8x79itf.fsf@psi.cis.ohio-state.edu>
Damien Mattei <mattei@unice.fr> writes:
> i wrote a simple program that read data from STDIN but redirected in
> a pipe from a file parse it and wrote output to STDOUT,here is the
> code:
(snip)
> cat ../var/oui.txt | ./preformat.pl > ../var/ieee_ethercodes.dat
> Value of <HANDLE> construct can be "0"; test with defined() at
> ./preformat.pl line 56.
This warning means that <STDIN> could return the value "0". This would
make your conditional statement for your while loop false, and
terminate the loop unintentionally. The warning is suggesting you use
'defined', so that if the handle returns 0, the loop won't exit.
while( defined($oneline = <STDIN>) && ($oneline !~/^\n$/)) {
^^^^^^^
Also, you may find it helpful to check 'perldoc perldiag' if you see
an error or warning you don't recognize. Or, put 'use diagnostics' in
your program, and it will display the information from perldiag about
any warnings or errors it finds.
--
Ryan Shondell <shondell@cis.ohio-state.edu>
------------------------------
Date: Tue, 02 Sep 2003 12:57:17 +0200
From: "Janek Schleicher" <bigj@kamelfreund.de>
Subject: Re: Effeciency question - perl scripts v's perl exe's
Message-Id: <pan.2003.09.02.10.57.16.577170@kamelfreund.de>
Steffen Müller wrote at Thu, 28 Aug 2003 19:16:53 +0200:
>> In our organisation we have a number of perl scripts which do various
>> things to word documents, (count lines, format w/ headers footers)
>> and other routine operations. My question is, is there any
>> performance gain to be had by compiling our scripts to executables
>> using perl2exe, and generally are there any performance benfits of
>> using perl executables instead of perl scripts?
>>
>> Also if anyone can point me to some statistics this would help a
>> great deal.
>
> The only existing Perl compiler is part of perl. Anything else is just a
> packager that puts a perl, required modules, and the application in an
> executable package. That won't give you any extra performance.
And in addition, even if there would be a reliable, good Perl compiler, it
would not result in a great speed advantage. perl compiles a script into
an internal opcode tree. Unlike the equavilent idea of the Java Virtual
Machines, the opcodes are quite complex (e.g. a grep is an own opcode
IIRC), thus some large code blocks (at least in other languages the code
blocks would be also large in the script) are usually already compiled to
opcodes. One affect is that it is much more complex to write a perl (one
of the reason why there is only one), but another is that the gain penalty
of interpreted code is only some percent, not some times.
The only way to increase speed with compilation is to avoid it. That's the
way mod_perl works (quick).
Greetings,
Janek
------------------------------
Date: Tue, 02 Sep 2003 23:34:44 +1000
From: Simon Taylor <simon@unisolve.com.au>
Subject: Re: Help -- how to execute "computed Perl code"
Message-Id: <bj25kj$1jps$1@otis.netspace.net.au>
Steve D wrote:
> My code dynamically creates a scalar with a text string that is a
> valid Perl code line.
>
> How can I get Perl to execute the line contained in that scalar?
>
> I could write it to a temp file and then "do <file>", but I want to
> avoid that overhead. It does not seem eval and "do BLOCK" are the
> answer.
>
> Any good solutions?
I won't elaborate on the answers in this thread to date, but simply
mention the 'Safe' module. I've found it to be very handy in some
circumstances when you want to execute arbitrary (dynamically created) code.
It allows you to run code in a reasonably safe 'compartment'.
Regards,
Simon Taylor
------------------------------
Date: Tue, 02 Sep 2003 15:34:59 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Help -- how to execute "computed Perl code"
Message-Id: <x7fzjfurtp.fsf@mail.sysarch.com>
>>>>> "BM" == Brian McCauley <nobull@mail.com> writes:
BM> Unlike some of the regulars here I'm not gripped by a rabid fear of
BM> this technique, indeed I embrace it. But before you use it you must
BM> understand how parsing data as code can easily produce obscure hidden
BM> bugs or security holes.
then you misunderstand why we always say it is evil. i have used string
eval but only as a last resort or where is provides a distinct
advantage. most newbies hear about it and start to use it for every nail
they see when they can usually use perl structures or other concepts
instead of eval. so the mantra here is don't use it (unless you know why
you are using it).
in this thread i still have no clear understanding of the problem
itself, let alone why eval is needed or even better, why it didn't work
at all.
BM> Dunno. The solution you have rejected (for no apparent reason) is the
BM> solution to your "Y" but there's a fairly good chance it's not a good
BM> solution to your "X"[1].
i smell XY problem as well. poorly articulated problems with eval as a
potential (and rejected!) solution reeks of XY.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
------------------------------
Date: Tue, 02 Sep 2003 15:37:05 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Help -- how to execute "computed Perl code"
Message-Id: <x7d6ejurq6.fsf@mail.sysarch.com>
>>>>> "SD" == Steve D <google.deller@smsail.com> writes:
SD> my $a = 1 ;
SD> my @code_text = ( '$a += 3 ;', '$a /= 2 ;', 'print $a ; ' ) ;
XY all the way.
use a dispatch table. search google for examples with that very
phrase. it crops up every month here. eval is the wrong way to solve it
(and eval would work if you did it correctly. i won't help there since
it is the wrong path for you).
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
------------------------------
Date: Tue, 02 Sep 2003 15:39:54 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Help -- how to execute "computed Perl code"
Message-Id: <x7ad9nurlh.fsf@mail.sysarch.com>
>>>>> "SD" == Steve D <google.deller@smsail.com> writes:
SD> my $a = 1 ;
SD> my @code_text = ( '$a += 3 ;', '$a /= 2 ;', 'print "$a\n" ; ' ) ;
SD> for my $code_text ( @code_text ) {
SD> my $code = sub { eval "$code_text" } ; # Just need eval to make it
SD> work
GACK! that is fugly. use a dispatch table already. you are jumping
through 3 hoops for no reason.
SD> print "Executing $code_text\n" ;
SD> $code->() ;
SD> } ;
you are making closures (and don't even realize it), calling eval in the
wrong place (even as eval is not needed)
SD> The eval in the sub def is the key. I had tried a do of an eval, an
SD> eval of an eval, but never a sub of an eval. That's the key.
no it is not the key.
use a dispatch table.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
------------------------------
Date: 2 Sep 2003 06:30:55 -0700
From: dv@proasyl.de (Lothar Fuhlrott)
Subject: Installing LWP fails
Message-Id: <b7cf856b.0309020530.3496f87@posting.google.com>
OS: Red Hat 6.2, Kernel 2.4.7
Having compiled Perl 5.8.1 and removed an older version I successfully
installed all additional perl modules we need using cpan. Save one:
Trying to implement libwww-perl-5.69, the following error messages
show up while running make test:
#################################################
<SNIP>
base/ua...............ok
html/form.............Can't call method "value" on an undefined value
at html/form.t line 26.
html/form.............dubious
Test returned status 2 (wstat 512, 0x200)
DIED. FAILED tests 2-14
Failed 13/14 tests, 7.14% okay
robot/rules-dbm.......ok
<SNAP>
<SNIP>
Failed 1/26 test scripts, 96.15% okay. 13/343 subtests failed, 96.21%
okay.
make: *** [test] Fehler 29
/usr/bin/make test -- NOT OK
Running make install
make test had returned bad status, won't install without force
<SNAP>
#################################################
I read some old messages in regard to a similar case suggesting the
"host system" might be "misconfigured". Actually the machine can be
pinged as localhost as well as by its network name and has been an
integrated part of our internal network for a long time anyway. So
IMHO this cannot be the reason.
I'd appreciate any help...
Thanks
Lothar Fuhlrott
------------------------------
Date: 02 Sep 2003 17:17:40 GMT
From: Joerg Fischer <jf505@yahoo.de>
Subject: Re: looking for a nedit Syntax Pattern file for perl
Message-Id: <3f54d0b4$0$25257$9b622d9e@news.freenet.de>
Andrew Crook wrote:
> looking for a nedit Syntax Pattern file for perl
> can anyone help?
Isn't it already built in? What NEdit version do you have?
Anyway here is a pattern set (seems to be newer than the default
pattern set built into NEdit 5.4):
http://npr.opendocuments.com/
Cheers,
Jörg
--
Niki -- The NEdit WiKi:
http://www.nr.no/~joachim/Niki/
------------------------------
Date: Wed, 03 Sep 2003 00:39:50 +0800
From: AcCeSsDeNiEd <dillon@SpamMinuSaccessdenied.darktech.org>
Subject: Reading a scalar line by line
Message-Id: <vph9lvkv8hi3dsrkpsknphfhtpno3ppn18@4ax.com>
Hi, does anyone know how I would do this?
something like:
while ($string)
print _$
}
Google searches did not turn up anything.
Thanks
To e-mail, remove the obvious
------------------------------
Date: Tue, 02 Sep 2003 18:44:47 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Reading a scalar line by line
Message-Id: <8O35b.19363$os2.267274@news2.e.nsc.no>
AcCeSsDeNiEd wrote:
> Hi, does anyone know how I would do this?
Do what?
> something like:
That's not good enough. Tell us excactly _what_ you want to do, and
we'll help you. Lucky you, your subject told us a little.
> while ($string)
>
> print _$
> }
No wonder. Try splitting your $string on line breaks (if that _is_ what
you want to do);
while ( split(/\n/, $string) ) {
print $_;
}
Why do you keep separate lines in a scalar anyway? I find it handier to
keep them in a list.
--
Tore Aursand <tore@extend.no>
"Whenever I see an old lady slip and fall on a wet sidewalk, my first
instinct is to laugh. But then I think, what if I was an ant, and she
fell on me. Then it wouldn't seem quite so funny." -- Jack Handey
------------------------------
Date: Tue, 02 Sep 2003 17:43:06 GMT
From: Brian Harnish <bharnish@technologist.com>
Subject: Re: Reading a scalar line by line
Message-Id: <pan.2003.09.02.17.43.24.565647@technologist.com>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
On Wed, 03 Sep 2003 00:39:50 +0800, AcCeSsDeNiEd wrote:
> Hi, does anyone know how I would do this?
>
> something like:
>
> while ($string)
>
> print _$
> }
I believe you are looking for something like this "print $_ for(<>)" for
strings instead of files.
print $_ for(split(/\n/, $string));
or
for(split(/\n/, $string)){
print $_;
}
Or, by "something like" do you mean non-working code? Because _$ doesn't
exist.
> To e-mail, remove the obvious
Obvious to who? Is removing just "SpamMinus" supposed to be obvious? or
just "Spam"?
- Brian
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE/VNasiK/rA3tCpFYRAlgzAKDoOVCLkRQO2e779WtDZACYUPUMjwCg1FqN
VN+OSr1EsddJ0MeZjtzQX7k=
=Jinw
-----END PGP SIGNATURE-----
------------------------------
Date: Tue, 2 Sep 2003 10:44:45 -0700
From: "David Oswald" <print split /!/"d!a!v!i!d!o!@!p!a!c!i!f!i!e!r!.!c!o!m!\n">
Subject: Re: Reading a scalar line by line
Message-Id: <vl9ln7e03bom41@corp.supernews.com>
"AcCeSsDeNiEd" <dillon@SpamMinuSaccessdenied.darktech.org> wrote:
> Hi, does anyone know how I would do this?
>
> something like:
>
> while ($string)
>
> print _$
> }
[ Subject line implies that the real question is how do I split a scalar on
"\n" and iterate over the list created by split?]
First, there is no such thing as _$. You must mean $_. Now to answer your
question:
Do this:
foreach ( split /\n/, $string ) {
print;
}
I might also suggest getting the Llama book, Learning Perl. It will go a
long way toward getting you started in the right direction. Of course Perl
also has built-in documentation. But just to get started, why not shell out
$20 on Learning Perl, so that you can really get a flavor of how Perl works?
------------------------------
Date: Tue, 2 Sep 2003 16:14:29 +0100
From: "Bigus" <someone@somewhere.com>
Subject: Re: Redirecting via LWP
Message-Id: <bj2c4n$h54@newton.cc.rl.ac.uk>
"Bigus" <someone@somewhere.com> wrote in message
news:bj1pq3$iu4@newton.cc.rl.ac.uk...
> "James Willmore" <jwillmore@cyberia.com> wrote in message
> news:20030901151911.3612309c.jwillmore@cyberia.com...
> > On Mon, 1 Sep 2003 18:41:57 +0100
> > "Bigus" <bigus_34@yahoo.co.uk> wrote:
> > > I'm not storing a file, no.. the directory I want the user to end up
> > > in is displayed via Apache's directory listing feature.. ie: there
> > > is no html page in that directory. If I have to start creating &
> > > storing temp files then it could be a bit of a pain. There are about
> > > a thousand directories that will be handled by this script.
> > >
> > > Can anyone give me any further pointers on what to search for?
> >
> > Why are you using LWP in a CGI script? If you are using Apache's
> > directory listing, what are you listing, if not files? What exactly
> > are you tring to do?
>
> Well, you've solved it for me (see below), but just for the sake of
> completeness, as I evidently didn't explain what I was doing very well -
> basically, each directory is a file area associated with a Listserv
mailing
> list. The directory is password protected by an Apache htaccess file to
stop
> users typing a URL and getting there directly. My CGI script makes them
log
> into Listserv and checks to make sure they are a subscriber (or a site
> administrator) to the mailing list that they are trying to access the file
> area of. If they pass those checks the script takes them to the file area,
> automatically dealing with the Apache authentication.
>
> > In your OP, you wanted to do redirection. Then why not
> > 1) use the CGI module's redirect method
> > 2) simply print the proper header (print "Location: <new URL>\n\n";)
>
> That worked!
Actually, it didn't work.. doh! The line:
print "Location: http://$listname:$password\@$host/files/$listname/\n\n";
Causes the Apache password dialog box to come up, even though $listname and
$password are absolutely definitely correct.
Any ideas?
Thanks
Bigus
------------------------------
Date: Tue, 2 Sep 2003 17:23:30 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Redirecting via LWP
Message-Id: <Pine.LNX.4.53.0309021721030.17161@lxplus078.cern.ch>
On Tue, Sep 2, Bigus inscribed on the eternal scroll:
> Actually, it didn't work.. doh! The line:
>
> print "Location: http://$listname:$password\@$host/files/$listname/\n\n";
Although the general URL format is defined to include an optional
name:password@domain part, this part is prohibited in HTTP URLs.
Some software is more scrupulous about applying the rules than others.
> Any ideas?
Do you have a question about Perl at all?
f'ups prophylactically redirected.
------------------------------
Date: 2 Sep 2003 08:42:54 -0700
From: alexmarinkovic@yahoo.com (Alex)
Subject: Re: Replacing IP's in ./etc/hosts
Message-Id: <cb7f097a.0309020742.509169b4@posting.google.com>
"John W. Krahn" <krahnj@acm.org> wrote in message news:<3F54228A.AA4B7AF0@acm.org>...
> Alex wrote:
> >
> > I have several Sun boxes that get their ip via DHCP. Once in a while,
> > when the ip's change, then I have to manually edit the /etc/hosts
> > files on all the boxes to correlate with the new IP. This is kind of
> > a hassle, so I wanted to use Perl to try and create a script that can
> > do this automatically.
>
> Have you thought about running DNS?
>
> > I am new to Perl, and scripting for that
> > matter, so I was hoping for some assistance. Here is what I have so
> > far for a machine named Morphine:
> >
> > #!/usr/bin/perl -w
> >
> > $ip = `ifconfig hme0 | grep inet | cut -f 2 -d " "`;
> >
> > $ip_hostfile = `less /etc/hosts | grep Morphine | cut -f 1`;
>
> Ick.
>
> #!/usr/bin/perl -w
> use strict;
> use Socket;
>
> my ( $ip ) = `ifconfig hme0` =~ /inet addr:\s*(\S+)/;
>
> my $ip_hostfile = do {
> my @temp;
> while ( @temp = gethostent ) { $temp[0] =~ /Morphine/ and last }
> inet_ntoa $temp[-1];
> };
>
>
> > print "The current ip is: $ip\n";
> > print "The ip in /etc/hosts is: $ip_hostfile\n";
> >
> > if ($ip eq $ip_hostfile)
> > {
> > print "The ip's are the same. No changes made.\n";
> > }
> > else
> > {
> > print "The ip's are different, writing new ip to
> > /etc/hosts\n";
> > open(INPUT, "/etc/hosts");
> > open(OUTPUT, ">/etc/newhosts");
> >
> > while (<INPUT>)
> > {
> > s/$ip_hostfile/$ip/g;
> > print OUTPUT;
> > }
> >
> > close(INPUT);
> > close(OUTPUT);
> > }
>
> if ( $ip eq $ip_hostfile ) {
> print "The ip's are the same. No changes made.\n";
> exit 0;
> }
>
> print "The ip's are different, writing new ip to /etc/hosts\n";
>
> ( $^I, @ARGV ) = ( '.back', '/etc/hosts' );
>
> while ( <> ) {
> s/^\Q$ip_hostfile\E\b/$ip/;
> print;
> }
>
> __END__
>
>
> > Obviously, I haven't gotten to the part where I actually replace the
> > newhosts file with the /etc/hosts file, because the replacement of the
> > $ip_hostfile with the $ip is not working. It creates the new file,
> > and populates it with the contents of the INPUT file, but the new ip
> > doesn't get placed in the newhosts file. The ip that needs to be
> > changed doesn't get changed, it stays the same value as in the INPUT
> > file. However, what I have found is that in this line:
> > s/$ip_hostfile/$ip/g, if I replace the variables with actual ip's,
> > then the script works fine...the values are switched like they are
> > supposed to. So the problem seems to be that the variables are not
> > working in that line for some reason. The variables are initializing
> > correctly, bacause the print statements correctly show the ip's. So,
> > any help would be much appreciated.
>
> First off, you don't need the /g modifier because you are only replacing
> one thing and it has to be at the beginning of the line so you should
> use ^ and contents of $ip_hostfile has dots which are special in regular
> expressions so you have to quotemeta the variable and you should mark
> the end of $ip_hostfile with \b so you get an exact match.
>
>
> John
Thanks so much to everyone who so quickly came up with great
suggestions. I especially like the "Ick" comment. :-) I can see that
I have a lot to learn about Perl.
Thanks again,
Alex
------------------------------
Date: 2 Sep 2003 09:28:58 -0700
From: alexmarinkovic@yahoo.com (Alex)
Subject: Re: Replacing IP's in ./etc/hosts
Message-Id: <cb7f097a.0309020828.40805d50@posting.google.com>
"John W. Krahn" <krahnj@acm.org> wrote in message news:<3F54228A.AA4B7AF0@acm.org>...
> Alex wrote:
> >
> > I have several Sun boxes that get their ip via DHCP. Once in a while,
> > when the ip's change, then I have to manually edit the /etc/hosts
> > files on all the boxes to correlate with the new IP. This is kind of
> > a hassle, so I wanted to use Perl to try and create a script that can
> > do this automatically.
>
> Have you thought about running DNS?
>
> > I am new to Perl, and scripting for that
> > matter, so I was hoping for some assistance. Here is what I have so
> > far for a machine named Morphine:
> >
> > #!/usr/bin/perl -w
> >
> > $ip = `ifconfig hme0 | grep inet | cut -f 2 -d " "`;
> >
> > $ip_hostfile = `less /etc/hosts | grep Morphine | cut -f 1`;
>
> Ick.
>
> #!/usr/bin/perl -w
> use strict;
> use Socket;
>
> my ( $ip ) = `ifconfig hme0` =~ /inet addr:\s*(\S+)/;
>
> my $ip_hostfile = do {
> my @temp;
> while ( @temp = gethostent ) { $temp[0] =~ /Morphine/ and last }
> inet_ntoa $temp[-1];
> };
>
>
> > print "The current ip is: $ip\n";
> > print "The ip in /etc/hosts is: $ip_hostfile\n";
> >
> > if ($ip eq $ip_hostfile)
> > {
> > print "The ip's are the same. No changes made.\n";
> > }
> > else
> > {
> > print "The ip's are different, writing new ip to
> > /etc/hosts\n";
> > open(INPUT, "/etc/hosts");
> > open(OUTPUT, ">/etc/newhosts");
> >
> > while (<INPUT>)
> > {
> > s/$ip_hostfile/$ip/g;
> > print OUTPUT;
> > }
> >
> > close(INPUT);
> > close(OUTPUT);
> > }
>
> if ( $ip eq $ip_hostfile ) {
> print "The ip's are the same. No changes made.\n";
> exit 0;
> }
>
> print "The ip's are different, writing new ip to /etc/hosts\n";
>
> ( $^I, @ARGV ) = ( '.back', '/etc/hosts' );
>
> while ( <> ) {
> s/^\Q$ip_hostfile\E\b/$ip/;
> print;
> }
>
> __END__
>
>
> > Obviously, I haven't gotten to the part where I actually replace the
> > newhosts file with the /etc/hosts file, because the replacement of the
> > $ip_hostfile with the $ip is not working. It creates the new file,
> > and populates it with the contents of the INPUT file, but the new ip
> > doesn't get placed in the newhosts file. The ip that needs to be
> > changed doesn't get changed, it stays the same value as in the INPUT
> > file. However, what I have found is that in this line:
> > s/$ip_hostfile/$ip/g, if I replace the variables with actual ip's,
> > then the script works fine...the values are switched like they are
> > supposed to. So the problem seems to be that the variables are not
> > working in that line for some reason. The variables are initializing
> > correctly, bacause the print statements correctly show the ip's. So,
> > any help would be much appreciated.
>
> First off, you don't need the /g modifier because you are only replacing
> one thing and it has to be at the beginning of the line so you should
> use ^ and contents of $ip_hostfile has dots which are special in regular
> expressions so you have to quotemeta the variable and you should mark
> the end of $ip_hostfile with \b so you get an exact match.
>
>
> John
John,
I tried running your version of the script as:
#!/usr/bin/perl -w
use strict;
use Socket;
my ( $ip ) = `ifconfig hme0` =~ /inet addr:\s*(\S+)/;
my $ip_hostfile = do {
my @temp;
while ( @temp = gethostent ) { $temp[0] =~ /Morphine/ and last }
inet_ntoa $temp[-1];
};
if ( $ip eq $ip_hostfile ) {
print "The ip's are the same. No changes made.\n";
exit 0;
}
print "The ip's are different, writing new ip to /etc/hosts\n";
( $^I, @ARGV ) = ( '.back', '/etc/hosts' );
while ( <> ) {
s/^\Q$ip_hostfile\E\b/$ip/;
print;
}
And got the following errors:
Use of uninitialized value at ./ngscript line 13.
The ip's are different, writing new ip to /etc/hosts
Use of uninitialized value at ./ngscript line 23, <> chunk 5.
Any thoughts?
Thanks,
Alex
------------------------------
Date: Tue, 02 Sep 2003 16:34:44 GMT
From: Brian Harnish <bharnish@technologist.com>
Subject: Re: Replacing IP's in ./etc/hosts
Message-Id: <pan.2003.09.02.16.35.04.878610@technologist.com>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
On Mon, 01 Sep 2003 14:38:02 -0700, Alex wrote:
> Hello,
>
> I have several Sun boxes that get their ip via DHCP. Once in a while,
> when the ip's change, then I have to manually edit the /etc/hosts
> files on all the boxes to correlate with the new IP. This is kind of
> a hassle, so I wanted to use Perl to try and create a script that can
> do this automatically. I am new to Perl, and scripting for that
> matter, so I was hoping for some assistance. Here is what I have so
> far for a machine named Morphine:
>
> #!/usr/bin/perl -w
>
> $ip = `ifconfig hme0 | grep inet | cut -f 2 -d " "`;
>
> $ip_hostfile = `less /etc/hosts | grep Morphine | cut -f 1`;
>
#!/bin/sh
$ip = `whatever`
perl -wlpi -e "s/^\S+(.*\bMorphine\b)/$ip\$1/" /etc/hosts
# __END__
- Brian
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE/VMariK/rA3tCpFYRAnGDAJ9uFKylJjJnHD8NUzW+1u5UdZkgZQCdH5Rr
ZsbIrSBwpqFSU359wyzvbHQ=
=qDuf
-----END PGP SIGNATURE-----
------------------------------
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 5428
***************************************