Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Llama book appendix A ch12 - how in the world?

by zerogeek (Monk)
on May 12, 2006 at 11:38 UTC ( [id://548955]=perlquestion: print w/replies, xml ) Need Help??

zerogeek has asked for the wisdom of the Perl Monks concerning the following question:

Let me begin by saying that I have been reading through the llama book and gaining vast amounts of knowledge. The book seems so logically laid out and I have had all my questions answered. Granted, some of the exercises have caused me quite a bit of thinking, but that is all part of the learning process and I accept that.

However...
While trying to come up with a solution to q5 in ch12 (I am using the 4th ed), I was stumped trying to make the 2nd half work.
That full question reads,
"Write a program that works like mv, renaming the first command-line argument to the second command-line argument. (You don't need to handle any of the options of mv or additional arguments.) Allow for the destination to be a directory; if it is, use the same original basename in the new directory."

The 1st part was easy using rename, but I struggled with the 2nd half allowing for the directory. So, I checked the appendix for some guidance and find that the authors used File::Basename and File::Spec to solve this as well as questions 6,7, and 8.

Am I the only one that has read this book and become confused by the use of modules in a solution 3 chapters before they are introduced for the 1st time???

Please forgive the rant (it's 0430 and I'm getting tired). I'll try this again in the morning WITHOUT the use of the modules. I guess I was just surprised at this particular solution being offered by the book.

  • Comment on Llama book appendix A ch12 - how in the world?

Replies are listed 'Best First'.
Re: Llama book appendix A ch12 - how in the world?
by blazar (Canon) on May 12, 2006 at 12:27 UTC
    Am I the only one that has read this book and become confused by the use of modules in a solution 3 chapters before they are introduced for the 1st time???

    I think that the explicit purpose is to make you work out the code without using any module. Granted, it happens all the time that when we see a newbie doing so we recommend using the suitable modules instead. But it is a task that can be done reliably (at least on a fixed system) enough with regexen and other basic tools, so in the context of an exercise it may well make sense...

    I don't know the book, but I wouldn't be surprised if at a later point it would say: "do you remember when we had you doing so and so? Well, now forget about that and use File::Basename".

Re: Llama book appendix A ch12 - how in the world?
by merlyn (Sage) on May 12, 2006 at 16:33 UTC
    We did some restructuring in the book between the third and fourth edition, and it's quite possible that we used something before we defined it as a result. That's the way book writing goes sometimes... you get three authors working on things, an editor who is supposed to check for details like that, and a bunch of reviewers who are also supposed to catch it, and little things like that still slip through the cracks.

    Be sure to report this as a bug against the book. That way we can track and fix it for the next edition, if not the next printing.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Llama book appendix A ch12 - how in the world?
by kwaping (Priest) on May 12, 2006 at 15:12 UTC
    Modules are wonderful. CPAN is a godsend. We should use them whenever possible.

    With that said, modules can also be awesome learning tools for how to do it yourself, should you desire. Just look up a module on CPAN that does what you want to do and click the "Source" link. You can then view the raw guts of the module.

    This is what I would do in your situation. Sure, it's faster and easier to use those modules, but since you seem to be learning Perl, it might be of some benefit to view the source of those modules to see how they do it.

    Beware though, some of the code you'll see will blow your mind and confuse you for weeks! :)

    ---
    It's all fine and dandy until someone has to look at the code.
Re: Llama book appendix A ch12 - how in the world?
by zerogeek (Monk) on May 15, 2006 at 23:17 UTC
    OK, I sat back down on this one and figured it out:

    #!/usr/bin/perl -w use strict; ########################## # # # Coded by Scott J Ulmen # # # ########################## my $source; my $dest; ($source, $dest) = @ARGV; my $new = "$dest".'/'."$source"; if (-d $dest) { rename $source, $new; } elsif (-e $dest) { print "Sorry, $dest already exists in this dir!\n"; } else { rename $source, $dest; }

    Granted, I should add a regex to determine if the dir that I type in has a trailing "/", but it gets the job done an an exercise for the idea being presented WITHOUT A MODULE.

    I have to admit that I was a bit upset that I couldn't get pointed in the right direction, but I'm glad now that nobody posted an answer as it mad me sit down today to figure it out. (I can be stubborn at times) :)

    Apparently, my problem was simply with the ORDER of the else's. Man did that drive me crazy! To the point that I tried to rewrite it at least 4 different ways before coming back to my 1st try.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://548955]
Approved by moklevat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2024-04-18 20:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found