[31895] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3158 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 1 16:09:22 2010

Date: Fri, 1 Oct 2010 13:09:05 -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           Fri, 1 Oct 2010     Volume: 11 Number: 3158

Today's topics:
        ANNOUNCE - NHI1 / PLMK / libmsgque - Work-Package-II <aotto1968@users.sourceforge.net>
        eval within grep not working <schaitan@gmail.com>
    Re: eval within grep not working <uri@StemSystems.com>
    Re: Need Regex for phone number <cwilbur@chromatico.net>
    Re: Need Regex for phone number <stanley@peak.org>
    Re: Need Regex for phone number <stanley@peak.org>
    Re: Need Regex for phone number <uri@StemSystems.com>
    Re: Need Regex for phone number <cwilbur@chromatico.net>
    Re: Need Regex for phone number <tzz@lifelogs.com>
    Re: Need Regex for phone number <stanley@peak.org>
    Re: why does this happen? <merrilljensen@q.com>
    Re: why does this happen? <Huge@nowhere.much.invalid>
    Re: why does this happen? <lws4art@gmail.com>
    Re: why does this happen? <mvdwege@mail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 01 Oct 2010 12:55:41 +0200
From: Andreas Otto <aotto1968@users.sourceforge.net>
Subject: ANNOUNCE - NHI1 / PLMK / libmsgque - Work-Package-II
Message-Id: <i84eps$e3k$00$3@news.t-online.com>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

Dear User,


ANNOUNCE:    Work-Package-II
============================

  libmsgque: Application-Server-Toolkit for
             C, C++, JAVA, C#, TCL, PERL, PYTHON, VB.NET
  PLMK:      Programming-Language-Microkernel
  NHI1:      Non-Human-Intelligence #1


SUMMARY
=======

  After six month research workshop with bicycling through the
  'Black Forrest', 'Vosges', 'Switzerland' / 'French' and 'Italy'
  Alps ... I'm back to serve for "Work Package II" of NHI1


Read More At
============

  http://nhi1.berlios.de/theLink/wp2.htm


mfg

  Andreas Otto (aotto1968)
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJMpb4tAAoJEGTcPijNG3/APk8H/1xKqBf3TTcYjGwwBso5dRE1
y4m2C+LsLgSyWrBCsXFnArKmlMegQvKioYnpLS/U+SO3OfbbIz6QufClTBl1mHXN
Ca98hId41fM5v4sulquB9CD38S5PgKoHte1qacjmReRHHFj15UzxwcTIUydik9bf
ly7dApj0UaiA7ktTNxFPIJwdw9cWt7gSaEizB8MTBDkGdkcseqKhzIv3ucItgi0i
2d/ZKQenI0FppeD7LpBHpfZVjtFG//splYw9pdRu2lggttTKHfD398XPp/fASLVS
qKnPgKT63idanRyyUvkBedmBUrsJE23N8XnmgZMtIKVx6IK0q576n5Jwks3M8ho=
=6b02
-----END PGP SIGNATURE-----


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

Date: Fri, 1 Oct 2010 09:08:45 -0700 (PDT)
From: Krishna Chaitanya <schaitan@gmail.com>
Subject: eval within grep not working
Message-Id: <22eb44df-df5f-498e-990f-ebf813c7e458@s24g2000pri.googlegroups.com>

Hi,

Am trying the exercise in Chapter 17 of Learning Perl....for those who
don't have that book, here is the question:

"Make a program that reads a list of strings from a file, one string
per line, and then lets the user interactively enter patterns that may
match some of the strings. For each pattern, the program should tell
how many strings from the file matched, then which ones those were.
Don=92t reread the file for each new pattern; keep the strings in
memory. The filename may be hardcoded in the file. If a pattern is
invalid (for example, if it has unmatched parentheses), the program
should simply report that error and let the user continue trying
patterns. When the user enters a blank line instead of a pattern, the
program should quit."

I came up with this code:

-----------

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

open FH, "abc" or die "Cannot open file: $!\n";
my @file_contents =3D <FH>;

while(1) {
  chomp(my $pattern =3D <STDIN>);
  last if $pattern =3D~ /^\s*$/;
  my @matched =3D grep {
                        my $line =3D $_;
                        eval { $line =3D~ /$pattern/ };
                        if ($@) {
                          print "Error in pattern : $@\n";
                          undef;
                        }
                     } @file_contents;
  print "Number of matched lines =3D ", scalar @matched, "\n";
  print "Matched lines are:\n @matched\n";
}
-------------------

