Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Any Simple Examples of File::Tasks::Edit

by thunders (Priest)
on Dec 28, 2011 at 19:16 UTC ( [id://945401]=note: print w/replies, xml ) Need Help??


in reply to Any Simple Examples of File::Tasks::Edit

Not sure that's really the module you want to use if all you want to do is modify a single existing file. I've never used File::Tasks, but based on the documentation, it's probably overly complicated for what you are talking about, and all the edit operation does is overwrite the entire file. If all you need to do is append to the hosts file IO::File is the standard way. If you need random access to the lines in your hosts file, Tie::File is a good choice.

Is there a particular reason you want to use File::Tasks::Edit over a more typical solution?

  • Comment on Re: Any Simple Examples of File::Tasks::Edit

Replies are listed 'Best First'.
Re^2: Any Simple Examples of File::Tasks::Edit
by Preston@Large (Initiate) on Dec 30, 2011 at 18:26 UTC
    Not really. It just seemed like a logic entry point to start with. And yes, If I don't find the given entry in the file I the script will then append to the bottom or by some marker within the file.
    #!/usr/bin/perl ###################################################################### ##################################################################### #################################################################### ################################################################### # Set Variable file modification #**************************************************************** $datefile = `date '+%m%d%y'`; # Copy current /etc/hosts file to the work directory (/etc/work/hostsf +ile) #************************************************************* # Search and file section for hosts file entry #Main Program format STDOUT = @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> +> "********************************************************************* +" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "*******************************************************************" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "****************************************************************" @||||||||||||||||||||||||||||||||||||||| "ETC Hosts File Inplace Edit Program" @||||||||||||||||||||||||||||||||||||||||||||||||||||| "Inplace Edit (Work Directory: /etc/work/hostfiles)" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "****************************************************************" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "*******************************************************************" @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> +>> "********************************************************************* +*" . write; ########################### Main Program Section ##################### +### print "\n"; print "This program is will copy the current etc/hosts file to the \n" +; print "working directory (/etc/work/hostfile) to perform the \"Inplace +\,\" \n"; print "edit of the new or modified directories. \n"; print "\n"; print "Should I continue with the process, or exit for another time? ( +Yes|No): "; $choice1 = <STDIN>; chomp($choice1); while ($choice1 =~ m/No|NO|no|N|n/) { print "Now exiting the program. \n"; print "Please wait . . . .\n"; `sleep 5`; exit; } print "Please enter the following the following information . . .\n"; print "New System IP Address (Example - 10.12.13.14): "; $NewIP = <STDIN>; chomp($NewIP); print "Absolute name of system or device (Example - neptune.itdo.pri): + "; $NewLgName = <STDIN>; chomp($NewLgName); print "Short name of system or device (Example - neptune): "; $NewStName = <STDIN>; chomp($NewStName); ######################## Array ####################################### +## @input = ($NewIP, $NewLgName, $NewStName); ######################## Sub Routine Call Section #################### +### &copyFile; &editPlace(*input); ########################### Sub Routine Section ###################### +### sub copyFile { `cp /etc/hosts /etc/work/hostfiles/hosts`; $TargetDir = "/etc/work/hostfiles"; $TargetFile = "/etc/work/hostfiles/hosts"; `cp $TargetFile /etc/work/hosts.workcopy`; `chmod 777 /etc/work/hosts.workcopy`; `mv $TargetFile $TargetFile.orig.$datefile`; } sub editPlace { #$datefile = `date '+%m%d%y'`; local(*thisIP) = @_; $WorkCopy = "/etc/work/hosts.workcopy"; open(HOSTS, $WorkCopy) || die "Can't open hosts.workcopy $!\n" +; LINE: while(<HOSTS>) { next LINE if /^#/; next LINE if ! /$thisIP[0]/; ($IP, $longhostname, $shorthostname, $devicealias)=split(" +"); print "$IP\t $longhostname\n"; } close(HOSTS); } It's not done, but you can see the idea arising.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-25 03:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found