Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Syntactic Confectionery Delight
 
PerlMonks  

help please

by Anonymous Monk
on Mar 25, 2002 at 13:55 UTC ( #154088=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello there I am a student in desparate need of how to actually do this perl assignment I have to do by the 12th april.

Can you give me any tips as to what I have to write in order to succeed I doing this as I am still trying to pick this language up along with all the other ones as I am studying Web Design.

Help would be appricated
thanks simon
simonp5@yahoo.com

#!/usr/bin/perl # assignq1 # Q1 You are to write a script (or suite of scripts) that will perform + the following # administrative tasks. # The script is to take as an argument a directory. It is to search th +e directory's files # and perform the following. # A) htm files are to be renamed html. # B) any references to http within an htm/html file are to be changed +to https. # C) any html tags within html files are to be converted to upper case # (you may assume that <>s are used exclusively to identify html tags) +. # D) record in a log file each file that has been modified. # Additional tasks for additional credit # Your script is to work recursively through any sub-directories # Your script will not convert to upper case any SRC attributes of + a tag # Your log file will record each tag converted by your program # Data validation (really this should be part of the main spec!!!! +) // This is what I have started to do but is wrong I think??? print "Enter in only a directory name" $dirname = <>; chomp $dirname; if ( -d $dirname) { print "$dirname is a directory thankyou \n"; opendir(DIR, $dirname); @filenames = readdir(DIR); print"Directory is a $dirname \n"; foreach $filename(@filenames) { chomp ($filename); if (-d $dirname

Edit Petruchio Mon Mar 25 14:27:36 UTC 2002 - Added markup.

Comment on help please
Download Code
Re: help please
by Snuggle (Friar) on Mar 25, 2002 at 14:17 UTC
    The purpose of this site is not to do your homework for you. If you have a specific problem with some code that you have been working on then there may be some help for you here. All the things that you are trying to do have been done before and are pretty straight forward. Search this site for examples before you go asking vauge questions like this one.

    Also, when including code, please use the code html tags and other tips found in Writeup Formatting tips
    Anyway, no drug, not even alcohol, causes the fundamental ills of society. If we're looking for the source of our troubles, we shouldn't test people for drugs, we should test them for stupidity, ignorance, greed and love of power.

    --P. J. O'Rourke
Re: help please
by ropey (Hermit) on Mar 25, 2002 at 14:29 UTC
    I think where your going wrong is lack of planning, whenever I start a new project script I try and identify what I actually want to achieve. Try breaking it down into much smaller blocks and work from there. Address each issue in turn and you will find it pulls itself together. A good starting place is to write whatever sub procedures you may need first and add comments, don't even attempt to start coding before you have done this. For your case I would do something like this.
    sub input { # Need to receive the input from the user # and perform } sub scan { # Going to receive a directory as a a parameter # Look through each file or directory,and perform # Specific action, if directory call myself with the # path to the directory } sub rename_html { # Receive filename (with path) to be changed # Change the name of the file and return } sub convert_tags { # Receive the filemname( with path) to be converted # Open the file and using a regex will replace http with # Https } sub log { # When we change a file going to keep a record of what is to be # done, recieve filename and a code dependent on what was done. # Write to the log }
    Most importantly don't get yourself into a panic just do take one small step at a time. HTH
      It is amazing how simple things go when this method is attempted. I often find myself scratching my head. I then start to write code much like you in an essay form: explaining what I need to do... Then I break each sentance into a task to be completed.

      I hope young grasshoper reads your post--it is essential to any programming or scripting: valuable life skill!!!!

      Very good post

Re: help please
by demerphq (Chancellor) on Mar 25, 2002 at 14:46 UTC
    Despite agreeing with Snuggle regarding Homework you have at least been honest (most arent) and have made an attempt (even if it is a partial attempt.) So heres some thoughts...

    • The script is to take as an argument a directory. It is to search the directory's files and perform the following.
      This sounds much like what File::Find takes as a parameter and what it is used for
    • htm files are to be renamed html.
      Perhaps the rename keyword would be useful, consult perlfunc.
    • any references to http within an htm/html file are to be changed to https.
      Personally I would do this with HTML::TreeBuilder or alternately with HTML::TokeParser theres an excellent tutorial on using the later here<super>(fixed link sorry.)</super>
    • any html tags within html files are to be converted to upper case
      Sounds like learning one of the HTML modules really could pay off...
    • (you may assume that <>s are used exclusively to identify html tags)
      this may be true for your assignment but is patently and absolutely not true of real life. Impress your teacher use one of the HTML modules
    • record in a log file each file that has been modified.
      Might want to look up print open and close in perlfunc as well.
    • Additional tasks for additional credit
      • Your script is to work recursively through any sub-directories.
        Yup, the time you took to learn File::Find is gunna score you bunch of bonus points on this one boy...
      • Your script will not convert to upper case any SRC attributes of a tag.
        Strange how learning one of the HTML modules will pay off in the long run isnt it?
      • Your log file will record each tag converted by your program.
        Hmm, youll have lots of time to figure out the best way to do this because of the well spent time you put into learning the HTML modules.
      • Data validation (really this should be part of the main spec!!!!)
        Hmm, aftering mastering a couple of modules validating a path (the only user input) shouldn't be any trouble at all!

    Ok its a little tongue in cheek, but I think you get the idea... ;-)

    Good luck! And dont forget to use strict and warnings, to have a read through perlfunc, and perhaps perlopentut

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.
      htm files are to be renamed html.
      Perhaps the rename keyword would be useful, consult perlfunc.

      It is better to use move from File::Copy. It is guaranteed to work across filesystem boundaries.

      --
      Ilya Martynov (http://martynov.org/)

(jeffa) Re: help please
by jeffa (Chancellor) on Mar 25, 2002 at 14:57 UTC
    Hello. While we do appreciate your 'business' here, i have to recommend that you instead talk with your professor/instructor. That is one of the reasons they are there. I know from experience that unless the instructor is just plain mean, talking to them on a regular basis can make the difference for borderline pass/fail students.

    Now, for everyone else - i am the only one that got a little disturbed by this requirement?

    C) any html tags within html files are to be converted to upper case

    I took me 3 years to convince myself that lower case was better ...Check out this little snippet from http://www.w3schools.com/html/html_elements.asp:

    If you want to prepare yourself for the next generations of HTML you should start using lowercase tags. The World Wide Web Consortium (W3C) recommends lowercase tags in their HTML 4 recommendation, and XHTML (the next generation HTML) demands lowercase tags.

    Don't tell your instructor i said this though ... they might agree with you and substitute a harder requirement. ;) (although lowercasing the tags would be better)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: help please
by thunders (Priest) on Mar 25, 2002 at 17:19 UTC
    first problem is that the code you posted does not really begin to address the problem specified. Please make an effort to show that you tried to convert .htm to .html and fufill the other requirements. Your example appears to check if files are directories, but there are a few errors in that code, as this task should require recursion, or preferably use a File:: module.
Re: help please
by sdyates (Scribe) on Mar 25, 2002 at 19:34 UTC
    I too agree that we cannot do your homework for you.

    An excellent tool for getting started with perl is: Perl for Dummies--They spell everything out in this book.

    Did you try searching this site on commands such as dir(). Many people have snippets of code where they read directories. Also use google to search on specific perl commands. They are many examples out there.

    Once you have a good idea, come on back and ask a more specific question. We want to help, but want to know that you really are trying to understand the language: you task is not a difficult one.

      An even better book.

      Beginning Perl by Simon Cozens

      It covers many areas and will give you a decent feel for perl.

Re: help please
by Revelation (Deacon) on Mar 26, 2002 at 01:58 UTC
    The readdir() function will return ., and .. as directories, in addition to the other directories on under that directory.. I'm not sure you want to use that, as you may end up coding a reverse recursive function, until you have done this to every directory you have access to. To fix this you may wish to do something as simple as greping to check if your result is actually there.

    Also: Instead of -d $dirname, it may be simpler to just die, if the directory can't be opened?
    opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
    Or you can write a function to nicely die, and output the reason for it to the user. Just remember to exit;, with that function.

    so: instead of
    if ( -d $dirname) { print "$dirname is a directory thankyou \n"; opendir(DIR, $dirname); @filenames = readdir(DIR); blah blah blah blah blah
    You Could Use:
    opendir(DIR, $dirname) || die "can't opendir $dirname: $!"; my @directory = readdir(DIR); my @filenames = grep { -f "$dirname/$_" } @directory; # Either use th +e array @directory, or rewinddir(DIR) here, and grep readdir(DIR) twi +ce. my @directories = grep {-d "$some_dir/$_" && !/^\.$/ && !/^\..$/} @di +rectory; closedir DIR;

    And you'd have a list of everything in the directory, without . and .., and with diff arrays for directories and files. In fact, since you're only editing certain types of files, you could change the first grep to suit your needs, with a regex, or you could add a few greps for the differant types of files, which would let you change shtml files to html files rather easily. If you want to have only two greps, then use && regex.

    It's also my opinion that your teacher will not be very happy if you use a ton of perl modules, as the point of this excersize seems to be to test your knowledge of perl, not the writer of a perl modules' knowledge. Using a perl module is like using somebody else code, which is alright in the GNU/perl world, but is not alright in school. I would advise hard coding much of it, and adding comments to show your teacher that you know perl modules.
    Gyan Kapur
    gyan.kapur@rhhllp.com
Re: help please
by MAXOMENOS (Scribe) on Mar 26, 2002 at 03:23 UTC

    Once again, I won't do your homework for you. However, you'll probably do well to consult the following books to figure out how to get the job done:

    • Learning Perl
    • Perl Cookbook
    • Advanced Perl Programming

    I would not recommend Programming Perl if all you're trying to do is get things done, fast.

    With these three books, and a little bit of programming background, you can probably figure out how to get this done in a few days.

    I definitely do not recommend putting this off until the last minute. It's much easier to solve problems when one has more time and is more releaxed.

    I would recommend running your script against some very short tests just to make sure that all the individual requirements get done.

    Finally, good luck, and don't do this again.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-04-20 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls