Perl Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Help
# 1  
Old 08-17-2010
Perl Help

Hi All,

I have the following script and I need to add more functionality to it -
Code:
#!/usr/bin/perl
use strict;
use warnings;
my $res;
my @names;
while ( $res = <DATA> ) {
push @names, ( $res =~ m{^".+apps.*"\s*=\s*"(.+)"} );
}
print "$_\n" for @names;
__DATA__
{
"_exp" = "81652254";
"apps" = {
"AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
"StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
"ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
"CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
"SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
};
}

1)Append the uri cgi-bin/jboss/apps.joa/health?method=checkurl to each of the urls (from 1)

2) print/echo appname ( e.g. SerialNberapps )and again run LWP(wget,curl) against each url from 2.

Can you help in modifying the existing script so it does the above two?

Thanks,
Jacki.

Last edited by jim mcnamara; 08-17-2010 at 05:35 PM.. Reason: code tags please
# 2  
Old 08-17-2010
Quote:
Originally Posted by jacki
...
1)Append the uri cgi-bin/jboss/apps.joa/health?method=checkurl to each of the urls (from 1)

2) print/echo appname ( e.g. SerialNberapps )and again run LWP(wget,curl) against each url from 2.
...
Code:
$
$
$ cat -n apps.pl
     1  #!perl
     2  use strict;
     3  use warnings;
     4  my $append = "/cgi-bin/jboss/apps.joa/health?method=checkurl";
     5  my %x;
     6  while (<DATA>) {
     7    /^"(.+apps.*)"\s*=\s*"(.+)"/ and $x{$1} = "\"$2$append\"";
     8  }
     9  while (my ($key, $value) = each %x) {
    10    print "Appname : $key\n";
    11    print "curl $value\n";
    12    # If you want to actually invoke curl, then uncomment the line below
    13    # system("curl $value");
    14  }
    15  __DATA__
    16  {
    17  "_exp" = "81652254";
    18  "apps" = {
    19  "AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com...oa/accountInfo";
    20  "StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.co.../staticcontent";
    21  "ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com...ps.joa/content";
    22  "CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com...ervice/qastats";
    23  "SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com...pps.joa/serial";
    24  };
    25  }
    26
$
$
$ perl apps.pl
Appname : CommQandAStatisticapps
curl "http://spaceqa3-svc.corp.zaphida.com...ethod=checkurl"
Appname : ContappsB2B
curl "http://spaceqa3-svc.corp.zaphida.com...ethod=checkurl"
Appname : StatContentapps
curl "http://egq3-wsf-5001.corp.zaphida.co...ethod=checkurl"
Appname : SerialNberapps
curl "http://spaceqa3-svc.corp.zaphida.com...ethod=checkurl"
Appname : AcctInfoapps
curl "http://spaceqa3-svc.corp.zaphida.com...ethod=checkurl"
$
$
$

tyler_durden
# 3  
Old 08-17-2010
Thanks Tyler, it works great!!

One change I want though here is that instead of appending to the whole URL it should append to http://...Smilieort

SO the output I want is -

http://spaceqa3-svc.corp.zaphida.com...ethod=checkurl

instead of -

http://spaceqa3-svc.corp.zaphida.com...ethod=checkurl

Thanks,
Jack.
# 4  
Old 08-17-2010
Quote:
Originally Posted by jacki
...
One change I want though here is that instead of appending to the whole URL it should append to http://...port
...
Code:
$
$
$ cat -n apps.pl
     1  #!perl
     2  use strict;
     3  use warnings;
     4  my $append = "cgi-bin/jboss/apps.joa/health?method=checkurl";
     5  my %x;
     6  while (<DATA>) {
     7    m{^"(.+apps.*)"\s*=\s*"(http://.*?/).*"} and $x{$1} = "\"$2$append\"";
     8  }
     9  while (my ($key, $value) = each %x) {
    10    print "Appname : $key\n";
    11    print "curl $value\n";
    12    # If you want to actually invoke curl, then uncomment the line below
    13    # system("curl $value");
    14  }
    15  __DATA__
    16  {
    17  "_exp" = "81652254";
    18  "apps" = {
    19  "AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
    20  "StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
    21  "ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
    22  "CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
    23  "SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
    24  };
    25  }
    26
$
$
$ perl apps.pl
Appname : CommQandAStatisticapps
curl "http://spaceqa3-svc.corp.zaphida.com:8546/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : ContappsB2B
curl "http://spaceqa3-svc.corp.zaphida.com:9203/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : StatContentapps
curl "http://egq3-wsf-5001.corp.zaphida.com:8531/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : SerialNberapps
curl "http://spaceqa3-svc.corp.zaphida.com:8341/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : AcctInfoapps
curl "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/apps.joa/health?method=checkurl"
$
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 08-18-2010
Thanks Tyler

Your the man!!Smilie

Can you explain a little about this line -

m{^"(.+apps.*)"\s*=\s*"(http://.*?/).*"} and $x{$1} = "\"$2$append\"";

specially "\"$2...
# 6  
Old 08-18-2010
Quote:
Originally Posted by jacki
Thanks Tyler

Your the man!!Smilie

Can you explain a little about this line -

m{^"(.+apps.*)"\s*=\s*"(http://.*?/).*"} and $x{$1} = "\"$2$append\"";

specially "\"$2...
perlre - perldoc.perl.org

Each ( ) pair creates a backreference that can later be accessed (until next regex) using $1, $2, $3 ... $n. So, the first ^"(.+apps.*)"\s*= captures everything after the first double-quotes until before the last double-quotes, that which comes before zero or more amount of spaces and literal =. After that again the undetermined amount of spaces and single double-quotes. After that it practically the same thing with the URI as it was with the "key", except that he uses .*? to make * ungreedy in the capture. Without ungreedy matching more than the hostname would have been captured.

Using and is a short-hand so he doesn't have to wrap the m{} in an if-block. In $x{$1} he's using the first backreference as key in the hash, and "\"$2$append\"" means interpolating two values, $2 (2nd backreference) and $append into a single string surrounded by double-quotes.
This User Gave Thanks to ikki For This Post:
# 7  
Old 08-18-2010
In addition to the explanation posted above, I'd like to add that this -

Code:
$x{$1} = "\"$2$append\"";

was done so that the value of each hash key would be of the form

Code:
"http://someURL"

instead of

Code:
http://someURL

That's because I wasn't sure if the URL in the curl invocation requires double-quotes. If it doesn't, i.e. if the following works -

Code:
curl http://someURL

then you could change the code to -

Code:
$x{$1} = "$2$append";

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. UNIX for Advanced & Expert Users

perl and HP-UX : instmodsh in combination with software depot : update inventory for installed Perl

we create a HP-UX software depot with a new perl-modul. after installation of the software depot, the perl module i can't find with instmodsh in the inventory for installed Perl modules. - i have learned of using instmodsh command : i find out what modules are already installed on my system. ... (0 Replies)
Discussion started by: bora99
0 Replies

5. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

7. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

8. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

9. Shell Programming and Scripting

Passing date formats in Perl: i.e. Jul/10/2007 -> 20070710 (yyyymmdd) - Perl

Hi , This script working for fine if pass script-name.sh Jul/10/2007 ,I want to pass 20070710(yyyymmdd) .Please any help it should be appereciated. use Time::Local; my $d = $ARGV; my $t = $ARGV; my $m = ""; @d = split /\//, $d; @t = split /:/, $t; if ( $d eq "Jan" ) { $m = 0 }... (7 Replies)
Discussion started by: akil
7 Replies

10. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies
Login or Register to Ask a Question