Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / Modules / September 2008



Tip: Looking for answers? Try searching our database.

using result of text::diff module

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SP - 28 Aug 2008 20:34 GMT
I have a script which uses the text::diff module and I cant figure out
how to use the output
I can print the result, but I need to parse the results into a array
of strings and format it.

My code is:

use Net::Telnet::Cisco;
use File::Copy;
use Text::Diff;

my $device = '1.2.3.4';

# Connect to device
my $session = Net::Telnet::Cisco->new(Host => $device );
$session->login('login','password');

# Enable mode
if ($session->enable("password"))
{
        $session->cmd('term len 0');
        @output = $session->cmd('show run');
        $session->cmd('exit');
}
else
{
        warn "Can't go into enable mode: " . $session->errmsg;
}

my $NEWCFGFILE = "D:/cfg-data/$device.new.cfg";
my $OLDCFGFILE = "D:/cfg-data/$device.old.cfg";

# Delete old config file
unlink "$OLDCFGFILE";

# Rename existing new file to old file
copy ( $NEWCFGFILE, $OLDCFGFILE );
unlink "$NEWCFGFILE";

# Save new config to file
open CURRENTFILE, ">$NEWCFGFILE";
print CURRENTFILE @output;
close CURRENTFILE;

# Test if any lines are different
my $diff_txt = diff( $NEWCFGFILE, $OLDCFGFILE, { STYLE =>
"Context" } );
print "$diff_txt";

Thanks
Mark Clements - 04 Sep 2008 06:13 GMT
> I have a script which uses the text::diff module and I cant figure out
> how to use the output
[quoted text clipped - 8 lines]
>
>   my $device = '1.2.3.4';

Hi,
Your script is missing

use strict;
use warnings;

This will help you catch potential errors before runtime. Also, you need
to *always* check the return value of system calls:

unless(unlink "$OLDCFGFILE"){
# no files deleted!!
}

unless(open CURRENTFILE, ">$NEWCFGFILE"){
# could not open file!!
die "$!";
}

It's generally recommended to use the three-argument form of open and a
lexically-scoped filehandle:
unless(open my $currentfh, ">",$NEWCFGFILE){
...
}
print $currentfh @output;

although I'd argue it's confusing writing to a new file with a
filehandle called "current".

You may like to look at Text::Patch for ways of using the output of
Text::Diff.

Mark
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.