in reply to Re^2: switch and case statement using a function
in thread switch and case statement using a function

The attached program code has two input params. Vendor name and Technology name. Now I am not able to access the parameter $vendor and $tech inside the sub functions like sub aln_g(). What I need to do for that. Where I need to redeclare the arguments. I am not able to read the two arguments. Also I need to pass one more argument i.e. date in YYYYMMDD format to this program. How can I make the changes for that so that the new argument is also accessible inside sub aln_g()

#!/usr/bin/perl -w package synchronizer; use strict; use warnings; use Nexius::CM::Utils; use Getopt::Long; use Data::Dumper; use Switch; #use File::stat; #use Time::localtime; use POSIX; BEGIN { CMlog( 'info', 'Started' ); } my %month = qw( jan 01 feb 02 mar 03 apr 04 may 50 jun 06 jul 07 aug 08 sep 09 oct 10 nov 11 dec 12 ); sub main { # my ($gen_key, $host, $user, $pass, $sync, $config); my ($vendor, $tech, $help); Getopt::Long::GetOptions( # "generate_key" => \$gen_key +, # "sync" => \$sync, # "server:s" => \$host, # "user:s" => \$user, "help" => \$help, "vendor:s" => \$vendor, "tech:s" => \$tech ); if ((defined $help) || !(defined $vendor and defined $tech)) { print Dumper $0; print "Run: $0 --vendor=\"VENDOR_NAME\" --tech=\"TECHNOLOGY_NA +ME\"\n"; print "Vendor names: AlcatelLucent, Ericsson, IPAccess, Nokia, + Nortel\n"; print "Technologies: Gsm, Umts\n"; } else { switch (lc $tech) { case "gsm" { switch (lc $vendor){ case "alcatellucent" {aln_g()} case "ericsson" {eri_g()} case "ipaccess" {ipa_g()} case "nokia" {nok_g()} case "nortel" {nor_g()} else { print "The checker for $ +vendor/$tech has not been implemented.\n Try $0 --help for further in +formation"} } } case "umts" { switch (lc $vendor){ case "ericsson" {eri_u()} case "nokia" {nok_u()} else { print "The checker for $ +vendor/$tech has not been implemented.\n Try $0 --help for further in +formation" } } } else { print "The checker for $vendor/$tech has not been imp +lemented.\n Try $0 --help for further information" } } } } sub read_file { my $my_file = shift; open(FH,"< $my_file"); my @my_content = <FH>; return @my_content; } sub get_files { my ($path, $fxn_pointer_) = @_; opendir (DIR, $path) or die "Unable to open $path: $!"; my @files = grep { !/^\.{1,2}$/ } readdir (DIR); closedir (DIR); @files = map { $path . '/' . $_ } @files; for (@files) { if (-d $_) { get_files ($_, $fxn_pointer_); } else { $fxn_pointer_->( $_) } } } sub aln_g { my $date=0; get_files("$ENV{'XCONFIG_VAR_DIR'}/oss/alcatellucent-nss/gsm", sub{ my $my_file = shift; my @my_content = read_file($my_file); my $latest=0; foreach my $line (@my_content) { if ($line =~ m/dumped\s*\w\w\w\s*(\w\w\w)\s*(\ +d)\s*\d\d:\d\d:\d\d\s*(\d\d\d\d)/) { $latest = $3.$month{lc($1)}.sprintf("%02d" +, $2) if ($latest < $3.$month{lc($1)}.sprintf("%02d", $2)); } } $date = $latest if($latest && $date < $latest); } ); $date =~ /(\d\d\d\d)(\d\d)(\d\d)/; print "Latest update for AlcatelLucent NSS is: $2/$3/$1\n"; } sub eri_g { my $date=0; get_files("$ENV{'XCONFIG_VAR_DIR'}/oss/ericsson/gsm", sub{ my $my_file = shift; my $latest = POSIX::strftime( "%Y%m%d", localtime( ( stat $my_file )[9] ) ); $date = $latest if($latest && $date < $latest); } ); $date =~ /(\d\d\d\d)(\d\d)(\d\d)/; print "Latest update for Ericsson GSM is: $2/$3/$1\n"; } sub ipa_g { my $date=0; get_files("$ENV{'XCONFIG_VAR_DIR'}/oss/ip-access/gsm", sub{ my $my_file = shift; my @my_content = read_file($my_file); my $latest=0; if ($my_content[-1] =~ m/OK END/ && $my_content[-2 +] =~ m/(\d\d)(\d\d)(\d\d)/) { $latest="20$1$2$3" } $date = $latest if($latest && $date < $latest); } ); $date =~ /(\d\d\d\d)(\d\d)(\d\d)/; print "Latest update for IP-Access is: $2/$3/$1\n"; } sub nok_g { my $date=0; get_files("$ENV{'XCONFIG_VAR_DIR'}/oss/nokia/gsm", sub{ my $my_file = shift; my @my_content = read_file($my_file); my $latest=0; foreach my $line (@my_content) { if ($line =~ m/\|"(\d\d)-(\d\d)-(\d\d\d\d) \d\ +d:\d\d:\d\d"\|/) { $latest = $3.$1.$2 if ($latest < $3.$1.$2) +; } } $date = $latest if($latest && $date < $latest); } ); $date =~ /(\d\d\d\d)(\d\d)(\d\d)/; print "Latest update for Nokia GSM is: $2/$3/$1\n"; } What do you mean by "just fails"? Your code worked perfectly for me.$ perl -MPOSIX -e 'print POSIX::strftime("%d-%m-%Y %H:%M:%S", localtime) +,"\n"' sub nor_g { my $date=0; get_files("$ENV{'XCONFIG_VAR_DIR'}/oss/nortel/gsm", sub{ my $my_file = shift; my $latest = POSIX::strftime( "%Y%m%d", localtime( ( stat $my_file )[9] ) ); $date = $latest if($latest && $date < $latest); } ); $date =~ /(\d\d\d\d)(\d\d)(\d\d)/; print "Latest update for Nortel GSM is: $2/$3/$1\n"; } sub eri_u { my $date=0; get_files("$ENV{'XCONFIG_VAR_DIR'}/oss/ericsson/umts", sub{ my $my_file = shift; my @my_content = read_file($my_file); my $latest=0; if ($my_content[-2] =~ m/<fileFooter dateTime="(\d +\d\d\d)-(\d\d)-(\d\d)T/) { $latest="$1$2$3" } $date = $latest if($latest && $date < $latest); } ); $date =~ /(\d\d\d\d)(\d\d)(\d\d)/; print "Latest update for Ericsson UMTS is: $2/$3/$1\n"; } sub nok_u { my $date=0; get_files("$ENV{'XCONFIG_VAR_DIR'}/oss/nokia/umts", sub{ my $my_file = shift; my @my_content = read_file($my_file); my $latest=0; foreach my $line (@my_content) { if ($line =~ m/\|"(\d\d)-(\d\d)-(\d\d\d\d) \d\ +d:\d\d:\d\d"\|/) { $latest = $3.$1.$2 if ($latest < $3.$1.$2) +; } } $date = $latest if($latest && $date < $latest); } ); $date =~ /(\d\d\d\d)(\d\d)(\d\d)/; print "Latest update for Nokia UMTS is: $2/$3/$1\n"; } main; 0;
Kindly help me out. The other switch question I have raised for this +only.