It doesn't work...for simple patterns like "This", "cat", etc. Oh, and
file "abc" contains:


This is a cat
This is a dog
This is a ball

But it works if I put the eval outside the grep (as mentioned in the
solution to the exercise in that book). I thought the code block for
grep in my code above always returns either true/false......but it's
not working. Can you pls. help me by pointing out the mistake?

Am using Perl 5.8.8 on Linux, if it matters.

Thanks,
Chaitanya


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

Date: Fri, 01 Oct 2010 12:26:36 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: eval within grep not working
Message-Id: <87wrq1nakj.fsf@quad.sysarch.com>

>>>>> "KC" == Krishna Chaitanya <schaitan@gmail.com> writes:

  KC>   my @matched = grep {
  KC>                         my $line = $_;
  KC>                         eval { $line =~ /$pattern/ };
  KC>                         if ($@) {
  KC>                           print "Error in pattern : $@\n";
  KC>                           undef;
  KC>                         }
  KC>                      } @file_contents;


your problem is simple. you don't pass the result of the match to
grep. it is using the last expression evaluated as its boolean (likely
the if()). so you can do one of two things: save the results of the eval
and pass that to grep, or eval a qr// to check the regex validity and
then do the match at the end. here are untested examples:


  my @matched = grep {
                        my $line = $_;

# that isn't needed even though i always used named vars. in this short
# context, $_ is fine
 
                       my $match = eval { $line =~ /$pattern/ };
                        if ($@) {
                          print "Error in pattern : $@\n";
# exit the grep and get another user line. this is what the exercise
# asks for
                          next ;
                        }
			$match 

                     } @file_contents;


# this version also moves the pattern verifyer outside the grep where it
# belongs. makes the grep short and easy to read as well.

	my $pat = eval { qr/$pattern/ };
	unless( $pat ) {
		print "Error in pattern : $@\n" ;
		next ;
	}

	my @matched = grep /$pat/, @file_contents;

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: Fri, 01 Oct 2010 12:29:20 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Need Regex for phone number
Message-Id: <86vd5l7u73.fsf@mithril.chromatico.net>

>>>>> "UG" == Uri Guttman <uri@StemSystems.com> writes:

    UG> i am amazed at the analness of those who think this is a bad
    UG> thing to do on an interview. wow. i interview dozens of perl
    UG> hackers a year. i may know something about this area.

I'm not amazed, because I've seen candidates make the same response.  

If the candidate says, "There are a lot of phone number formats," and I
say, "Right, so limit it to North American formats," that's a good
sign.  If the candidate then engages in 20 minutes of bafflegab aimed at
proving to me that I know very little about phone numbers because that's
the topic I chose for a toy problem, well, that gives me an incredible
amount of information that is useful in making hiring decisions.

Charlton



-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Fri, 1 Oct 2010 10:11:15 -0700
From: John Stanley <stanley@peak.org>
Subject: Re: Need Regex for phone number
Message-Id: <alpine.LRH.2.00.1010011003090.15934@shell.peak.org>

On Thu, 30 Sep 2010, Charlton Wilbur wrote:

>>>>>> "JS" == John Stanley <stanley@peak.org> writes:
>
>    JS> On Thu, 30 Sep 2010, Uri Guttman wrote:
>    >> you just blew the interview. NEXT!!
>
>    JS> Nope. As soon as this interviewer told me I failed his test
>    JS> because I didn't include code to deal with a specification he
>    JS> had explicitely REMOVED from the question, he failed the
>    JS> interview.
>
> The original spec:
>
>    >> We have a text-input field that we ask the user to type a
>    >> telephone number into.  As part of data validation, we need to
>    >> know if it's a valid phone number and to identify the area code.
>    >> Please write some code to do that.
>
> The instruction was "Substitute well-formed for valid."   The result:
>
>    >> We have a text-input field that we ask the user to type a
>    >> telephone number into.  As part of data validation, we need to
>    >> know if it's a well-formed phone number and to identify the area
>    >> code.  Please write some code to do that.

Yes. As I said, YOU explicitely removed the requirement to validate the 
area code and replaced it with a nebulous "well-formed". Then you claimed 
I failed your test because I didn't validate the area code, even though I 
clearly "identified" it. Changed the spec in mid-test and then graded on 
the original you did. Not very ethical.

