[32091] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3355 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 19 00:09:24 2011

Date: Mon, 18 Apr 2011 21:09: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, 18 Apr 2011     Volume: 11 Number: 3355

Today's topics:
    Re: grabbing a facebook group <Uno@example.invalid>
    Re: grabbing a facebook group <uri@StemSystems.com>
    Re: Help! How to pass array indexes to subroutine? <Joey@still_Learning.invalid>
    Re: Help! How to pass array indexes to subroutine? <uri@StemSystems.com>
        Learning by example <justin.1104@purestblue.com>
    Re: Learning by example (Randal L. Schwartz)
    Re: Learning by example <cartercc@gmail.com>
    Re: Learning by example <willem@toad.stack.nl>
    Re: Learning by example (Randal L. Schwartz)
        module XML::Reader <klaus03@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 18 Apr 2011 20:35:51 -0600
From: Uno <Uno@example.invalid>
Subject: Re: grabbing a facebook group
Message-Id: <914ao7FjtqU1@mid.individual.net>

On 04/16/2011 10:08 PM, Tad McClellan wrote:
> Uno<Uno@example.invalid>  wrote:
>
>>>> I find that white space is not your
>                  ^^^^^^^^^^^
>>>> friend in perl.
>             ^^^^^^^
>>>
>>> When people do you the courtesy of patiently explaining how something
>>> works, please do have the courtesy in return of actually *reading* what
>>> they wrote.
>
>
>> Whatever, Mart,
>
>
> That's enough for me.
>
> So long.
>
>
>> I think you overestimate your courtesy when you try to
>> tell me what I read.
>
>
> You read it, but did not understand it!
>
>
>> The first 2 characters were whitespace
>
>
> ... so there was no perl.
>
> So there was no "white space in perl" anywhere in the thread.
>
>

$ cat fb1.pl
  #!/usr/bin/perl
use strict;

Are you stoned?  Tad, if either being correct or promoting the 
discussion in c.l.p.misc is part of your job description, I think you 
need to resign.
-- 
Uno


------------------------------

Date: Mon, 18 Apr 2011 23:38:38 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: grabbing a facebook group
Message-Id: <878vv67vwh.fsf@quad.sysarch.com>

>>>>> "U" == Uno  <Uno@example.invalid> writes:

  U> $ cat fb1.pl
  U>  #!/usr/bin/perl
  U> use strict;

  U> Are you stoned?  Tad, if either being correct or promoting the
  U> discussion in c.l.p.misc is part of your job description, I think you
  U> need to resign.

wow. you win the prize for the thickest head i have seen here since
moronzilla graced us with its smell.

