[18068] in Perl-Users-Digest
Perl-Users Digest, Issue: 228 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 7 00:05:34 2001
Date: Tue, 6 Feb 2001 21:05:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <981522308-v10-i228@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 6 Feb 2001 Volume: 10 Number: 228
Today's topics:
Re: "Average of List of Numbers" by Abigail <gregbast@tartarus.uwa.edu.au>
Anyone know how to get an online perl cgi program to pr <Jay@uknighttimeonline.com>
Re: Anyone know how to get an online perl cgi program t <tony_curtis32@yahoo.com>
Re: Anyone know how to get an online perl cgi program t <Jay@uknighttimeonline.com>
Re: Anyone know how to get an online perl cgi program t <tony_curtis32@yahoo.com>
Re: Apologies to Joyce Kilmer, a hack for your enjoymen (Chris)
Re: Convert postscript to jpeg? <juex@deja.com>
Hashes bakor@my-deja.com
Re: Hashes aramis1631@my-deja.com
Re: Hashes (David K. Wall)
Re: Hashes <hafateltec@hotmail.com>
Re: Hashes <uri@sysarch.com>
hex to binary conversion ? Please help tvn007@my-deja.com
Re: hex to binary conversion ? Please help (Steven Smolinski)
Re: Mail::Mailer <jkitchin@udel.edu>
Re: Modules/Constants. (Garry Williams)
Re: Need Help Checking IP Address Syntax w/ PERL? <mischief@velma.motion.net>
Re: Need Help Checking IP Address Syntax w/ PERL? (Garry Williams)
Pattern Matching <charleswarner@bigfoot.com>
Re: Perl for the Fortran programmer <hafateltec@hotmail.com>
PLEASE HELP A NEWBIE <shutupsteve@aNwOdSaPnAgM.com>
Re: PLEASE HELP A NEWBIE <tony_curtis32@yahoo.com>
Re: Text::Template Problem (Chris Fedde)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 7 Feb 2001 04:58:17 +0000 (UTC)
From: Gregory Neil Bastow <gregbast@tartarus.uwa.edu.au>
Subject: Re: "Average of List of Numbers" by Abigail
Message-Id: <95qkl9$sro$1@enyo.uwa.edu.au>
In comp.lang.misc Chris Stith <mischief@velma.motion.net> wrote:
: In the context of programming, I've always thought elegance had to do
: with doing something in a way that is either conscise and fast or simple
: but not necessarily obvious.
Not sure what you mean about "obviousness" with respect to elegance.
I think it should be fairly easy to see what it does and how it works, but
usually taking a non-obvious step to come up with it in the first place.
-Greg
------------------------------
Date: Wed, 7 Feb 2001 03:43:38 -0000
From: "Jay P" <Jay@uknighttimeonline.com>
Subject: Anyone know how to get an online perl cgi program to pretend to be a 'submitted form' and therefore POST to another cgi expecting a forms contents?
Message-Id: <3a80c332$1@shiva.ukisp.net>
I am trying to do this as an SMS provider requires that I use a form to post
the details for SMS outbound messaging.
This I am not going to do if I can have one of my cgi's elsewhere pretend to
have filled in the form by delivering properly
formatted contents to the form handler cgi.
a perl program example would be marvelous
Jay Pritchard
------------------------------
Date: 06 Feb 2001 21:42:08 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Anyone know how to get an online perl cgi program to pretend to be a 'submitted form' and therefore POST to another cgi expecting a forms contents?
Message-Id: <87r91bkvlb.fsf@limey.hpcc.uh.edu>
>> On Wed, 7 Feb 2001 03:43:38 -0000,
>> "Jay P" <Jay@uknighttimeonline.com> said:
> I am trying to do this as an SMS provider requires that
> I use a form to post the details for SMS outbound
> messaging. This I am not going to do if I can have one
> of my cgi's elsewhere pretend to have filled in the form
> by delivering properly formatted contents to the form
> handler cgi.
Well, for a start:
perldoc lwpcook
perldoc HTTP::Request
perldoc LWP::UserAgent
perldoc URI
hth
t
--
Eih bennek, eih blavek.
------------------------------
Date: Wed, 7 Feb 2001 04:15:58 -0000
From: "Jay P" <Jay@uknighttimeonline.com>
Subject: Re: Anyone know how to get an online perl cgi program to pretend to be a 'submitted form' and therefore POST to another cgi expecting a forms contents?
Message-Id: <3a80cac5$1@shiva.ukisp.net>
Dear Tony, thanks for your help
I have had a look at these docs but they all seem to talk about processing
data 'coming in' from forms and do not seem to explain
how i can emulate it, such as how do I set $ENV{'REQUEST_METHOD'} to POST or
even how I send the header?
Tony Curtis <tony_curtis32@yahoo.com> wrote in message
news:87r91bkvlb.fsf@limey.hpcc.uh.edu...
> >> On Wed, 7 Feb 2001 03:43:38 -0000,
> >> "Jay P" <Jay@uknighttimeonline.com> said:
>
> > I am trying to do this as an SMS provider requires that
> > I use a form to post the details for SMS outbound
> > messaging. This I am not going to do if I can have one
> > of my cgi's elsewhere pretend to have filled in the form
> > by delivering properly formatted contents to the form
> > handler cgi.
>
> Well, for a start:
>
> perldoc lwpcook
> perldoc HTTP::Request
> perldoc LWP::UserAgent
> perldoc URI
>
> hth
> t
> --
> Eih bennek, eih blavek.
------------------------------
Date: 06 Feb 2001 22:21:52 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Anyone know how to get an online perl cgi program to pretend to be a 'submitted form' and therefore POST to another cgi expecting a forms contents?
Message-Id: <87r91brulb.fsf@limey.hpcc.uh.edu>
[ de jeopardised ]
>> On Wed, 7 Feb 2001 04:15:58 -0000,
>> "Jay P" <Jay@uknighttimeonline.com> said:
> Dear Tony, thanks for your help I have had a look at
> these docs but they all seem to talk about processing
> data 'coming in' from forms and do not seem to explain
> how i can emulate it, such as how do I set
> $ENV{'REQUEST_METHOD'} to POST or even how I send the
> header?
Look at the query_form() method in "perldoc URI". Then
you can construct an HTTP::Request with POST through
LWP::UserAgent.
See HTTP::Message about how to set the content() of a
request (for POST) from the URI object. HTTP::Request
inherits from HTTP::Message.
If you're still stymied, we can switch to email (and you'd
then summarise the solution in a subsequent post, if
you're agreeable).
hth
t
--
Eih bennek, eih blavek.
------------------------------
Date: Wed, 07 Feb 2001 02:20:44 GMT
From: cprasley@home.com (Chris)
Subject: Re: Apologies to Joyce Kilmer, a hack for your enjoyment
Message-Id: <3a80b095.5459089@news.rchmd1.bc.wave.home.com>
On Sun, 4 Feb 2001 11:19:52 +0100, rpolzer@web.de (Rudolf Polzer) wrote in
comp.lang.perl.misc:
>> There's no relation between Linux and Perl. Linux is a kernel, written
>> in C. SuSE is a Linux _distribution_.
>Of course I meant, a corrent distribution should have a current Perl
>package in it.
A current Linux distribution should have the latest *working* version of
Perl. Many people don't think 5.6 is there yet, and some distros agree.
------------------------------
Date: Tue, 6 Feb 2001 18:45:56 -0800
From: "Jürgen Exner" <juex@deja.com>
Subject: Re: Convert postscript to jpeg?
Message-Id: <3a80b6e4@news.microsoft.com>
"Scott Bossi" <Scott.Bossi@fmr.com> wrote in message
news:3A805CA6.7797164A@fmr.com...
> I have an application that generates output in postscript format, which
> is no good, since it needs to be viewed from a web browser. Does anyone
> know of a way to convert postscript to another format, such as a jpeg?
> Or am I missing something obvious here...
Well, what you are asking for is similar to converting Perl into GIF or C++
into BMP.
Are you sure that is what you want? I don't think so.
jue
------------------------------
Date: Wed, 07 Feb 2001 02:28:17 GMT
From: bakor@my-deja.com
Subject: Hashes
Message-Id: <95qbrr$cku$1@nnrp1.deja.com>
I am just starting with Perl. Below I am trying
to extract the number of
days in the current month using a hash list. If I
reference the hash element
with the literal "Feb" (without quotes) it works
fine but not with $month in
place...?
%days_in_month=(Jan,31,Feb,28,Mar,31);
$month=`date +%b`;
print $month;
print $days_in_month($month);
print "\n";
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Wed, 07 Feb 2001 02:52:27 GMT
From: aramis1631@my-deja.com
Subject: Re: Hashes
Message-Id: <95qd98$dni$1@nnrp1.deja.com>
In article <95qbrr$cku$1@nnrp1.deja.com>,
bakor@my-deja.com wrote:
> I am just starting with Perl. Below I am trying
> to extract the number of
> days in the current month using a hash list. If I
> reference the hash element
> with the literal "Feb" (without quotes) it works
> fine but not with $month in
> place...?
>
> %days_in_month=(Jan,31,Feb,28,Mar,31);
>
> $month=`date +%b`;
> print $month;
> print $days_in_month($month);
It appears to me, that you have parens (), when you mean to use curly
braces {}.
also you forgot about the newline character in $month that was returned
by date. Try the following:
chomp $month;
print "$month\n"; # or instead of newline use a space
print $days_in_month{$month}; #curly braces tell me it's a hash key
Good Luck!
> print "\n";
>
> Sent via Deja.com
> http://www.deja.com/
>
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: 6 Feb 2001 22:08:13 -0500
From: darkon@one.net (David K. Wall)
Subject: Re: Hashes
Message-Id: <9040E875Adarkononenet@206.112.192.118>
bakor@my-deja.com wrote:
>I am just starting with Perl. Below I am trying
>to extract the number of
>days in the current month using a hash list. If I
>reference the hash element
>with the literal "Feb" (without quotes) it works
>fine but not with $month in
>place...?
>
>%days_in_month=(Jan,31,Feb,28,Mar,31);
>
>$month=`date +%b`;
>print $month;
>print $days_in_month($month);
>print "\n";
Since you used parentheses, perl thinks $days_in_month is a reference to a
function. Use curly brackets instead: $days_in_month{$month};
See perldata in the documentation that came with Perl for more information.
--
David Wall
darkon@one.net
------------------------------
Date: Wed, 7 Feb 2001 13:57:50 +1000
From: "Mike McPherson" <hafateltec@hotmail.com>
Subject: Re: Hashes
Message-Id: <95qh5k$r6a$1@brokaw.wa.com>
<bakor@my-deja.com> wrote in message news:95qbrr$cku$1@nnrp1.deja.com...
> I am just starting with Perl. Below I am trying
> to extract the number of
> days in the current month using a hash list. If I
> reference the hash element
> with the literal "Feb" (without quotes) it works
> fine but not with $month in
> place...?
>
> %days_in_month=(Jan,31,Feb,28,Mar,31);
>
> $month=`date +%b`;
> print $month;
> print $days_in_month($month);
> print "\n";
Take a look at this script I just wrote... I'm sure a few people will say
"Why didn't you just use the pelmod" for figuring out seconds since 1970 but
hey it was an assignment what can I say.
#!/usr/bin/perl -w
#flush buffer so that output appears immediately
$| = 1;
#-------------------------------------------------------------#
# Written by Michael R. McPherson
# an ongoing exercise in learning how
# to script perl or shoot off my foot
#-------------------------------------------------------------#
# Problem::: Figure out how many Days in a month of a given year
# and what day each falls on. Then figure out how many Mondays
# Tuesdays ect ...
#-------------------------------------------------------------#
#Command line input for determining the year and month to be processed
print "Please enter the the year in YYYYMM you would like to process:";
my $indate = <STDIN>;
chomp $indate;
#Correct the indate for calculating leap year
my $inyear = substr($indate,0,4);
my $leap = &leap($inyear);
my %DaysInMonth =
(0,31,1,$leap,2,31,3,30,4,31,5,30,6,31,7,31,8,30,9,31,10,30,11,31);
my $cordate = $indate."02000000";
my @array = localtime(&CurrentLineTime($cordate));
my $month = $array[4];
#Define Days in month and there names.
my %NamesOfMonth = (0 => January, 1 => Febuary,
2 => March, 3 => April,
4 => May, 5 => June,
6 => July, 7 => August,
8 => September, 9 => October,
10 => November, 11 => December);
#Return The name of the month and how many days it has.
my $Month = $NamesOfMonth{$month};
my $TotalDaysInMonth = $DaysInMonth{$month};
print "\nThe month of $Month";
print "\nIn The Year of our LORD $inyear";
print "\nThere are $TotalDaysInMonth days in this month";
if ($DaysInMonth{1} eq "28") {
print "\nThis is a not a leap year";
} else {
print "\nThis is a leap year";
}
print "\n";
#Create data structure for output.
my $table = {0,0,1,0,2,0,3,0,4,0,5,0,6,0};
my $today;
foreach $today(1..$TotalDaysInMonth) {
if ($today == 1 || $today == 2 || $today == 3 || $today == 4 || $today == 5
|| $today == 6 || $today == 7 || $today == 8 || $today == 9){
@day = localtime(&CurrentLineTime($indate."0".$today."000000"));
} else {
@day = localtime(&CurrentLineTime($indate.$today."000000"));
}
my $wday = $day[6];
if ($wday eq 0) {
$table{0}++;
}
if ($wday eq 1) {
$table{1}++;
}
if ($wday eq 2) {
$table{2}++;
}
if ($wday eq 3) {
$table{3}++;
}
if ($wday eq 4) {
$table{4}++;
}
if ($wday eq 5) {
$table{5}++;
}
if ($wday eq 6) {
$table{6}++;
}
}
print "\nThere are $table{0} Sundays";
print "\nThere are $table{1} Mondays";
print "\nThere are $table{2} Tuesdays";
print "\nThere are $table{3} Wednsdays";
print "\nThere are $table{4} Thursdays";
print "\nThere are $table{5} Fridays";
print "\nThere are $table{6} Saturdays\n";
exit;
#---------------------------------------------------------------------------
--#
# usage.pl function - CurrentLineTime
# Purpose : Converts the date & time from a cdr entry into number
# of seconds since Jan 1, 1970
# Format:YYYYMMDDHHMMSS
#---------------------------------------------------------------------------
--#
sub CurrentLineTime {
my $time = 0;
my $date = $_[0];
my $day = substr($date,6,2);
my $month = substr($date,4,2);
my $year = substr($date,0,4);
my %month = ("01", 0, "02", 31, "03", 59, "04", 90,
"05", 120, "06", 151, "07", 181, "08", 212,
"09", 243, "10", 273, "11", 304, "12", 334);
$time += (substr($date,0,4) - 1970) * 31536000;
$time += ($month{substr($date,4,2)} + substr($date,6,2) - 1) * 86400;
my ($hours, $minutes, $seconds);
if (substr($date,8,2) eq " ") {
$hours = 0;
} elsif (substr($date,8,1) eq " ") {
$hours = substr($date,9,1);
} else {
$hours = substr($date,8,2);
}
if (substr($date,10,2) eq "00" || substr($date,10,2) eq " "){
$minutes = 0;
}elsif (substr($date,10,1) eq "0" || substr($date,10,1) eq " "){
$minutes = substr($date,11,1);
} else {
$minutes = substr($date,10,2);
}
if (substr($date,12,2) eq "00" || substr($date,12,2) eq " "){
$seconds = 0;
}elsif (substr($date,12,1) eq "0" || substr($date,12,1) eq " "){
$seconds = substr($date,13,1);
} else {
$seconds = substr($date,12,2);
}
$time += ($hours * 3600);
$time += ($minutes * 60);
$time += $seconds;
# Total Correction = (# of leap years) + (System Clock Correction)
# There have been ? leap years since 1970.
# System clock correction is -10 hours for this specific system.
# Total Correction = ? - 10*3600 for this specific system.
#---------------------------------------------------------#
# Calculate Time correction for leap year
#---------------------------------------------------------#
my $value = ($year - 1970)/4;
my $fixval = int($value);
my $truevalue = 0;
my $monthday = $month.$day;
if ($fixval eq 0 and $monthday gt "0229"){
$truevalue = 1;
} elsif ($fixval eq 0 and $monthday lt "0229") {
$truevalue = 0;
} elsif ($fixval gt 0 and $monthday gt "0229") {
$truevalue = $fixval + 1;
} else {
$truevalue = $fixval;
}
#---------------------------------------------------------#
$time += 86400 * $truevalue ; #Correction for leap year
$time -= 36000 ; #Correction for Time Zone
return $time;
}
#Determine if it is a leep year.
sub leap {
my $inyear = $_[0];
# rule leapyear is divisable by 4 except those divisable by 100 unless
divisable by 400...
my $dividedyear = $inyear / 4;
my $intyear = int($inyear / 4);
my $centryyear = $inyear / 100;
my $intcentryyear = int($inyear / 100);
if ($dividedyear == $intyear){
$leap = 29;
} elsif ($dividedyear == $intyear && $centryyear == $intcentryyear){
$leap = 28;
} else {
$leap = 28;
}
print $leap;
return $leap;
}
------------------------------
Date: Wed, 07 Feb 2001 04:55:41 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Hashes
Message-Id: <x7pugvozw2.fsf@home.sysarch.com>
>>>>> "b" == bakor <bakor@my-deja.com> writes:
b> %days_in_month=(Jan,31,Feb,28,Mar,31);
that won't compile under use strict. either quote those tokens or use =>
b> $month=`date +%b`;
there is a better way to get the month number. read
perldoc -f localtime
in fact, the date command could get you the number directly but that is
not the problem you has asked about. but using `date` is considered poor
perl anyway.
b> print $month;
hmm. try printing "[$month]" and looking at that. you will be surprised
where the ] is printed. that should tell you how to fix it.
b> print $days_in_month($month);
and if don't figure it out, read this:
perldoc -f chomp.
in fact you should read that anyhow.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com
------------------------------
Date: Wed, 07 Feb 2001 02:11:30 GMT
From: tvn007@my-deja.com
Subject: hex to binary conversion ? Please help
Message-Id: <95qasd$bpj$1@nnrp1.deja.com>
Hi ,
I have hex number and would like to convert it to binanry.
Could someone help me with a PERL script to do this.
Example:
If I have a hex number: BC
I would like to have binary number: b10111100
Example 2:
If I have a hex numbe: FF
I would like to have a binary number: b11111111
Thanks for any help,
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Wed, 07 Feb 2001 02:58:15 GMT
From: sjs@linux.ca (Steven Smolinski)
Subject: Re: hex to binary conversion ? Please help
Message-Id: <slrn981itl.1pl.sjs@ragnar.stevens.gulch>
tvn007@my-deja.com <tvn007@my-deja.com> wrote:
> Hi ,
>
> I have hex number and would like to convert it to binanry.
man perldata # for section "Scalar value constructors"
man perlfunc # for printf() and hex()
You might take a grep through your local pod directory next time.
> Could someone help me with a PERL script to do this.
Don't know what PERL is. See man perlfaq1 for why there is no such
thing.
> Example:
>
> If I have a hex number: BC
> I would like to have binary number: b10111100
Perhaps you should be consistent. Either 0xbc => b10111100, or bc =>
10111100, but anyway...
This is pretty trivial, though, as long as you have perl 5.6.0:
% perl -we 'printf "b%b\n", hex shift' BC
b10111100
% perl -we 'printf "b%b\n", hex shift' FF
b11111111
%
Steve
--
Steven Smolinski => http://www.steven.cx/
------------------------------
Date: Tue, 06 Feb 2001 22:13:23 -0500
From: John Kitchin <jkitchin@udel.edu>
Subject: Re: Mail::Mailer
Message-Id: <3A80BD53.E2492ED2@udel.edu>
I suspect it is the mailer you are using. I get different behavior depending on
how I specify the mailer. For example, as you have it written, regardless of
what I put in $from1, the From field in the email will have my username@hostname
in it (in my case jkitchin@hostname). however if I use
$mail = new Mail::Mailer 'smtp',Server=>$server ;
then the From field of the resulting email has $from1@servername in it.
I ran these on a redhat linux box, with the first one using the default mail
program (mail) and the second one using the university smtp server which is not
local on my machine.
Don't know if this helps at all, but it is all I know.
j
Jonas Nilsson wrote:
> Hello.
> I'm trying make the header field "From:" to work but it doesn't. Can anybody
> help?
>
> $mail = new Mail::Mailer;
> $mail->open({'Subject' => $subject1, 'To' => $recipient1, 'From' =>
> $from1});
> print $mail $mailcontent1;
> $mail->close;
>
> And the mail recieved:
>
> Received: (from nobody@localhost)
> by ifm.liu.se (8.11.0/8.11.0) id f15FJa519028
> for jonni; Mon, 5 Feb 2001 16:19:36 +0100 (MET)
> Date: Mon, 5 Feb 2001 16:19:36 +0100 (MET)
> From: nobody@ifm.liu.se
> Message-Id: <200102051519.f15FJa519028@ifm.liu.se>
> To: jonni@ifm.liu.se
> Subject: Your order
> Content-Length: 151
------------------------------
Date: Wed, 07 Feb 2001 04:37:39 GMT
From: garry@zvolve.com (Garry Williams)
Subject: Re: Modules/Constants.
Message-Id: <ni4g6.1366$561.8915@eagle.america.net>
On Mon, 05 Feb 2001 18:51:56 +0000, Kelly Dorset
<dorsettest@uk.insight.com> wrote:
>Garry Williams wrote:
>> On Mon, 05 Feb 2001 00:26:22 +0000, Kelly Dorset
>> <dorsettest@uk.insight.com> wrote:
>
>Ok, put use script back in, and put all of the constant names into the
>exporter thingy in the constants script but I'm still getting error when
>trying to use them.
>
>Here is a chunk:
>------
>sub BEGIN {
>
> print "Const module imported\n";
>
> require Exporter;
> use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
>
> @ISA = qw(Exporter);
> @EXPORT = qw(
> C_SYSTEM
> C_HELLO
> C_PING_REQ
> C_PING_REPLY
> etc etc for 70 others..
> C_SYSTEM);
> @EXPORT_OK = ();
> %EXPORT_TAGS = ( );
>}
This looks fine.
>When that script is used by the coms modules, here is the error:
>
>Bareword "C_LOGIN" not allowed while "strict subs" in use at
> /scripts/include/coms.pm line 95 (#1)
[snip]
>I'm guess it must be a problem in the way I'm using exported here.. but
>I have no clue really..
Are you sure that you have "use chunk;" in coms.pm? Are you sure that
chunk.pm defines C_LOGIN? Are you sure that 'C_LOGIN' is contained in
@chunk::EXPORT?
--
Garry Williams
------------------------------
Date: Wed, 07 Feb 2001 02:11:58 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: Need Help Checking IP Address Syntax w/ PERL?
Message-Id: <t81bnep2bmog58@corp.supernews.com>
brian d foy <comdog@panix.com> wrote:
> In article <95q1m0$9in$1@usenet.otenet.gr>, "SysAdmin"
> <root@novastar.dtdns.net> wrote:
>> $_="100.168.0.1";
>>
>> die "not valid IP format\n" if ! /^\d+\.\d+\.\d+\.\d+$/;
> that should say "dotted decimal IP format, although it is still
> incorrect.
I agree with the error stating the format, since IP addresses
can be represented in other ways (for example, as a 32-bit
binary number, which is what it really is when the computer
uses it). The program snippet "SysAdmin" posted does take
care of octets below 1 or above 254, but not in the regex.
See the complete snippet below.
> what about all of these invalid IP numbers:
> 256.0.0.1
> 1000.0.0.1
> and so on.
The complete snippet posted by "SysAdmin" is this (although
I re-indented it):
#-------------------------------------------------------
$_="100.168.0.1";
die "not valid IP format\n" if ! /^\d+\.\d+\.\d+\.\d+$/;
@ip=split /\./;
foreach (@ip) {
if (($_<=0) || ($_>=255)) {
die "IP octet $_ out of range\n";
}
}
undef @ip;
print "ok";
#-------------------------------------------------------
The foreach(){} loop should take care of octets out of range,
and even gives separate error messages based on the layout of
the address and the range of the octet. Multiple die statements
might not be the clearest way to handle this, but it does keep
from needing extra control variables.
A rewrite of "SysAdmin"'s code to handle this as a subroutine
is just below:
#-------------------------------------------------------
sub bad_dd_ip($) {
my $dotted = shift;
my $invalid = 0;
$invalid += 1 unless /^\d+\.\d+\.\d+\.\d+$/;
@ip = split /\./ $dotted;
foreach( @ip ) {
$invalid += 2 if( ( $_ <= 0 ) || ( $_ >= 255 ) );
}
return($invalid);
}
#-------------------------------------------------------
And then code to call it:
#---------------------------------------------------------
my $address_problem = bad_dd_ip('254.254.0.1');
my $err_str = '';
if( $address_problem == 0 ) {
$err_str = 'Address ok.';
} elsif( $address_problem == 1 ) {
$err_str = 'The address is not in proper dotted decimal format.';
} elsif( $address_problem == 2 ) {
$err_str = 'One or more of the octets is out of range.';
} elsif( $address_problem == 3 ) {
$err_str = 'The address is not in proper dotted decimal format';
$err_str .= ' and one or more of the octets is out of range.';
} else {
$err_str = 'Unknown error verifying IP address.';
}
#----------------------------------------------------------
Gee, I need to learn something about concise posts. ;)
Chris
--
Christopher E. Stith
Even in the worst of times, there is always someone who's
never had it better. Even in the best of times, there is
always someone who's never had it worse.
------------------------------
Date: Wed, 07 Feb 2001 03:42:36 GMT
From: garry@zvolve.com (Garry Williams)
Subject: Re: Need Help Checking IP Address Syntax w/ PERL?
Message-Id: <Mu3g6.1334$561.7859@eagle.america.net>
On Tue, 06 Feb 2001 16:35:10 GMT, webbgroup <webbgroup@my-deja.com> wrote:
>I am writing a script right now that is asking for an IP address.
>It needs to check the syntax with limiting it to a valid IP address.
>Obviously the end user should be able to put in any three numbers, but
>not > 256 for each octect.
>
>Does anybody know a simpler way of doing it than the following?
>
>unless (($ip2add =~ [0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].[0-
>9][0-9][0-9]) || ($ipadd =~ [0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9]
>[0-9].[0-9][0-9]) || (ip2add =~ [0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-
>9].[0-9][0-9][0-9]) || ($ip2add =~ [0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]
>[0-9].[0-9][0-9][0-9]))
> {
> print "Invalid IP address.";
> }
>
>Any suggestions??? Comments???
#!/usr/bin/perl -l
use warnings;
use strict;
use Socket;
print "Invalid IP address." unless inet_aton(shift);
__END__
--
Garry Williams
------------------------------
Date: Wed, 07 Feb 2001 04:20:32 GMT
From: "Charles Warner" <charleswarner@bigfoot.com>
Subject: Pattern Matching
Message-Id: <k24g6.7018$iM6.859258@newsread1.prod.itd.earthlink.net>
Hello,
I have a pattern matching question that I am not sure about. Let's say I
have a block of text in a variable $text:
$text = qq~This is the
text I want
to
match using
as few lines of
code
as possible.~;
I want to be able to extract the text "using as few lines" from the block.
Of course the text won't actually be formatted that way. Do you have any
suggetions on how to do this with pattern matching?
Thank you!
Charles
------------------------------
Date: Wed, 7 Feb 2001 12:51:32 +1000
From: "Mike McPherson" <hafateltec@hotmail.com>
Subject: Re: Perl for the Fortran programmer
Message-Id: <95qd97$oda$1@brokaw.wa.com>
Would have to agree the O'REilly Learning Perl 2nd Edition is a must and I
also have Perl from Visual maranGraphics published by IDG which has
helped...
"Michael Prager" <Mike.Prager@noaa.gov> wrote in message
news:qol08tge2s7uccfcu1ih6f66iauvuhv9c3@4ax.com...
> I am looking for a book to teach myself basic Perl. Ideally, it
> would be written for programmers, but not necessarily for
> programmers of the C or related languages. By that, I mean the
> book should be concise, yet avoid explanations like "The foo
> statement works the same as in C" or the general assumption that
> something not explained will be as in C.
>
> (Reviews of the book _Learning Perl_ at Amazon lead me to think
> that it is concise, but that too much C knowledge is assumed.)
>
> My anticipated uses of Perl are scripting and text processing;
> Web-server use is unlikely.
> --
> Mike Prager
> NOAA, Beaufort, NC
> Standard disclaimers:
> * Opinions expressed are personal and not represented otherwise.
> * Any use of tradenames does not constitute a NOAA endorsement.
------------------------------
Date: Wed, 07 Feb 2001 04:03:20 GMT
From: "Stephen Deken" <shutupsteve@aNwOdSaPnAgM.com>
Subject: PLEASE HELP A NEWBIE
Message-Id: <cO3g6.324658$IP1.10724556@news1.giganews.com>
Okay, I'm not a newbie, but will someone please tell me what to do? I have
positively identified a bug in perl 5.6.0 and I have no idea what to do now.
I have reproduced it on three different systems.
The bug is with the POSIX module, and the display of the human-readable
months in strftime (the %b and %B formats). strftime is incorrectly
returning 'December' for 0 and 'Janurary' for 1, instead of 'Janurary' and
'Feburary'. This is not the way it should be.
Please help me? Anyone? Bhuler?
--sjd;
------------------------------
Date: 06 Feb 2001 22:14:54 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: PLEASE HELP A NEWBIE
Message-Id: <87vgqnruwx.fsf@limey.hpcc.uh.edu>
>> On Wed, 07 Feb 2001 04:03:20 GMT,
>> "Stephen Deken" <shutupsteve@aNwOdSaPnAgM.com> said:
> Okay, I'm not a newbie, but will someone please tell me
So you've immediately contrdicted your subject. Not good.
> The bug is with the POSIX module, and the display of the
> human-readable months in strftime (the %b and %B
> formats). strftime is incorrectly returning 'December'
> for 0 and 'Janurary' for 1, instead of 'Janurary' and
> 'Feburary'. This is not the way it should be.
I think you need to show some code. The following works
fine for me with 5.6:
use POSIX;
$d = strftime('%b %B', localtime);
print "$d\n";
Feb February
Are you sure you're passing the right args to strftime()?
hth
t
--
Eih bennek, eih blavek.
------------------------------
Date: Wed, 07 Feb 2001 02:40:49 GMT
From: cfedde@fedde.littleton.co.us (Chris Fedde)
Subject: Re: Text::Template Problem
Message-Id: <RA2g6.114$M8.170997248@news.frii.net>
In article <3a7e63b7.4ca8$207@news.op.net>,
Mark Jason Dominus <mjd@plover.com> wrote:
>In article <vnmr7tk06hr8l53c21u776sd94jndiqeso@4ax.com>,
>Bart Lateur <bart.lateur@skynet.be> wrote:
>>@items and %items is not the same variable.
>
>Thanks, Bart.
>
>I kiboze clpm for Text::Template questions, but I guess I got to this
>one before you did. Thanks for taking care of it for me.
>
Long live Kibo!
chris
--
This space intentionally left blank
------------------------------
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 V10 Issue 228
**************************************