[32010] in Perl-Users-Digest
Perl-Users Digest, Issue: 3274 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 2 00:09:25 2011
Date: Tue, 1 Feb 2011 21:09:07 -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 Tue, 1 Feb 2011 Volume: 11 Number: 3274
Today's topics:
Re: access anonymous variable <nospam.gravitalsun@hotmail.com.nospam>
Re: dynamically naming arrays <uri@StemSystems.com>
Re: dynamically naming arrays <cartercc@gmail.com>
Re: dynamically naming arrays <uri@StemSystems.com>
Re: dynamically naming arrays <tadmc@seesig.invalid>
Re: How to draw thin lines with Image::Magick? <hjp-usenet2@hjp.at>
Re: How to draw thin lines with Image::Magick? <stevemay@bogus.local>
matching string literals <morfysster@gmail.com>
Re: matching string literals sln@netherlands.com
Re: matching string literals <jurgenex@hotmail.com>
Re: matching string literals <tadmc@seesig.invalid>
Re: pulling data from a file/piping variables to if sta <nospam.gravitalsun@hotmail.com.nospam>
Re: pulling data from a file/piping variables to if sta <tadmc@seesig.invalid>
Re: pulling data from a file/piping variables to if sta <jurgenex@hotmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 01 Feb 2011 22:35:55 +0200
From: George Mpouras <nospam.gravitalsun@hotmail.com.nospam>
To: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: access anonymous variable
Message-Id: <4D486EAB.6070906@hotmail.com.nospam>
Στις 1/2/2011 5:25 μμ, ο/η Ted Zlatanov έγραψε:
> On Tue, 1 Feb 2011 14:20:58 +0200 "George Mpouras"<nospam.gravitalsun@hotmail.com.nospam> wrote:
>
> GM> At the following example I want to alter the value of $counter variable ,
> GM> but without using the the $iter code reference.
> GM> I 've tried many tricks using __ANON__ but without luck . Can you help ?
>
> GM> my $iter = Create_counter( 37);
>
> GM> for (1..3)
> GM> {
> GM> print "$_) ". $iter->() ."\n"
> GM> }
>
> GM> sub Create_counter
> GM> {
> GM> my $counter = $_[0];
>
> GM> return sub
> GM> {
> GM> $counter++
> GM> }
> GM> }
>
> Here's one way:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
> use Modern::Perl;
>
> {
> my $counter = 0;
>
> sub get_counter
> {
> $counter = shift @_ if scalar @_;
> return \$counter;
> }
> }
>
> ${get_counter(37)}++; # increments the hidden $counter, starting at 37
> say ${get_counter()};
> ${get_counter()}++; # increments the hidden $counter
> say ${get_counter()};
>
> This passes around a reference to a scoped variable; there's no other
> way to access $counter outside its scope and the value of $counter will
> persist between get_counter() calls.
>
> I'll warn you that this is probably the wrong way to go unless you
> really need this and know why. Can you explain your requirements a
> little bit?
>
> Ted
I was doing some experiments after a discussion with my colleague about
iterators and how they can protect their variables. I thought I could
find a way for direct access private variable of an anonymous subroutine
but maybe it is impossible after all. They are completely invisible.
use Data::Dumper;
$Data::Dumper::Deparse=1;
print STDOUT Data::Dumper::Dumper(\%{__PACKAGE__.'::'});
------------------------------
Date: Tue, 01 Feb 2011 14:09:19 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: dynamically naming arrays
Message-Id: <87vd131sio.fsf@quad.sysarch.com>
>>>>> "c" == ccc31807 <cartercc@gmail.com> writes:
c> sub process_infile
c> {
c> open INFILE, '<', 'pop-all-tables.sql' or die "Cannot process
c> INFILE, $!";
don't use bareword file handles. use lexicals.
open my $infile, '<', 'pop-all-tables.sql' or
die "Cannot open infile $!" ;
c> while (<INFILE>)
c> {
avoid the use of $_ when you can use named variables. much better for
the reader of the code.
while( my $line = <$infile> )
c> next unless /^insert/ ;
c> chomp;
c> $_ =~ /^insert\s+into\s+ # match and skip 'insert into '
why skip here when you look for insert just before?
since you are using /x mode it is usually better to use a pair delim
like {} for this.
c> ([^(]+)\( # match and save any word
c> character before the first open parens as $1
if it is a word char, use \w. also you say singular word char in the
comment but show + in the regex. which is it? be accurate when
commenting on regexes.
c> ([^)]+)\) # match and save any word
c> character between the first set of parens as $2
ditto
c> ([^)]+)\) # match and save any word
c> character between the second set of parens as $3
c> /x;
you don't check the regex for success. what happens if it fails?
c> my $table = $1;
c> my $keys = $2;
c> my $values = $3;
you could just assign those from the regex and save the use of $1, etc.
my( $table, $keys, $values ) = $line =~ m{ .... }x
c> my @keys = parse_line(',', 0, $keys);
what is parse_line()?
c> my @values = parse_line(',', 0, $values);
c> #print " TABLE $table: KEYS: [@keys] => VALUES: [@values]
c> \n";
c> my %hash;
c> @hash{@keys} = @values;
c> #foreach my $key (keys %hash) { print "$key => $hash{$key}
c> \n"; }
c> push @{$database{$table}}, \%hash;
c> }
c> close INFILE;
c> }
nothing too grotty there. just building up some hashes in a hash. common
stuff.
c> sub test_hash
c> {
c> foreach my $k1 (keys %database)
c> {
for this code learn to use the each func. much easier when looping over
a hash:
while( my( $key, $val ) = each %database ) {
now $val will be a hash ref and you do the same with %{$val} for the
next level. choosing a better name for $val would be good too but i
don't know the context of what is in the hash. or if this is for
debugging, just use Data::Dumper.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 1 Feb 2011 11:45:02 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: dynamically naming arrays
Message-Id: <be31e90d-6aa5-4425-945c-ea889c06ec8f@a28g2000prb.googlegroups.com>
On Feb 1, 2:09=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
Thank you. Please see comments below.
> don't use bareword file handles. use lexicals.
Okay
> avoid the use of $_ when you can use named variables. much better for
> the reader of the code.
Okay.
> why skip here when you look for insert just before?
It doesn't cause any harm, does it? I built up the RE piece by piece
and left it the way it was.
> if it is a word char, use \w. also you say singular word char in the
> comment but show + in the regex. which is it? be accurate when
> commenting on regexes.
I was actually looking for any character (not necessarily a word
character) that isn't a literal parens.
>
> you don't check the regex for success. what happens if it fails?
Good idea! Why didn't I think of that?
> you could just assign those from the regex and save the use of $1, etc.
See your comment above about readability and using named variables. I
wanted the $1 to be explicit in the code, lest I look at it tomorrow
and wonder what I had been smoking the day before.
> what is parse_line()?
It's an exported function in Text::ParseWords.
> for this code learn to use the each func. much easier when looping over
> a hash:
>
> =A0 =A0 =A0 =A0 while( my( $key, $val ) =3D each %database ) {
>
> now $val will be a hash ref and you do the same with %{$val} for the
> next level. choosing a better name for $val would be good too but i
> don't know the context of what is in the hash. or if this is for
> debugging, just use Data::Dumper.
Dumper doesn't name the hash elements, using $VAR1, etc. I use Dumper
for quick tests, but when I really want to look at what's going on, I
prefer to use a custom function.
CC.
------------------------------
Date: Tue, 01 Feb 2011 16:41:40 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: dynamically naming arrays
Message-Id: <87aaif1lgr.fsf@quad.sysarch.com>
>>>>> "c" == ccc31807 <cartercc@gmail.com> writes:
c> On Feb 1, 2:09pm, "Uri Guttman" <u...@StemSystems.com> wrote:
c> I was actually looking for any character (not necessarily a word
c> character) that isn't a literal parens.
then say that in your comment. comments which don't accurately reflect
the code are nasty.
>> you don't check the regex for success. what happens if it fails?
c> Good idea! Why didn't I think of that?
that should be a standard idiom for you.
>> you could just assign those from the regex and save the use of $1, etc.
c> See your comment above about readability and using named variables. I
c> wanted the $1 to be explicit in the code, lest I look at it tomorrow
c> and wonder what I had been smoking the day before.
but you don't even NEED $1 if you assign grabbed stuff to a list. and
your comments should say what is being grabbed and assigned. that is a
useful regex comment.
>> while( my( $key, $val ) = each %database ) {
>>
>> now $val will be a hash ref and you do the same with %{$val} for the
>> next level. choosing a better name for $val would be good too but i
>> don't know the context of what is in the hash. or if this is for
>> debugging, just use Data::Dumper.
c> Dumper doesn't name the hash elements, using $VAR1, etc. I use Dumper
c> for quick tests, but when I really want to look at what's going on, I
c> prefer to use a custom function.
no, Dumper can use whatever you tell it too for the top level. and you
can take the dumper text and simply do an s/// on $VAR1 to whatever you
want. writing code just to dump a hash tree is silly.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Tue, 01 Feb 2011 17:25:59 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: dynamically naming arrays
Message-Id: <slrnikh5a5.cdl.tadmc@tadbox.sbcglobal.net>
ccc31807 <cartercc@gmail.com> wrote:
> $table =~ s/ //g;
> $keys =~ s/ //g;
Regexes are good for processing strings of characters,
tranliteration is good for processing characters.
You are processing characters.
$table =~ tr/ //d;
$keys =~ tr/ //d;
has the same effect and will run faster too...
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Tue, 1 Feb 2011 21:39:15 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: How to draw thin lines with Image::Magick?
Message-Id: <slrnikgrrj.uet.hjp-usenet2@hrunkner.hjp.at>
On 2011-02-01 15:17, Josef Moellers <josef.moellers@ts.fujitsu.com> wrote:
> Am 1.2.2011 schrub Steve M.:
>> On Mon, 31 Jan 2011 15:09:22 +0100, Josef Moellers wrote:
>>> I'd like to draw lines only one pixel thin using Image::Magick. The
>>> following draws lines two pixels thick:
[...]
>>> $img->Draw(primitive => 'line',
>>> linewidth => 1, <- ??????
>>> method => 'point',
>>> points => '0,10 20,10',
>>> fill => 'black',
>>> antialias => 'false',
>>> );
[...]
>>> What options are required to draw lines one pixel wide?
[...]
>> $img -> Draw (
>> stroke => 'black',
>> primitive => 'line',
>> strokewidth => '1',
>> points => '1,10 20,10',
>> antialias => 'false',
>> );
>>
>> should work.
>
>
> Thanks but ... the line is still 2 pixels wide.
Both variants work for me with Image::Magick 6.6.0 (Debian package
perlmagick 8:6.6.0.4-3).
hp
------------------------------
Date: Tue, 01 Feb 2011 21:24:28 GMT
From: "Steve M." <stevemay@bogus.local>
Subject: Re: How to draw thin lines with Image::Magick?
Message-Id: <gS_1p.20077$0S6.2168@newsfe09.iad>
On Tue, 01 Feb 2011 16:17:15 +0100, Josef Moellers wrote:
> Am 1.2.2011 schrub Steve M.:
>
>>
>> Well...
>>
>> $img -> Draw (
>> stroke => 'black',
>> primitive => 'line',
>> strokewidth => '1',
>> points => '1,10 20,10',
>> antialias => 'false',
>> );
>>
>> should work.
>
>
> Thanks but ... the line is still 2 pixels wide.
>
>
> Josef
>
> NB My perl is 5.10.1, my perlmagick is "ImageMagick 6.5.7-8 2010-12-02
> Q16 http://www.imagemagick.org"
Hmmm...... That code worked with my Perl (5.10.1) and ImageMagick....
Well, if you didn't care about fiddling with anti-alias nonsense, I
suppose you could:
for( 1..20 ){
$img->Set("pixel[$_,10]"=>'black');
}
Not efficient perhaps.
hth,
--
\s
------------------------------
Date: Tue, 1 Feb 2011 15:45:55 -0800 (PST)
From: Morfys <morfysster@gmail.com>
Subject: matching string literals
Message-Id: <cfadb897-da02-4b66-9b89-c427683a3156@r4g2000vbq.googlegroups.com>
Hello,
I would like to able to match strings with several uninterpreted
characters (for instance, "/", "-", "(", and "=").
Ideally, I would like to not have to escape each of these characters
with a "\", as I have many different strings with many special
characters to try to match.
Is there any way to force the search of a string literally?
I have tried m/\Q $string \E/, but the issue is that $string can
contain "/", which perl interprets rather than taking it literally.
Thank you in advance.
------------------------------
Date: Tue, 01 Feb 2011 15:57:04 -0800
From: sln@netherlands.com
Subject: Re: matching string literals
Message-Id: <pa7hk6hsl2nklc80dd9ebmtcc6m4c8ug5i@4ax.com>
On Tue, 1 Feb 2011 15:45:55 -0800 (PST), Morfys <morfysster@gmail.com> wrote:
>Hello,
>
>I would like to able to match strings with several uninterpreted
>characters (for instance, "/", "-", "(", and "=").
>
>Ideally, I would like to not have to escape each of these characters
>with a "\", as I have many different strings with many special
>characters to try to match.
>
>Is there any way to force the search of a string literally?
>
>I have tried m/\Q $string \E/, but the issue is that $string can
>contain "/", which perl interprets rather than taking it literally.
>
>Thank you in advance.
Try m{\Q $string \E}.
As a side note, $string has spaces around it that is literal in the
regex unless m//x
-sln
------------------------------
Date: Tue, 01 Feb 2011 17:44:14 -0800
From: Jrgen Exner <jurgenex@hotmail.com>
Subject: Re: matching string literals
Message-Id: <ikdhk65j77ojh9iqoppucd1kkb8oqflei4@4ax.com>
Morfys <morfysster@gmail.com> wrote:
>I would like to able to match strings with several uninterpreted
>characters (for instance, "/", "-", "(", and "=").
>
>Ideally, I would like to not have to escape each of these characters
>with a "\", as I have many different strings with many special
>characters to try to match.
>
>Is there any way to force the search of a string literally?
There is standard function that probably does exactly what you are
asking for, see
perldoc -f index
jue
------------------------------
Date: Tue, 01 Feb 2011 19:57:10 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: matching string literals
Message-Id: <slrnikhe5j.cp3.tadmc@tadbox.sbcglobal.net>
Morfys <morfysster@gmail.com> wrote:
> I would like to able to match strings with several uninterpreted
> characters (for instance, "/", "-", "(", and "=").
3 of those 4 are not special in regular expressions.
> Ideally, I would like to not have to escape each of these characters
> with a "\",
You don't have to, as only "(" would require a backslash in the regex.
> as I have many different strings with many special
> characters to try to match.
>
> Is there any way to force the search of a string literally?
Yes, by using \Q
> I have tried m/\Q $string \E/, but the issue is that $string can
> contain "/", which perl interprets rather than taking it literally.
No it doesn't.
If you post real code that we can run that displays the problem
you are having, then we can help you fix it.
If you don't, we can't.
This code matches 4 times...
---------------------------
#!/usr/bin/perl
use warnings;
use strict;
$_ = 'foo / bar = baz - frob ( nitz';
my $string = '/';
print "matched [$string]\n" if m/ $string /;
$string = '=';
print "matched [$string]\n" if m/ $string /;
$string = '-';
print "matched [$string]\n" if m/ $string /;
$string = '(';
print "matched [$string]\n" if m/\Q $string \E/;
---------------------------
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Tue, 01 Feb 2011 22:53:28 +0200
From: George Mpouras <nospam.gravitalsun@hotmail.com.nospam>
Subject: Re: pulling data from a file/piping variables to if statement...
Message-Id: <ii9rs7$2rro$1@ulysses.noc.ntua.gr>
my $server;
my $file_with_servers= './servers';
my $file_to_check = './test';
open FILE, '<', $file_with_servers or die "$^E\n";
while(<FILE>) {chomp; $server{$_}=1}
close FILE;
open FILE, '<', $file_to_check or die "$^E\n";
while(<FILE>) {chomp; print "Server $_ found\n" if exists $server{$_} }
close FILE;
------------------------------
Date: Tue, 01 Feb 2011 17:42:37 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: pulling data from a file/piping variables to if statement...
Message-Id: <slrnikh69c.cdl.tadmc@tadbox.sbcglobal.net>
Adimax <adimax@gmail.com> wrote:
> % open (FILE, "/var/www/test");
You should always, yes *always*, check the return value from open().
You should include the reason for the failure to open() if any,
by outputting the $! variable somewhere.
You should use the 3-arg form of open.
You should use lexical filehandles.
You should only use double quotes when you _need_ one of the two
extra things over single quotes that double quotes gives you
(variable interpolation and/or backslash escapes).
open my $FILE, '<', '/var/www/test'
or die "could not open '/var/www/test' $!";
> % my @custom = <FILE>;
my @custom = <$FILE>;
> % close FILE;
close $FILE;
> And my if statement works if I define it specifically:
>
> % if ($server eq "apple" or $server eq "orange" or $server eq "pear")
What is this $server?
You have not assigned anything to it...
You should post a short and complete program that we can run that
demonstrates your problem.
Have you seen the Posting Guidelines that are posted here frequently?
> % # if ($server eq @custom) {
if (grep $_ eq $server, @custom) {
Though that could be ineffiecient if @custom is large.
Have you looked to see if your problem is answered in the Perl FAQ?
perldoc -q array
How can I tell whether a certain element is contained in a list
or array?
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Tue, 01 Feb 2011 17:54:00 -0800
From: Jrgen Exner <jurgenex@hotmail.com>
Subject: Re: pulling data from a file/piping variables to if statement...
Message-Id: <2qdhk6p87r257dguu53f0mk20von7d2n23@4ax.com>
Adimax <adimax@gmail.com> wrote:
[...]
>And my if statement works if I define it specifically:
>
>% if ($server eq "apple" or $server eq "orange" or $server eq "pear")
>{
>display some text
>% }
>% } # endif $server
>
>However, if I try to use something like:
>
>% # if ($server eq @custom) {
@custom is used in scalar context ('eq' implies scalar context for its
arguments). The scalar value of an array is the number of its elements,
i.e. with your sample data you are effectively testing
if ($server eq "3"){
Probably not what you had in mind.
>display some text
>% }
>% } # endif $server
>
>every $server is included in the 'display some text'. I know I'm off
>a bit here, but any help or references to examples would be greatly
>appreciated.
Your scenario is a FAQ, please see "perldoc -q contain"
How can I tell whether a certain element is contained in a list
or array?
for a far better explanation than I could possibly provide here.
jue
------------------------------
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 3274
***************************************