As for "well-formed", it requires a knowledge of the standard for phone 
numbers to know what "well-formed" really is. At a minimum, a 
"well-formed" phone number starts with a '+' to indicate "dial the 
international access code for your system, if required", and, in the US, 
'1' for the country code. So, if the user doesn't type in "+1...", he's 
not entering a "well-formed" phone number for the United States.

Now, if you want to reject any number that isn't "well-formed", that's 
your right, but I doubt that many of your customers will be entering "+1". 
They MIGHT enter '1' as the "long distance" code, but rarely will they do 
the full deal. In any case, knowing what "well-formed" is requires 
knowledge that you haven't provided and isn't general knowledge.

> If you're going to play nit-picky semantic games, you need to play them
> a whole lot better than that.

No, I think I made my point. You change the rules in mid game and then 
punt when confronted about it.


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

Date: Fri, 1 Oct 2010 10:14:06 -0700
From: John Stanley <stanley@peak.org>
Subject: Re: Need Regex for phone number
Message-Id: <alpine.LRH.2.00.1010011011580.15934@shell.peak.org>

On Thu, 30 Sep 2010, Uri Guttman wrote:

>>>>>> "JS" == John Stanley <stanley@peak.org> writes:
>
>  JS> Nope. As soon as this interviewer told me I failed his test because I
>  JS> didn't include code to deal with a specification he had explicitely
>  JS> REMOVED from the question, he failed the interview. You can't hire/not
>  JS> hire someone who has decided they won't work for you in the first
>  JS> place.
>
> you failed again. i don't hire people.

You failed. I wasn't talking about you, I said "this interviewer". That's 
Charlton. Topic sentence. It means something.

> i place them. i also screen based
> on communication skills. you fail there too.

Yeah. You can't figure out who "this interviewer" refers to and that means 
I failed. Sure.

> you don't get it. your loss.

I don't get a job I wasn't applying for. I'll live.



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

Date: Fri, 01 Oct 2010 13:48:53 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Need Regex for phone number
Message-Id: <87d3rtn6re.fsf@quad.sysarch.com>

>>>>> "JS" == John Stanley <stanley@peak.org> writes:

  JS> On Thu, 30 Sep 2010, Uri Guttman wrote:
  >>>>>>> "JS" == John Stanley <stanley@peak.org> writes:
  >> 
  JS> Nope. As soon as this interviewer told me I failed his test because I
  JS> didn't include code to deal with a specification he had explicitely
  JS> REMOVED from the question, he failed the interview. You can't hire/not
  JS> hire someone who has decided they won't work for you in the first
  JS> place.
  >> 
  >> you failed again. i don't hire people.

  JS> You failed. I wasn't talking about you, I said "this
  JS> interviewer". That's Charlton. Topic sentence. It means something.

  >> i place them. i also screen based
  >> on communication skills. you fail there too.

  JS> Yeah. You can't figure out who "this interviewer" refers to and that
  JS> means I failed. Sure.

no, i failed you because of your attitude about being interviewed. and
as i place people for my business i have some experience in this
matter. you on the other hand don't.

  JS> I don't get a job I wasn't applying for. I'll live.

or other jobs you would want to get. your loss. their gain.

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: Fri, 01 Oct 2010 14:54:50 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Need Regex for phone number
Message-Id: <86lj6h7ngl.fsf@mithril.chromatico.net>

>>>>> "UG" == Uri Guttman <uri@StemSystems.com> writes:

    UG> or other jobs you would want to get. your loss. their gain.

Indeed.  I'd love to see Mr. Stanley's response to some of the requests
I've gotten from our sysadmins or our marketing department.

I'm sure they'd relish being yammered at because they didn't use a
particular term in the idiosyncratic sense he prefers, or because they
used a different term in a clarification and he can't find it in himself
to reconcile the two.

Charlton



-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Fri, 01 Oct 2010 14:39:02 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Need Regex for phone number
Message-Id: <87vd5lityh.fsf@lifelogs.com>

My personal favorite interview (as an applicant) was at ITA software.
"Sit down, here's a computer.  Write a program in 30 minutes to do X.
We'll come back."

My least favorite (as an applicant, again) was with Google:
Him: "What are the 7 OSI layers?"
Me: "You're kidding, right?" (thinking "it's not 1993 again, is it?")