Replies are listed 'Best First'.
Re: prob: adding arg
by ww (Archbishop) on Aug 18, 2011 at 15:27 UTC
    Did you ignore moritz advice above?
    "please don't use Switch;, it can go horribly wrong, and is deprecated in favor of given/when, which are available starting from perl 5.10.0 (current old-old release)."

    Perhaps you're using an even more outdated (superceded?) Perl than 5.10? If so, I strongly suggest you upgrade.

    But perhaps you're working for an employer who hasn't or won't upgrade. If so, pay attention to the other problems in the code you initially posted.

    Otherwise, some of us may conclude that you're asking us to do -- for free -- the work for which you're receiving payment. I'm pretty sure you don't want to have us believe that.

file handling
by ashish_sun123 (Initiate) on Aug 18, 2011 at 23:53 UTC

    Hi Monks, I have one file which contains a header and rest details. Now I have to read the file header. There are around 30 fields in the header and each separated by "|". There will be a fixed pattern in the header "LASTUPDATE" , I need to find out the position of this in the header. Now after taking the position of this fixed pattern , I need to read the subsequent second line from the file and take out the date field which will be in the same position. My question is that in file reading if I am reading the file from the very first line can I refer the first line as line[0] and then the second line as line1 I will read the first line and assign it to an array and then I will match the pattern and take the position of that element. Then I will read the second line as line1 and then again assign it to a new array and then read the position as got from first line. Kindly advice