do you even understand how unix run script programs? or what the #!
means? or where it MUST be in the file to work? read the man pages on
exec and its cousins (the unix system calls, not perl's). then come back
on your knees and beg for forgiveness. whatever babbling you may do, it
is obvious that the bug you had there IS NOT A PERL PROBLEM. perl never
even gets to see that file. so how could it be a perl problem?

sorry, i used logic on you. i won't make that mistake again as it
doesn't penetrate.

wow.

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: Sun, 17 Apr 2011 23:33:36 -0700
From: "Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Subject: Re: Help! How to pass array indexes to subroutine?
Message-Id: <cilnq6t4gf5h2rehnp8jihmkfom0nbdmcv@4ax.com>


Uri Guttman wrote:

>>>>>> "JLi" == Joey@still Learning invalid <Joey@still_Learning.invalid> writes:
>
>  JLi> Frédéric Perrin wrote:
>  >> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>  >>> I have an array:   @mod3Regions = ('','ridge1','ridge2','ridge3');
>  >>> 
>  >>> And a variable ,$valueOfInterest, that bitwise represents which selections
>  >>> have been made. 
>  >>> 
>  >>> I can compute as follows:
>  >>> 
>  >>> for (my $j=1; $j<6; $j++) {
>  >>> $mask = 1 << $j-1;
>  >> 
>  >> Where does $mask comes from ? Add `use strict;' at the top of your
>  >> script (just after the shebang line).
>  >> 
>  >>> $zzz = $valueOfInterest & $mask;
>  >>> if ($zzz != 0) {
>  >>> print "$mod3Regions[$j], \n";
>  >>> }
>  >>> } 
>  >>> 
>  >>> I want to use a subroutine, but I can't get it to output the selected
>  >>> array value. I'm doing this:
>  >>> 
>  >>> getValues($valueOfInterest, '$mod3Regions',4);
>  >>> 
>  >>> sub getValues($what, $who, 4) {
>  >> 
>  >> You just invented some syntax, there.
>
>  JLi> Actually, no. It's the same syntax as javascript (except for the $), and
>  JLi> Perl ignores the parameters. I write as much js as I write in Perl and
>  JLi> it's convenient to not have to change syntax rules if it's not essential.
>
>wow. perl uses that for prototypes, it isn't ignoring them. please don't
>be coding like this on any production job you get. that is insane.

Wow! I had no idea this could eventually cause problems. In deference to
your expertise, I corrected every instance within my scripts. Thank you.
>
>  JLi> decodeRegions($ridges,6,@mod3Regions); //(buttons, counter, array)
>
>  JLi> sub decodeRegions {
>  JLi> 	my ($what, $counter, @who) = @_;
>
>and what if you want to pass in 2 arrays? learn about array refs before
>you need them.

I appreciate the pointer, but I intend to implement my scripts for my
current situation only. I have no further aspirations in Perl. 
>
>  JLi> 	for (my $j=1; $j<$counter; $j++) {
>
>c syntax isn't perl either.
>
>	foreach my $j ( 0 .. $counter - 1 ) {

I understand that for/next loops are perfectlyt legit in Perl. While
foreach may be preferred insofar as programming elegance or program
efficiency, it is not an exclusive method for achieving its intended end.
>
>  JLi> 	my $mask = 1 << $j-1;
>
>don't need the -1 if your loop index is done correctly.
>
Or, had I numbered the buttons starting with 0. There's a reason I didn't.
>
>  JLi> 	$buttonSelected = $what & $mask;
>
>no need for the temp variable mask or even the temp var buttonselected
>
>  JLi> 	if ($buttonSelected != 0) {
>
>	if ( $what & (1 << $j) ) {
>
>  JLi> 	print $who[$j].", \n";
>
>why two args to print? one will do and is much easier to read.
>
>	print "$who[$j]\n";
>
I know and understand. Thanks.
-- 
Joey


------------------------------

Date: Mon, 18 Apr 2011 02:37:15 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Help! How to pass array indexes to subroutine?
Message-Id: <87ei50dq04.fsf@quad.sysarch.com>

>>>>> "JLi" == Joey@still Learning invalid <Joey@still_Learning.invalid> writes:

  JLi> Uri Guttman wrote:

  >> c syntax isn't perl either.
  >> 
  >> foreach my $j ( 0 .. $counter - 1 ) {

  JLi> I understand that for/next loops are perfectlyt legit in Perl. While
  JLi> foreach may be preferred insofar as programming elegance or program
  JLi> efficiency, it is not an exclusive method for achieving its intended end.

but it is the perl way and much faster, cleaner and less bug prone than
the c style loop. you may not care but others do and they read these
posts. 


  JLi> I know and understand. Thanks.

but will you learn better perl? :)

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: Mon, 18 Apr 2011 15:44:58 +0100
From: Justin C <justin.1104@purestblue.com>
Subject: Learning by example
Message-Id: <acvt78-i7n.ln1@zem.masonsmusic.co.uk>


I've been writing perl (poorly) for several years, not an awful lot of
it, but enough that I know how little I know. I pick up a lot here,
especially from the FAQ. I was thinking that it might be a good idea to
actually go over other people code, see how they do it, but more than
the snippets I see here in the newsgroup. My reasoning being that Linux
code improves with peer review, maybe my coding can improve by examining
other real code.

Of course, there is probably worse code out there than mine, and very
likely millions of lines of mediocre code too. I don't want to try and
learn from bad code, or pick up any bad habits so I was wondering if
there is any consensus here that any modules in particular are finely
written, and good examples of perl from which I might learn.

I look forward to your replies.

   Justin.

-- 
Justin C, by the sea.


------------------------------

Date: Mon, 18 Apr 2011 08:41:40 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Learning by example
Message-Id: <8639lfa7nv.fsf@red.stonehenge.com>

>>>>> "Justin" == Justin C <justin.1104@purestblue.com> writes:

Justin> Of course, there is probably worse code out there than mine, and
Justin> very likely millions of lines of mediocre code too. I don't want
Justin> to try and learn from bad code, or pick up any bad habits so I
Justin> was wondering if there is any consensus here that any modules in
Justin> particular are finely written, and good examples of perl from
Justin> which I might learn.

My 250+ Perl magazine articles are still available online downward from
http://www.stonehenge.com/merlyn/columns.html, although the CSS got
broken a couple of years ago and I haven't had time to fix it. :)

Beware that many small parts of the information is somewhat dated
now... I keep meaning to drop in a paragraph or two above the more
serious trouble spots but haven't had the tuits.

print "Just another Perl hacker,"; # the original

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion


------------------------------

Date: Mon, 18 Apr 2011 12:20:40 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Learning by example
Message-Id: <0b942220-e2e9-485c-bfe4-ca824d13a63c@w36g2000vbi.googlegroups.com>

On Apr 18, 10:44=A0am, Justin C <justin.1...@purestblue.com> wrote:
> I've been writing perl (poorly) for several years, not an awful lot of
> it, but enough that I know how little I know. I pick up a lot here,
> especially from the FAQ. I was thinking that it might be a good idea to
> actually go over other people code, see how they do it, but more than
> the snippets I see here in the newsgroup.

The best way to learn anything, Perl included, is to use it for useful
work. If you don't have a task that needs doing, you aren't going to
do it, but if it needs doing, then you will be motivated to do it.
This may sound trite, but it's absolutely true. Even using a tool
(like Perl) 15 minutes a day will increase your skills, so the task
may not be large ... it's enough if the task is real and you use the
tool (like Perl) to do it.

By the same token, looking at other people's code won't do much good
unless you have struggled with it yourself. Looking at the 'right
answers' won't help unless you know the right questions. Once you know
the right questions, you can make sense of the answers.

I'll also suggest that there isn't any mass of Perl (or anything else)
that you should know, once you get past the basics. When you know how
to do something well, it's a lot easier to add to it and learn how to
do other things well. In my case, I started writing Perl about 11
years ago in connection with developing web sites, and got pretty good
at CGI. As time went on, I changed jobs (several times) and learned
how to do new things.

Bottom line: use Perl daily, writing real solutions to real tasks.

(I recently had the opportunity to learn R. It helps that R is a
simple, small language, but it hurts that R requires substantial
domain knowledge in statistics. As it turned out, in about a week of
pretty heavy immersion I gained the ability to write programs (in
Perl) that spit out R objects. The motivation was directly job
related, and this motivation probably cut the time I spent learning R
by a couple of years. No joke! You can learn a lot with some time and
effort, and a lot of motivation.)

CC.


------------------------------

Date: Mon, 18 Apr 2011 20:00:51 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: Learning by example
Message-Id: <slrniqp63j.qks.willem@toad.stack.nl>

Justin C wrote:
) I've been writing perl (poorly) for several years, not an awful lot of
) it, but enough that I know how little I know. I pick up a lot here,
) especially from the FAQ. I was thinking that it might be a good idea to
) actually go over other people code, see how they do it, but more than
) the snippets I see here in the newsgroup. My reasoning being that Linux
) code improves with peer review, maybe my coding can improve by examining
) other real code.

Yes and no.  Perl is one of those languages where there are lots of
different methods, and most of the perl code out there isn't that good.

One suggestion would be that, whenever you come up with a solution in
Perl to some interesting problem, to try and find a better one.
And in that case, this NG is a very good place to ask for better ways
to do something.

) Of course, there is probably worse code out there than mine, and very
) likely millions of lines of mediocre code too. I don't want to try and
) learn from bad code, or pick up any bad habits so I was wondering if
) there is any consensus here that any modules in particular are finely
) written, and good examples of perl from which I might learn.

A lot of modules tend to contain hairy code, which is there to make
the use of the module easier.  In other words, the complex, iffy,
nasty, hairy stuff is hidden in a module and from the outside it's
all peachy.  So I wouldn't recommend looking at most modules.


And anyway, as another poster said, you learn the most from solving your
own problems, and also from learning new and better solutions to those same
problems.

Example: how to parse a simple ini-file (only 'key=value' pairs)

PS: This is just an example, so it's oversimplified.

Simple way:
my %keys;
for my $line (split "\n", $file) {
  if (my ($key, $val) = $line =~ /^(.*)=(.*)$/) {
    $keys{$key} = $val;
  }
}

Clever way:
my %keys = $file =~ /^(.*)=(.*)$/gm;


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


------------------------------

Date: Mon, 18 Apr 2011 13:13:08 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Learning by example
Message-Id: <864o5v8giz.fsf@red.stonehenge.com>

>>>>> "Willem" == Willem  <willem@toad.stack.nl> writes:

Willem> Example: how to parse a simple ini-file (only 'key=value' pairs)

Willem> PS: This is just an example, so it's oversimplified.

Willem> Simple way:
Willem> my %keys;
Willem> for my $line (split "\n", $file) {
Willem>   if (my ($key, $val) = $line =~ /^(.*)=(.*)$/) {
Willem>     $keys{$key} = $val;
Willem>   }
Willem> }

That would parse a line with "a=b=c" to be key "a=b" value "c".
I would think you'd prefer that as key "a", value "b=c", which suggests
a lazy match:

  if (my ($key, $val) = $line =~ /^(.*?)=(.*)$/) {

print "Just another Perl hacker,"; # the original

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion


------------------------------

Date: Mon, 18 Apr 2011 05:24:53 -0700 (PDT)
From: Klaus <klaus03@gmail.com>
Subject: module XML::Reader
Message-Id: <47b738eb-97f5-40c4-8a8a-9ba60c3be105@x37g2000prb.googlegroups.com>

Just a small message to say that my module XML::Reader is on CPAN:
http://search.cpan.org/~keichner/XML-Reader-0.37/lib/XML/Reader.pm

It is most useful to extract XML sequentially (uses constant memory,
even with huge XML files).

Feedback is most welcome.


Here is an example from the documentation:

  use XML::Reader;

  my $rdr = XML::Reader->new(\$line2, {filter => 5},
    { root => 'customer',       branch => ['/@name', '/street', '/
city'] },
    { root => 'p',              branch => '*' },
  );

  my $out0 = '';
  my $out1 = '';

  while ($rdr->iterate) {
      if ($rdr->rx == 0) {
          my @rv = $rdr->value;
          $out0 .= sprintf "  Cust: Name = %-7s Street = %-12s City =
%s\n",
            $rv[0], $rv[1], $rv[2];
      }
      elsif ($rdr->rx == 1) {
          $out1 .= "  P: ".$rdr->value."\n";
      }
  }

  print "output0:\n$out0\n";
  print "output1:\n$out1\n";

Given the following XML structure as input:

  my $line2 = q{
  <data>
    <supplier>ggg</supplier>
    <customer name="o'rob" id="444">
      <street>pod alley</street>
      <city>no city</city>
    </customer>
    <customer1 name="troy" id="333">
      <street>one way</street>
      <city>any city</city>
    </customer1>
    <tcustomer name="nbc" id="777">
      <street>away</street>
      <city>acity</city>
    </tcustomer>
    <supplier>hhh</supplier>
    <zzz>
      <customer name='"sue"' id="111">
        <street>baker street</street>
        <city>sidney</city>
      </customer>
    </zzz>
    <order>
      <database>
        <customer name="&lt;smith&gt;" id="652">
          <street>high street</street>
          <city>boston</city>
        </customer>
        <customer name="&amp;jones" id="184">
          <street>maple street</street>
          <city>new york</city>
        </customer>
        <customer name="stewart" id="520">
          <street>  ring   road   </street>
          <city>  "'&amp;&lt;&#65;&gt;'"  </city>
        </customer>
      </database>
    </order>
    <dummy value="ttt">test</dummy>
    <supplier>iii</supplier>
    <supplier>jjj</supplier>
    <p>
      <p>b1</p>
      <p>b2</p>
    </p>
    <p>
      b3
    </p>
  </data>
  };

This is the output:

  output0:
    Cust: Name = o'rob   Street = pod alley    City = no city
    Cust: Name = "sue"   Street = baker street City = sidney
    Cust: Name = <smith> Street = high street  City = boston
    Cust: Name = &jones  Street = maple street City = new york
    Cust: Name = stewart Street = ring road    City = "'&<A>'"

  output1:
    P: <p><p>b1</p><p>b2</p></p>
    P: <p>b3</p>


------------------------------

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 3355
***************************************


home help back first fref pref prev next nref lref last post