[31496] in Perl-Users-Digest
Perl-Users Digest, Issue: 2755 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 6 16:09:41 2010
Date: Wed, 6 Jan 2010 13:09:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 6 Jan 2010 Volume: 11 Number: 2755
Today's topics:
Re: correct way to call a perl subroutine <ben@morrow.me.uk>
Re: Determine physical location of IP <mytechsolutions@gmail.com>
Re: Determine physical location of IP <user@example.net>
LWP::UserAgent HTTP POST authentication problem <graham.stow@stowassocs.co.uk>
Re: LWP::UserAgent HTTP POST authentication problem <pshendley@gmail.com>
Perl DBI module hanging (transaction isolation) <dn.perl@gmail.com>
Re: Perl DBI module hanging (transaction isolation) <ben@morrow.me.uk>
Re: perl in BartPE: locale warning <keith.watson@cc.gatech.edu>
Re: Regex, spaces in pattern stored in variable. <ben@morrow.me.uk>
Re: Regex, spaces in pattern stored in variable. <justin.0911@purestblue.com>
Re: Regex, spaces in pattern stored in variable. <justin.0911@purestblue.com>
Re: significant figures <ben@morrow.me.uk>
Re: significant figures <stanley@peak.org>
Re: unicode newbie, can you help? <bmb@mail.libs.uga.edu>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 6 Jan 2010 12:05:07 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: correct way to call a perl subroutine
Message-Id: <jsae17-97v.ln1@osiris.mauzo.dyndns.org>
Quoth Parapura Rajkumar <ptrajkumar@gmail.com>:
> On Jan 5, 8:12 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth Parapura Rajkumar <ptrajku...@gmail.com>:
> >
<snip>
> > > use strict;
> > > use warnings;
> >
> > > package LogHelper;
> > > use base 'Exporter';
> > > our @EXPORT = ('WriteLine');
> >
> > > sub WriteLine
> > > {
> > > print @_ , "\n";
> > > }
> >
> > > package main;
> > > import LogHelper;
> >
> > > sub WriteLine2
> > > {
> > > print @_ , "\n";
> > > }
> >
> > > #Call 1
> > > WriteLine2 "Test1";
> >
> > > #Call 2
> > > WriteLine( "Test2" );
> >
<snip>
> > The 'workaround' (or rather, the correct way to do things) is to put
> > LogHelper in its own .pm file and pull it in with 'use'. Since this
> > calls import at compile time, &main::WriteLine will be visible at the
> > point where the call is compiled. If you have some particular reason for
> > doing the import at runtime, you just have to put up with needing the
> > parens on the call.
>
> Thanks for the explanation. It does seem to work with 'use'. But
> unfortunately using 'use' doesn't seem to be legal when you have all
> your packages defined in the same file.
>
> A bit disappointed with perl flexibility here :(
If you read the docs for 'use', 'require' and 'do' carefully, you will
find out that it can be made to work like this:
# The package must be defined at compile time
BEGIN {
package LogHelper;
# This tells 'require' that we've already loaded LogHelper.pm
$INC{"LogHelper.pm"} = __FILE__;
use base qw/Exporter/;
our @EXPORT = qw/WriteLine/;
sub WriteLine { ... }
}
# we are back in package main here, since the package statement was
# scoped to the BEGIN block
use LogHelper;
WriteLine "foo";
but I wouldn't recommend it except under rather exceptional
circumstances.
Ben
------------------------------
Date: Wed, 6 Jan 2010 10:22:55 -0800 (PST)
From: Jopa <mytechsolutions@gmail.com>
Subject: Re: Determine physical location of IP
Message-Id: <9aae592e-a08e-4372-aa3c-4b0c0abbcb4c@p8g2000yqb.googlegroups.com>
"but for anything more specific than 'country' you have to pay."
Actually I get a rough estimate of which city you are in/near from the
free geoip data.
ie. GeoLiteCity.dat data file. It gives me postal code, latitude/
longitude also.
use Geo::IP;
my $gi = Geo::IP->open("/path/to/GeoLiteCity.dat", GEOIP_STANDARD);
my $record = $gi->record_by_name("192.168.0.1");
my $city = $record->city;
my $state = $record->region_name;
my $state_abbr = $record->region;
It would be impossible to figure out exact location, all you can ever
get is a near guess
which in most cases is reasonable. I live in the country and it gives
me the nearest city.
Jopa
------------------------------
Date: Wed, 06 Jan 2010 13:52:47 -0500
From: monkeys paw <user@example.net>
Subject: Re: Determine physical location of IP
Message-Id: <xdudna6t6vtsRtnWnZ2dnUVZ_gidnZ2d@insightbb.com>
The conundrum of finding IP location was more profound than
i thought. You get into human privacy as well as the limits of the
information in a TCP/IP packet.
I found from looking into this that the only way to pseude achieve
the ip2location function is downloading the most current CPAN
GeoLiteCity CSV files and process them with the Geo::IP module.
The GeoLiteCity files use latitude/longitude coordinates *
with a typical entry in the file looking like:
2401,"US","KY","Covington","41011",39.0657,-84.5290,515,859
It was interesting, Thanks everyone.
Ben Morrow wrote:
> Quoth Justin C <justin.1001@purestblue.com>:
>> In article <IqWdnQP-M6YzKN7WnZ2dnUVZ_ridnZ2d@insightbb.com>, monkeys paw wrote:
>>> I know some www sites perform this service, i'm interested in
>>> the underlying network code that could accomplish this. A working
>>> example would be fab, a point in the right direction much appreciated.
>> It's far from reliable. I'm on the Sussex coast (UK) and those things
>> put me either in Windsor, or in High Wycombe. There are about 30 million
>> people between me and those locations.
>
> Generally speaking they have only the location of your ISP, at best,
> since they are the people who actually own that IP. ISPs selling
> customer addresses to random websites would almost certainly fall foul
> of the Data Protection Act.
>
> Ben
>
------------------------------
Date: Wed, 6 Jan 2010 18:04:48 -0000
From: "Graham" <graham.stow@stowassocs.co.uk>
Subject: LWP::UserAgent HTTP POST authentication problem
Message-Id: <xKWdnfHhU51fTdnWnZ2dnUVZ8gmdnZ2d@bt.com>
Below is a suggested 'Automatic Payment Confirmation' script copied and
pasted from the Nochex Developer's forum (Nochex themselves do not offer
support in Perl). This thread is now closed and I suspect the Nochex forum
does not get much traffic, hence this post here. I have spotted one error
(the line '$mail_method = "sendmail";' needs to be added if using sendmail).
However, even with this correction, the script always goes down the first of
the four 'if (res->' options (i.e. (res->is_error)), and thus none of my
test transactions are being authorised, which makes me suspect that there is
something wrong with the following lines:-
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$req = new HTTP::Request "POST","https://www.nochex.com/nochex.dll/apc/apc";
$req->content_type("application/x-www-form-urlencoded");
$req->content($query);
$res = $ua->request($req);
Anyone any ideas?
#!/usr/bin/perl
# REPLACE THIS WITH YOUR NOCHEX EMAIL ADDRESS.
$admin_email = "sales@geodetech.com";
# THIS SHOULD BE EITHER smtp OR sendmail DEPENDING
# ON THE MAIL METHOD YOU WANT TO USE.
$mail_method = "smtp";
# IF YOU ARE USING SENDMAIL TO SEND EMAIL
# SET THE PATH TO SENDMAIL ON YOUR SERVER.
# IN ALL PROBABILITY THE DEFAULT WILL WORK
$sendmail_path = "/usr/sbin/sendmail -t";
# IF YOU ARE USING SMTP TO SEND EMAIL
# SPECIFY THE SMTP SERVER TO USE.
# IN ALL PROBABILITY THE DEFAULT WILL WORK
$smtp_server = "localhost";
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $query);
$count = 0;
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$variable{$name} = $value;
$count++;
}
$transaction_id = $variable{'transaction_id'};
$transaction_date = $variable{'transaction_date'};
$from_email = $variable{'from_email'};
$to_email = $variable{'to_email'};
$order_id = $variable{'order_id'};
$amount = $variable{'amount'};
$security_key = $variable{'security_key'};
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$req = new HTTP::Request "POST","https://www.nochex.com/nochex.dll/apc/apc";
$req->content_type("application/x-www-form-urlencoded");
$req->content($query);
$res = $ua->request($req);
if ($res->is_error) {
$subject = "Perl APC Script Error - Could not connect to Nochex servers";
$message = "Your Perl APC script was called but returned an error because
it \ncould not connect with the NOCHEX servers.\n.";
} elsif ($res->content eq "AUTHORISED") {
$subject = "APC Result - AUTHORISED";
$message = "NOCHEX RESPONSE:
AUTHORISED\n-----------------------------------------------\nOrder submitted
with ID:
".$order_id."\n-----------------------------------------------\ntransaction
id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
email:\t".$from_email."\nto
email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nsecurity
key:\t".$security_key."\n";
} elsif ($res->content eq "DECLINED") {
$subject = "APC Result - DECLINED";
$message = "NOCHEX RESPONSE:
DECLINED\n-----------------------------------------------\nOrder submitted
with ID:
".$order_id."\n-----------------------------------------------\ntransaction
id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
email:\t".$from_email."\nto
email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nsecurity
key:\t".$security_key."\n";
} else {
$subject = "Invalid APC Result Returned";
$message = "The NOCHEX APC server returned an unrecognised or invalid
response. In \nall probability due to en error in your code but could be the
APC \nserver screwing
up.\n-----------------------------------------------\nOrder submitted with
ID:
".$order_id."\n-----------------------------------------------\ntransaction
id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
email:\t".$from_email."\nto
email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nsecurity
key:\t".$security_key."\n";
}
print "Content-Type: text/plain\n\n";
if ($mail_method eq "smtp") {
use Net::SMTP;
$smtp = Net::SMTP->new($smtp_server);
$smtp->mail($ENV{USER});
$smtp->to($admin_email);
$smtp->data();
$smtp->datasend("From: APC Script <apc_script@your.website>\n");
$smtp->datasend("To: ".$admin_email."\n");
$smtp->datasend("Subject: ".$admin_email."\n");
$smtp->datasend("Content-Type: text/plain\n");
$smtp->datasend("\n");
$smtp->datasend($message);
$smtp->dataend();
$smtp->quit;
} elsif ($mail_method eq "sendmail") {
open(SENDMAIL, "|$sendmail_path") or die "Cannot open sendmail: $!";
print SENDMAIL "From: APC Script <apc_script@your.website>\n";
print SENDMAIL "To: ".$admin_email."\n";
print SENDMAIL "Subject: ".$subject."\n";
print SENDMAIL "Content-Type: text/plain\n\n";
print SENDMAIL $message;
close(SENDMAIL);
}
------------------------------
Date: Wed, 6 Jan 2010 11:33:52 -0800 (PST)
From: Sherm Pendley <pshendley@gmail.com>
Subject: Re: LWP::UserAgent HTTP POST authentication problem
Message-Id: <5a663d9b-dc49-4cde-bc1d-558320d5446a@m3g2000yqf.googlegroups.com>
On Jan 6, 12:04=A0pm, "Graham" <graham.s...@stowassocs.co.uk> wrote:
> Below is a suggested 'Automatic Payment Confirmation' script copied and
> pasted from the Nochex Developer's forum (Nochex themselves do not offer
> support in Perl). This thread is now closed and I suspect the Nochex foru=
m
> does not get much traffic, hence this post here. I have spotted one error
> (the line '$mail_method =3D "sendmail";' needs to be added if using sendm=
ail).
> However, even with this correction, the script always goes down the first=
of
> the four 'if (res->' options (i.e. (res->is_error)), and thus none of my
> test transactions are being authorised, which makes me suspect that there=
is
> something wrong with the following lines:-
>
> use LWP::UserAgent;
> $ua =3D new LWP::UserAgent;
> $req =3D new HTTP::Request "POST","https://www.nochex.com/nochex.dll/apc/=
apc";
> $req->content_type("application/x-www-form-urlencoded");
> $req->content($query);
> $res =3D $ua->request($req);
>
> Anyone any ideas?
>
> #!/usr/bin/perl
>
> # REPLACE THIS WITH YOUR NOCHEX EMAIL ADDRESS.
> $admin_email =3D "sa...@geodetech.com";
>
> # THIS SHOULD BE EITHER smtp OR sendmail DEPENDING
> # ON THE MAIL METHOD YOU WANT TO USE.
> $mail_method =3D "smtp";
>
> # IF YOU ARE USING SENDMAIL TO SEND EMAIL
> # SET THE PATH TO SENDMAIL ON YOUR SERVER.
> # IN ALL PROBABILITY THE DEFAULT WILL WORK
> $sendmail_path =3D "/usr/sbin/sendmail -t";
>
> # IF YOU ARE USING SMTP TO SEND EMAIL
> # SPECIFY THE SMTP SERVER TO USE.
> # IN ALL PROBABILITY THE DEFAULT WILL WORK
> $smtp_server =3D "localhost";
>
> read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
>
> @pairs =3D split(/&/, $query);
> $count =3D 0;
> foreach $pair (@pairs) {
> =A0 ($name, $value) =3D split(/=3D/, $pair);
> =A0 $value =3D~ tr/+/ /;
> =A0 $value =3D~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> =A0 $variable{$name} =3D $value;
> =A0 $count++;
>
> }
>
> $transaction_id =3D $variable{'transaction_id'};
> $transaction_date =3D $variable{'transaction_date'};
> $from_email =3D $variable{'from_email'};
> $to_email =3D $variable{'to_email'};
> $order_id =3D $variable{'order_id'};
> $amount =3D $variable{'amount'};
> $security_key =3D $variable{'security_key'};
>
> use LWP::UserAgent;
> $ua =3D new LWP::UserAgent;
> $req =3D new HTTP::Request "POST","https://www.nochex.com/nochex.dll/apc/=
apc";
> $req->content_type("application/x-www-form-urlencoded");
> $req->content($query);
> $res =3D $ua->request($req);
>
> if ($res->is_error) {
> =A0 $subject =3D "Perl APC Script Error - Could not connect to Nochex ser=
vers";
> =A0 $message =3D "Your Perl APC script was called but returned an error b=
ecause
> it \ncould not connect with the NOCHEX servers.\n.";} elsif ($res->conten=
t eq "AUTHORISED") {
>
> =A0 $subject =3D "APC Result - AUTHORISED";
> $message =3D "NOCHEX RESPONSE:
> AUTHORISED\n-----------------------------------------------\nOrder submit=
ted
> with ID:
> ".$order_id."\n-----------------------------------------------\ntransacti=
on
> id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
> email:\t".$from_email."\nto
> email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nse=
cu rity
> key:\t".$security_key."\n";} elsif ($res->content eq "DECLINED") {
>
> =A0 $subject =3D "APC Result - DECLINED";
> $message =3D "NOCHEX RESPONSE:
> DECLINED\n-----------------------------------------------\nOrder submitte=
d
> with ID:
> ".$order_id."\n-----------------------------------------------\ntransacti=
on
> id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
> email:\t".$from_email."\nto
> email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nse=
cu rity
> key:\t".$security_key."\n";} else {
>
> =A0 $subject =3D "Invalid APC Result Returned";
> $message =3D "The NOCHEX APC server returned an unrecognised or invalid
> response. In \nall probability due to en error in your code but could be =
the
> APC \nserver screwing
> up.\n-----------------------------------------------\nOrder submitted wit=
h
> ID:
> ".$order_id."\n-----------------------------------------------\ntransacti=
on
> id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
> email:\t".$from_email."\nto
> email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nse=
cu rity
> key:\t".$security_key."\n";
>
> }
>
> print "Content-Type: text/plain\n\n";
>
> if ($mail_method eq "smtp") {
> =A0 use Net::SMTP;
> =A0 $smtp =3D Net::SMTP->new($smtp_server);
> =A0 $smtp->mail($ENV{USER});
> =A0 $smtp->to($admin_email);
> =A0 $smtp->data();
> =A0 $smtp->datasend("From: APC Script <apc_scr...@your.website>\n");
> =A0 $smtp->datasend("To: ".$admin_email."\n");
> =A0 $smtp->datasend("Subject: ".$admin_email."\n");
> =A0 $smtp->datasend("Content-Type: text/plain\n");
> =A0 $smtp->datasend("\n");
> =A0 $smtp->datasend($message);
> =A0 $smtp->dataend();
> =A0 $smtp->quit;} elsif ($mail_method eq "sendmail") {
>
> =A0 open(SENDMAIL, "|$sendmail_path") or die "Cannot open sendmail: $!";
> =A0 print SENDMAIL "From: APC Script <apc_scr...@your.website>\n";
> =A0 print SENDMAIL "To: ".$admin_email."\n";
> =A0 print SENDMAIL "Subject: ".$subject."\n";
> =A0 print SENDMAIL "Content-Type: text/plain\n\n";
> =A0 print SENDMAIL $message;
> =A0 close(SENDMAIL);
>
>
>
> }
Why did you post this here? Seems to me *someone* forgot what groups
are for what! ;)
sherm--
------------------------------
Date: Wed, 6 Jan 2010 03:50:35 -0800 (PST)
From: "dn.perl@gmail.com" <dn.perl@gmail.com>
Subject: Perl DBI module hanging (transaction isolation)
Message-Id: <8cd3e97b-de0a-4cb5-b3dc-765c12e538f3@a32g2000yqm.googlegroups.com>
I am running a perl script (on ancient Perl 5.6, with which I am
stuck) which uses DBI module. The script runs select, delete and
insert statements against an Oracle table. The script runs properly
most of the time.
I also have a 'sqlplus' session running.
There are 5 records in table tt22.
Case A)
In sqlplus session: I delete all the 5 records from the table; issue a
'commit', and run the perl script. It runs fine.
Case B)
In sqlplus session, I delete all the 5 records from the table but do
not run commit.
Then I run the perl script but it hangs.
I issue 'commit' via sqlplus, and the 'hanging' perl script starts
running at once.
I do not want my perl script to hang. Is it possible to set a
transaction isolation level via DBI (perhaps immediately after
connecting to the database DBI->connect) which will enable the perl
script to run smoothly even when I have deleted some records in the
sqlplus session without commiting the delete action.
------------------------------
Date: Wed, 6 Jan 2010 12:23:19 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Perl DBI module hanging (transaction isolation)
Message-Id: <nube17-97v.ln1@osiris.mauzo.dyndns.org>
Quoth "dn.perl@gmail.com" <dn.perl@gmail.com>:
>
> I am running a perl script (on ancient Perl 5.6, with which I am
> stuck) which uses DBI module. The script runs select, delete and
> insert statements against an Oracle table. The script runs properly
> most of the time.
>
> I also have a 'sqlplus' session running.
> There are 5 records in table tt22.
>
> Case A)
> In sqlplus session: I delete all the 5 records from the table; issue a
> 'commit', and run the perl script. It runs fine.
>
> Case B)
> In sqlplus session, I delete all the 5 records from the table but do
> not run commit.
> Then I run the perl script but it hangs.
> I issue 'commit' via sqlplus, and the 'hanging' perl script starts
> running at once.
>
> I do not want my perl script to hang. Is it possible to set a
> transaction isolation level via DBI (perhaps immediately after
> connecting to the database DBI->connect) which will enable the perl
> script to run smoothly even when I have deleted some records in the
> sqlplus session without commiting the delete action.
Going by the Oracle SQL docs, you would want to use
$dbh->do("ALTER SESSION SET ISOLATION_LEVEL = ...");
however it seems that Oracle only supports SERIALIZABLE and READ
COMMITTED, neither of which will do what you want. (SERIALIZABLE will
cause the SELECT to fail rather than hang.) Check your Oracle
documentation to see if this applies to your version.
Ben
------------------------------
Date: Wed, 6 Jan 2010 17:26:15 +0000 (UTC)
From: "Keith R. Watson" <keith.watson@cc.gatech.edu>
Subject: Re: perl in BartPE: locale warning
Message-Id: <hi2h3n$23h$1@news-int2.gatech.edu>
Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote in
news:slrnhk6n7g.1o8.nospam-abuse@powdermilk.math.berkeley.edu:
> On 2010-01-05, Keith R. Watson <keith.watson@cc.gatech.edu> wrote:
>> BartPE by default does not have locale information. You can use the
>> following plugin to add locale information to Windows XP:
>>
>> http://www.cc.gatech.edu/~krwatson/files/locale_defaults.zip
>
> A lot of thanks! I did not try it yet - I do not have BartPE-Builder
> configured to run under BartPE yet, so would need to run real Windows
> at some moment to rebuild...
>
>> The locale settings in the plugin are for the United States. If you
>> need the settings for a different locale you will need to find a
>> system configured the way you want, capture the registry settings,
>> and edit the Locale_Defaults.inf file.
>>
>> The settings in the [Default.AddReg] section come from
>> HKEY_CURRENT_USER\Control Panel\International
>>
>> The settings in the [SetupReg.AddReg] section come from
>> HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Nls
>>
>> Use RegEdit to export the registry settings to a .reg file then use
>> the following web site to convert the settings to the BartPE .inf
>> file format.
>
> Why? Cannot BartPE "just load the .reg" file at runtime?
During the Windows install process it ask several questions that tell it
how to set up locale. The BartPE utility builds a bootable Windows CD
using a Windows install CD and some external plugins, it doesn't actually
run the Windows installer so the locale questions never get asked.
You could try the following:
1. Put the .reg files on a USB flash drive.
2. Plug the flash drive into the machine
3. Boot the machine with the BartPE CD.
4. Import the .reg files
This would give the machine running the BartPE CD the locale registry
settings. However, I don't know if the locale settings take effect
immediately or not. Some registry keys work as soon as you change them in
the registry and others require telling the operating system to reload
their settings from the registry. You could try it and see if it works.
The down side is you would always have use the USB flash drive to load
the .reg files every time you boot. If you use the plugin it adds the
registry keys to the BartPE image so they work every time without the
flash drive.
>
> And, btw, would not BartPE-builder be able to emit .reg for a certain
> subtree automatically?
When you the BartPE utility runs it gets the Windows files from a Windows
install CD and not from the machine the utility is running on. There are
not registry keys on the Windows install CD for it to export.
>
> Yours,
> Ilya
>
keith
--
Keith R. Watson Georgia Institute of Technology
Systems Support Specialist IV College of Computing
keith.watson@cc.gatech.edu 801 Atlantic Drive NW
(404) 385-7401 Atlanta, GA 30332-0280
------------------------------
Date: Wed, 6 Jan 2010 12:09:50 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Regex, spaces in pattern stored in variable.
Message-Id: <e5be17-97v.ln1@osiris.mauzo.dyndns.org>
Quoth Justin C <justin.0911@purestblue.com>:
> I have a list of strings, some contain spaces. The strings are used as
> patterns for a regex match, they don't seem to be working! I tried
> substituting the space with '\s' but I got warnings, and still no match.
>
> I'm sure there's a way to do this, but Google isn't providing any
> answers (more likely I don't know how to formulate a useful search).
>
> Anyway, here's what I have:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my @list = (
> "Fred Flintstone",
> "Barney Rubble",
> );
>
> while (<DATA>) {
> my $string = chomp $_;
chomp;
> foreach (@list) {
It's very confusing to have several nested loops all looping with $_.
for my $match (@list) {
> if ( $string =~ /($_)/ ) {
You normally want \Q\E when interpolating a plain string into a regex:
if (/(\Q$match\E)/) {
Ben
------------------------------
Date: Wed, 06 Jan 2010 11:56:18 -0000
From: Justin C <justin.0911@purestblue.com>
Subject: Re: Regex, spaces in pattern stored in variable.
Message-Id: <1615.4b447a62.6fd2c@zem>
On 2010-01-06, Peter Makholm <peter@makholm.net> wrote:
> Justin C <justin.0911@purestblue.com> writes:
>
>> #!/usr/bin/perl
>>
>> use warnings;
>> use strict;
>>
>> my @list = (
>> "Fred Flintstone",
>> "Barney Rubble",
>> );
>>
>> while (<DATA>) {
>> my $string = chomp $_;
>
> 'perldoc -f chomp' says:
>
> [...] It returns the total number of characters removed from all its
> arguments. [...]
Doh!
Thank you.
Justin.
--
Justin C, by the sea.
------------------------------
Date: Wed, 06 Jan 2010 14:46:24 -0000
From: Justin C <justin.0911@purestblue.com>
Subject: Re: Regex, spaces in pattern stored in variable.
Message-Id: <2bbd.4b44a240.cd851@zem>
On 2010-01-06, Ben Morrow <ben@morrow.me.uk> wrote:
>
> Quoth Justin C <justin.0911@purestblue.com>:
>> I have a list of strings, some contain spaces. The strings are used as
>> patterns for a regex match, they don't seem to be working! I tried
>> substituting the space with '\s' but I got warnings, and still no match.
>>
>> I'm sure there's a way to do this, but Google isn't providing any
>> answers (more likely I don't know how to formulate a useful search).
>>
>> Anyway, here's what I have:
>>
>> #!/usr/bin/perl
>>
>> use warnings;
>> use strict;
>>
>> my @list = (
>> "Fred Flintstone",
>> "Barney Rubble",
>> );
>>
>> while (<DATA>) {
>> my $string = chomp $_;
>
> chomp;
>
>> foreach (@list) {
>
> It's very confusing to have several nested loops all looping with $_.
>
You're telling me!
> for my $match (@list) {
>
>> if ( $string =~ /($_)/ ) {
>
> You normally want \Q\E when interpolating a plain string into a regex:
>
> if (/(\Q$match\E)/) {
Ah, yes, I see. That makes sense (this is, I think, my first pattern in
a scalar).
>
> Ben
>
Justin.
--
Justin C, by the sea.
------------------------------
Date: Wed, 6 Jan 2010 12:06:38 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: significant figures
Message-Id: <evae17-97v.ln1@osiris.mauzo.dyndns.org>
Quoth dan <spam.meplease@ntlworld.com>:
> Whilst trying to create something that would parse a number into one with
> an appropriate number of significant figures, I accidentally wrote this:
>
> sub sigfig {
> my ($sigfigs, $number) = @_;
>
> my $divisor = 10**(length(int $number) - $sigfigs);
> $number /= $divisor;
> $number = sprintf "%1.0f", $number;
> $number *= $divisor;
>
> return $number
> }
>
> which seems to work for positive numbers not in scientific notation.
It fails for cases like
sigfigs 2, 0.00123;
Ben
------------------------------
Date: Wed, 6 Jan 2010 12:52:51 -0800
From: John Stanley <stanley@peak.org>
Subject: Re: significant figures
Message-Id: <alpine.LRH.2.00.1001061235190.9129@shell.peak.org>
On Wed, 6 Jan 2010, Ben Morrow wrote:
> Quoth dan <spam.meplease@ntlworld.com>:
>> Whilst trying to create something that would parse a number into one with
>> an appropriate number of significant figures, I accidentally wrote this:
>>
>> sub sigfig {
>> my ($sigfigs, $number) = @_;
>>
>> my $divisor = 10**(length(int $number) - $sigfigs);
>> $number /= $divisor;
>> $number = sprintf "%1.0f", $number;
>> $number *= $divisor;
>>
>> return $number
>> }
>>
>> which seems to work for positive numbers not in scientific notation.
>
> It fails for cases like
>
> sigfigs 2, 0.00123;
>
> Ben
And for sigfigs 7, 123
The auto-conversion of numbers to strings and back in perl makes it
difficult to manage significant figures without keeping that information
separately. Even something simple like:
$number = "123.0000";
print "$number\n";
print $number + 1.0000 . "\n";
print "$number" + "1.0000" . "\n";
shows the loss of information about significance. The only printed result
that is correct wrt sig-figs is the first, and that's only because $number
started as a string and was printed as a string. In the latter two cases,
the addition forced the conversion.
If you could force perl to keep your numbers in string form and then
overload the math operators to deal with them as strings, you might be
successful...
The other respondent's suggestion to:
$number = sprintf("%d", $number); # just in case ...
return $number if $sigfigs > length($number);
shows an even more amazing lack of understanding of significant figures.
It would return "1" for sigfigs 4, 1.234, which is patently absurd.
------------------------------
Date: Wed, 06 Jan 2010 14:19:43 -0500
From: Brad Baxter <bmb@mail.libs.uga.edu>
Subject: Re: unicode newbie, can you help?
Message-Id: <hi2lmq$1hon$1@news.telesweet.net>
Jochen Lehmeier wrote:
> On Tue, 05 Jan 2010 15:40:24 +0100, alexxx.magni@gmail.com
> <alexxx.magni@gmail.com> wrote:
>
>> now I have a long text with standard ascii and, sometimes, cyrillic
>> text - and I want to filter it out (the cyrillic, of course).
>> I tried with
>>
>> perl -wne 'foreach($_){if (/(\p{InCyrillic})/){print"record $_ matches
>>>>> $1<<<\n"}else{print"ok\n"}}' test.htm
>>
>> but it fails to recognize it - what am I doing wrong?
>
> You are not telling Perl to expect utf8.
>
> Try to add "use Encoding;" and "$_ = decode_utf8($_)" (and maybe
> "binmode STDOUT,':utf8'" to avoid a warning "wide character..." while
> printing) at the appropriate places; see "perldoc Encode" and "perldoc
> -f binmode" for more details on this.
I found the perlunitut tutorial to be very helpful as
an entry point, e.g.,
http://perldoc.perl.org/perlunitut.html
If your experience is like mine, you'll find the author
is right when he says there, "You may have to re-read
this entire section a few times..."
--
Brad
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 2755
***************************************