It went downhill from there...

Ted


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

Date: Fri, 1 Oct 2010 13:02:04 -0700
From: John Stanley <stanley@peak.org>
Subject: Re: Need Regex for phone number
Message-Id: <alpine.LRH.2.00.1010011244440.21404@shell.peak.org>

On Fri, 1 Oct 2010, Uri Guttman wrote:

>>>>>> "JS" == John Stanley <stanley@peak.org> writes:
>
> no, i failed you because of your attitude about being interviewed.

No, you didn't fail me because you weren't the interviewer. You played no 
part in the interview. And he didn't fail me because of my attitude, he
failed me because I didn't meet a spec for the code that he had already 
voided. I didn't validate the area code.

>  JS> I don't get a job I wasn't applying for. I'll live.
>
> or other jobs you would want to get.

You still don't get it. "Other jobs" means that this was a job I wanted to 
get. I don't. Not from the beginning, and certainly not after finding out 
my potential employer was incapable of keeping track of the specifications 
he himself wrote and changed. Not after he copped an attitude when this 
change to the rules in mid-stream was pointed out, and when questioned 
about the requirements of what he really wanted. And certainly not after 
being evaluated on requirements that were explicitely removed from the 
task.

I would have no reason to expect he'd not play the same game when it came 
time for a performance review. "Your code didn't X." "YOU said it didn't 
have to X." "Well, I wanted it to X and you should have known that." "We 
discussed whether X was feasible and practical and YOU AGREED that it was 
not so it was removed from the spec." "You should have done it anyway."

If you think that kind of game is fair and productive, more power to you.
Like I said, interviews are a two-edged sword. Treat the interviewee like 
crap, don't expect him to respond well to a job offer.

> your loss. their gain.

No, my gain, and they lose a patsy for a fun but completely unproductive 
game of "specification, specification, who's got the current 
specification?" Yes, it is fun to do that to someone. It's a power trip, 
but ultimately ineffective as a management style. Maybe it is their gain 
after all since they'd just have to go through the process all over again 
when I walked away from that kind of nonsense. Life's too short.





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

Date: Fri, 01 Oct 2010 01:38:40 -0600
From: Uno <merrilljensen@q.com>
Subject: Re: why does this happen?
Message-Id: <8glhg1Fj46U1@mid.individual.net>

Tad McClellan wrote:
> ["Followup-To:" header set to comp.lang.perl.misc.]
> 
> Jürgen Exner <jurgenex@hotmail.com> wrote:
>> Uno <merrilljensen@q.com> wrote:
> 
>>> Nuts, jue, I have every belief that it wasn't perl.  
> 
> 
> Then you should not have posted it to a Perl newsgroup!
> 
> 
>>> Maybe you can help 
>>> me with this while the other subthread considers the part that is 
>>> germane to perl.
> 
> 
> There is no part that is germane to Perl (nor to perl).
> 
> 
>>> So I rename rm1.f90 to rm2.f90, and I have no problem on the command 
>>> line except to iterate the integer that postpends the source file.
>> What do you mean by "iterate the integer that postpends the source
>> file"? And what's a f90 file?
> 
> 
> I expect that he did something like:
> 
>     rename 1 2 rm1.f90
> 
> Though he never said what he did, which is pretty silly, as what
> he did is germane to what ended up happening...
> 
> 

I haven't gotten to the perl part yet due to technical difficulties.  It 
is the other subthread that is yet to happen.

Uno
-- 

I'm sorry for your preventable loss.

Rest In Peace:
Jonah McClellan gave his life for his country in a
helicopter crash in Afghanistan on September 21,2010.
Please pray for his wife and three children.

Lutherans do every week they show up.


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

Date: 1 Oct 2010 09:23:02 GMT
From: Huge <Huge@nowhere.much.invalid>
Subject: Re: why does this happen?
Message-Id: <8glnjmFifvU1@mid.individual.net>

On 2010-10-01, Uno <merrilljensen@q.com> wrote:

>> teco if you can get it? :)

I have it for Ubuntu. Can't recall where I got it. Or how to drive
it any more.

>> 
>> uri
>> 
>
>
> the rat was that I used mv and confounded the "usual" order of things.
>
> what is teco?

God's editor of choice;

http://en.wikipedia.org/wiki/Text_Editor_and_Corrector


