[23204] in Perl-Users-Digest
Perl-Users Digest, Issue: 5425 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Sep 1 21:05:45 2003
Date: Mon, 1 Sep 2003 18:05:07 -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 Mon, 1 Sep 2003 Volume: 10 Number: 5425
Today's topics:
Re: A few questions about Perl and it's capabilities <ben.goldberg@hotpop.com>
Re: A few questions about Perl and it's capabilities <minceme@start.no>
Re: iterating through hash of hashes <ben.goldberg@hotpop.com>
Re: learning examples <matthew.garrish@sympatico.ca>
Re: regex query <ben.goldberg@hotpop.com>
Re: Replacing IP's in ./etc/hosts <bwalton@rochester.rr.com>
Re: Replacing IP's in ./etc/hosts (Charles DeRykus)
Re: Safely eval code from text file--suggestions? (Charles DeRykus)
Re: <bwalton@rochester.rr.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 01 Sep 2003 18:53:05 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: A few questions about Perl and it's capabilities
Message-Id: <3F53CDD1.C896A1EC@hotpop.com>
Vlad Tepes wrote:
[snip]
> Let's speed it up a bit:
>
> use Time::HiRes qw( usleep );
>
> $|++;
> my @waitchar = qw( \ | / - );
> for (1..100) {
> print @waitchar[$_ % @waitchar ];
> usleep 1e4;
> print "\b";
> }
>
> Wheeeee!
>
> ( But how to get rid of the cursor? )
use Term::Cap;
use constant TERMCAP => Term::Cap->Tgetent( {
TERM => undef, OSPEED => 9600
};
use constant TERM_CURSOR_INVIS => TERMCAP->Tputs( "civis", 0 );
print STDOUT TERM_CURSOR_INVIS;
[untested]
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Tue, 2 Sep 2003 00:29:06 +0000 (UTC)
From: Vlad Tepes <minceme@start.no>
Subject: Re: A few questions about Perl and it's capabilities
Message-Id: <bj0o8h$fg8$1@troll.powertech.no>
Benjamin Goldberg <ben.goldberg@hotpop.com> wrote:
> Vlad Tepes wrote:
>>
>> ( But how to get rid of the cursor? )
>
> use Term::Cap;
> use constant TERMCAP => Term::Cap->Tgetent( {
> TERM => undef, OSPEED => 9600
> };
> use constant TERM_CURSOR_INVIS => TERMCAP->Tputs( "civis", 0 );
> print STDOUT TERM_CURSOR_INVIS;
>
> [untested]
I couldn't get that to work, but the module was right.
Here's a fast spinner (or whatsitsname) that works on my
system (Linux). ( I've just thrown this together, so
it's probably not very portable. )
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes qw( usleep );
sub cursor($) {
require Term::Cap;
Term::Cap->Tgetent->Tputs( $_[0] ? 've' : 'vi', 0, *STDOUT );
}
cursor 0;
$|++;
my @waitchar = qw( \ | / - );
for (1..100) {
print @waitchar[$_ % @waitchar ];
usleep .5e5;
print "\b";
}
cursor 1;
__END__
--
Vlad
------------------------------
Date: Mon, 01 Sep 2003 19:08:15 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: iterating through hash of hashes
Message-Id: <3F53D15F.88FE8DAC@hotpop.com>
Jeff Thies wrote:
>
> I'd like to iterate through a hash of hashes and get all the values.
> How do I do that?
>
> The following works for me. But it seems a bit convoluted and I'm
> unable to retrieve where the value came from, only the last key:
>
> my %h;
> $h{a}='A';
> $h{b}='B';
Ok so far.
> $h{a}{b}='AB';
But this is eqivilant to:
${ "A" }{b} = 'AB';
That is, it uses a symbolic reference. That's bad. Don't do it.
Read the following:
http://perl.plover.com/varvarname.html
After you've fixed your data structure to not muck with perl's symbol
table, you can easily print it out using Data::Dumper.
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Mon, 1 Sep 2003 18:17:38 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: learning examples
Message-Id: <iBP4b.1804$su.152557@news20.bellglobal.com>
"Sam" <samj@austarmetro.com.au> wrote in message
news:3f53c04d@news.comindico.com.au...
> the first is as bellow it prints a "\n" in the output right after the
$nmber
> so that converts to ... is on a new line. why?
>
> my $nmber= <STDIN>;
>
> print "\$$nmber converts to decimal number: ", hex("$nmber"), "\n";
>
When you read from stdin, you are capturing the "\n" the user enters to
indicate that input is finished (i.e., when you press the enter key). If you
don't want it in your string, chomp it:
my $nmber=<STDIN>;
chomp $nmber
Matt
------------------------------
Date: Mon, 01 Sep 2003 18:41:49 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: regex query
Message-Id: <3F53CB2D.F41AF8BD@hotpop.com>
Steve Dunn wrote:
>
> Hi all,
> Just a quick question for someone that might have the answer.
> I need a regex that'll turn ? and * that are not in quotes into the
> regex equivalant (? to .{1} and * to .*?)
> For instance,
>
> "ig*ore st?ff in qu*tes" t?p high*
>
> would be:
>
> "ig*ore st?ff in qu*tes" t.{1}p high.*?
>
> So, to recap, a regex that'll replace * and ? into the regex equivalant.
>
> Cheers!
my %trans = (qw/ * .*? ? .?? { ( , | } ) /, "", "");
(my $regex = $glob_pattern) =~ s<
( [^*?{,}]* )( [*?{,}]? )
>{
quotemeta($1) . $trans{$2};
}xge;
$regex = '^' . $regex . '$';
[untested]
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Mon, 01 Sep 2003 23:38:08 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: Replacing IP's in ./etc/hosts
Message-Id: <3F53D842.2080700@rochester.rr.com>
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. 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`;
>
> 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);
> }
>
> 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.
...
> Alex
>
My recommendation: Check and see if $ip and/or $ip_hostfile have line
terminators included in their values. If they do, chomp them and try
that. You might also quote the metacharacters (.'s) which occur in your
IP addresses when using them as a regexp. The metacharacters won't stop
them from matching in this case, but it is general good practice.
Something like:
s/\Q$ip_hostfile/$ip/g;
Also, you might want to check around in some Unix newsgroups -- seems
like the /etc/hosts mechanism is pretty well dated, replaced by BIND or
something. Maybe you don't need to do what you are doing.
--
Bob Walton
------------------------------
Date: Mon, 1 Sep 2003 23:53:36 GMT
From: ced@bcstec.ca.boeing.com (Charles DeRykus)
Subject: Re: Replacing IP's in ./etc/hosts
Message-Id: <HKK7pC.DsK@news.boeing.com>
In article <cb7f097a.0309011338.1b4a9a8b@posting.google.com>,
Alex <alexmarinkovic@yahoo.com> wrote:
>$ip = `ifconfig hme0 | grep inet | cut -f 2 -d " "`;
>
>$ip_hostfile = `less /etc/hosts | grep Morphine | cut -f 1`;
>
chomp to remove line endings (perldoc -f chomp):
chomp( $var = `...` );
die "... failed: $?" unless $? == 0;
...
>open(INPUT, "/etc/hosts");
>open(OUTPUT, ">/etc/newhosts");
also recommend defensive checks for potential open errors:
open(... ) or die "can't open: $!";
HTH,
--
Charles DeRykus
------------------------------
Date: Tue, 2 Sep 2003 00:19:24 GMT
From: ced@bcstec.ca.boeing.com (Charles DeRykus)
Subject: Re: Safely eval code from text file--suggestions?
Message-Id: <HKK8wC.E7B@news.boeing.com>
In article <Pine.A41.4.56.0308312148390.99070@dante18.u.washington.edu>,
JS Bangs <jaspax@u.washington.edu> wrote:
>All,
>
>I've got a module that will read an XML file that has code as the
>contents of some elements. I'd like to be able to capture this code as a
>code reference and pass that code reference to a function, without risking
>any internals. The following code works, but doesn't seem foolproof:
>
># %code, $self, and $parse defined elsewhere
>if (exists $code{$_}) {
> my $c;
> my $code = '$c = sub {' . $parse->{$_} . '}';
>
> # Prevent $code from modifying in-scope variables we need to keep
> {
> local($self, $parse);
> eval $code;
> }
>
> if ($@) {
> err("Errors processing $_ : $@");
> }
> else {
> $self->$_($c);
> }
>}
>
Wouldn't you almost certainly want to use the core Safe module
in case other wickedness creeps into the code...
HTH,
--
Charles DeRykus
------------------------------
Date: Sat, 19 Jul 2003 01:59:56 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re:
Message-Id: <3F18A600.3040306@rochester.rr.com>
Ron wrote:
> Tried this code get a server 500 error.
>
> Anyone know what's wrong with it?
>
> if $DayName eq "Select a Day" or $RouteName eq "Select A Route") {
(---^
> dienice("Please use the back button on your browser to fill out the Day
> & Route fields.");
> }
...
> Ron
...
--
Bob Walton
------------------------------
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 5425
***************************************