Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re: The Gates of Perl are not newbie friendly.

by Hielo (Sexton)
on Apr 19, 2003 at 20:30 UTC ( [id://251704]=note: print w/replies, xml ) Need Help??


in reply to Re: The Gates of Perl are not newbie friendly.
in thread The Gates of Perl are not newbie friendly.

It looks like an excellent "Rank-Newbie" page, I will work my way through it tonight and see if it sheds any light on the hash issue, and if it delves into using modules at all. (two things I am stumbling over at the present).

Thank you for taking the time to point this site out.

  • Comment on Re: Re: The Gates of Perl are not newbie friendly.

Replies are listed 'Best First'.
Using modules for Newbies
by logan (Curate) on Apr 20, 2003 at 03:52 UTC
    Modules look imtimidating at first, but they're really cake. Simply load the module at run time. Wow, that doesn't sound simple at all. Here's what I mean:
    #!/usr/bin/perl -w use MP3::ID3v1Tag;
    This allows your code to access the subroutines in MP3::ID3v1Tag. From there, you call them like any other sub.
    use MP3::ID3v1Tag; # Adds the library $mp3_file = new MP3::ID3v1Tag("filename.mp3"); # Creates a new objec +t called $mp3_file. $mp3_file->print_tag(); # calls subroutine print_tag() on the file t +he $mp3_file object is working on (filename.mp3) if($mp3_file->got_tag()) { # calls subroutine got_tag(), which will +return 1 if the tag exists, 0 if not. $mp3_file->set_title($title); # calls subroutine set_title(), +which will set the title to the value $title. In normal practice, you + probably would have parsed out of the filename $save_status = $mp3_file->save(); # calls subroutine save(), c +ommitting your changes. }

    Clear as mud? Think of a module as a big bag of subroutines that you don't have to write. Just read the documentation, and you'll have all you need to know about how to use them, and all the dirty work happens behind the scenes.

    Over time I've found that a new project begins by searching CPAN for relavent modules. If you can find one, you may find that 90% of your work is already done for you.

    One more thing, as you continue coding perl, I can't stress enough how useful The Perl Cookbook will be. It's one book I wish I'd bought on my first day coding.

    -Logan
    "What do I want? I'm an American. I want more."

      I see what you are trying to get across, but it is quite muddy. I am using a simple looking module for getting SETI@Home stats (SETI-WebStats-1.02), so far this is the code I got working

      use SETI::WebStats; my $emailAddr = "wmarcy\@stny.rr.com"; my $seti = SETI::WebStats->new($emailAddr); my $unitsProcd = $seti->numResults; print "Work Units Processed = ",$unitsProcd;
      The third line is what is throwing me, what does it do? I know it is "invoking" the module some how, but I can't fathom how (how is that for a complete idiot newbie question?)

      Thanks for the help!

      Oh, and the program actually works! I am amazed. Even though I only added one line and removed a half dozen or so that I didn't need.

      On looking at my question preview mode, I am also wondering what the operator "->" is doing in the program also?

        The line starting with "my $seti" is using the idiom of Object Oriented programming. I recommend reading a tutorial on Object Oriented Perl here at the Monastery. The third one in the list was also recommended by Louis_Wu above.

        Anyway to be brief, it is a very useful way to deal with data and subroutines (or "methods") as belonging to a metaphorical object.

        In this case a new object is being created based on the WebStats object template by calling WebStats' object constructing subroutine (called "new"). The arrow means "the WebStats method called ..." and the ampersand which normally preceeds a subroutine you are calling is left out by convention. The constructor method is given an email address and returns a WebStats object, which gets put into $seti.

        $seti is now an object which knows everything a WebStats object should know, like its email address (which you told it) and how many units it has processed. It has a method (a subroutine) called "numResults" which when called returns the number of units processed.

        You will find that you can do an awful lot of things with the CPAN (www.cpan.org) and just this knowledge, but I *urge* you to read the above tutorial and some of the very good books or online sites recommended by the nice monks above. Perl.com does have a Documentation page, also perldoc.com has all the manuals that come with perl.

        But amazingly enough, as Juerd told you there is a whole book online for free! Can you imagine it! Chapter 11 is all about Object-Oriented Perl. Run, don't walk to it. We can answer individual questions but you will be able to make the most out of Perlmonks (and the CGI.pm module, and ..) if you take advantage of these wonderful resources.

Log In?
Username:
Password:

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

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

    No recent polls found