-- 
Today is Prickle-Prickle, the 55th day of Bureaucracy in the YOLD 3176
     Open are the double doors of the horizon, unlock'd are its bolts


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

Date: Fri, 01 Oct 2010 10:33:05 -0400
From: "Jonathan N. Little" <lws4art@gmail.com>
Subject: Re: why does this happen?
Message-Id: <i84rev$ntu$1@news.eternal-september.org>

Uno wrote:
> Mart van de Wege wrote:
>
>> Follow-up to -9999.
>
> Well I hope I haven't stepped on a landmine. Thanks all for replies.
> There seems to be some smoke from other fires, so I wanted to begin with
> getting a handle on the bone-headed stuff:
>
> What I typically do when I begin to code for a utility like the one I
> desire is that I start with some more or less standard template, make it
> compile and perform--for perl these steps are less discrete, and see if
> I agree with my output.
>
> So far on ubuntu, I have only used gedit for writing source. When I've
> got the template working, I iterate the number that postpends in the
> filename. I couldn't say that any more clearly with a microphone.
>
> So when I rename perl2.pl to perl3.pl, this is what I get:
>
> $ ls -l perl*
> -rwx------ 1 dan dan 405 2010-04-01 12:37 perl1.pl
> -rw------- 1 dan dan 269 2010-04-01 12:30 perl1.pl~
> -rwxr-xr-x 1 dan dan 580 2010-09-25 21:47 perl2.pl
> -rw-r--r-- 1 dan dan 527 2010-09-25 19:20 perl2.pl~
> -rw-r--r-- 1 dan dan 580 2010-09-30 21:40 perl3.pl
> $

Okay as others have asked *how are you "renaming" your script?*

If you use cp or mv the permissions remain. Example I created a small 
shell script and made it executable:

$ ls -lha
total 12K
drwxr-xr-x  2 jonathan jonathan 4.0K 2010-10-01 10:08 .
drwxr-xr-x 68 jonathan jonathan 4.0K 2010-10-01 10:08 ..
-rwxr-xr-x  1 jonathan jonathan   25 2010-10-01 10:08 ascript

Now I copy "ascript" to "ascriptbycp" using cp, same result if you mv to 
rename:

$ cp ascript ascriptbycp

$ ls -lha
total 16K
drwxr-xr-x  2 jonathan jonathan 4.0K 2010-10-01 10:09 .
drwxr-xr-x 68 jonathan jonathan 4.0K 2010-10-01 10:08 ..
-rwxr-xr-x  1 jonathan jonathan   25 2010-10-01 10:08 ascript
-rwxr-xr-x  1 jonathan jonathan   25 2010-10-01 10:09 ascriptbycp

Permissions remain, but that is *not* how you renamed the script did 
you. You used a text editor and to give it a new new name use used the 
editor's "Save As" function. Well of course you will loose the 
executable attribute! A perl script like a shell script is just a text 
file. When you use an editor's "Save As" function you are not renaming 
the script, but making a *new* text file. Text files by default are not 
supposed to be executable. Confirmed in my example where I open 
"ascript" in a text editor and saved as "ascriptbysaveas" see it is just 
a plain text file:

$ ls -lha
total 20K
drwxr-xr-x  2 jonathan jonathan 4.0K 2010-10-01 10:10 .
drwxr-xr-x 68 jonathan jonathan 4.0K 2010-10-01 10:10 ..
-rwxr-xr-x  1 jonathan jonathan   25 2010-10-01 10:08 ascript
-rwxr-xr-x  1 jonathan jonathan   25 2010-10-01 10:09 ascriptbycp
-rw-r--r--  1 jonathan jonathan   25 2010-10-01 10:10 ascriptbysaveas

No mystery here.

-- 
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com


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

Date: Fri, 01 Oct 2010 18:20:34 +0200
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: why does this happen?
Message-Id: <86tyl5x4tp.fsf@gareth.avalon.lan>

Uno <merrilljensen@q.com> writes:

> Mart van de Wege wrote:
>
>> Follow-up to -9999.
>
> Well I hope I haven't stepped on a landmine.  

Well, you not particularly. It would have been helpful if you had
answered the first query on *how* you 'iterated' your filenames
(apparently by saving under a new name from your editor), but aside from
that I won't fault you.

If people start throwing out advice which a) is not correct, and b) not
germane to your question, and c) posted by a known troll, then *those*
people get flamed.

Mart

-- 
"We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.


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

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


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