Help me with perl programming


 
Thread Tools Search this Thread
Top Forums Programming Help me with perl programming
# 1  
Old 10-12-2011
Help me with perl programming

Hi,
i am very beginer to perl, I am reading one xml file and i am creating hash table for that file. i written code like this
Code:
#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML::Reader;
#Reading XML with a pull parser
my $file;
open( $file, 'formal.xml');
my $reader = XML::LibXML::Reader->new( IO => $file ) or die ("unable to open file");
my %nums;
while ($reader->nextElement( 'Data' ) ) {
 
my $des = $reader->readOuterXml();
 
$reader->nextElement( 'Number' ); 
my $desnode = $reader->readInnerXml(); 
 
$nums{$desnode}= $des;
print( " NUMBER: $desnode\n" );
print( " Datainfo: $des\n" );
}

but my problem is i am specifying file path inside the module "formal.xml". i cant use file path inside i need to specify outside the module.I use that one in my module. how should i do in perl. can anyone help me.
# 2  
Old 10-12-2011
Without a copy of formal.xml I can't test, but I assume you successfully build the hash in the code above.
Could you clarify your question, you import modules with the use keyword and create instances of the classes they define with the new keyword (by convention), formal.xml is not a module but a file, are you saying that the path to the formal.xml file is declared in the file itself?
# 3  
Old 10-12-2011
Hi,
thanks for your reply.
In the above code i used libxml::reader module, i read the "formal.xml" file and i written script to create hash, file path specified in the main module. but now i need to modify the code wothout specify the filepath in main module, specify outside the main module and create hash. for example like main and sub function call and returns in c language. i am trying in perl using sub functions.but i am not succeded.

regards,
veerubiji.

Last edited by veerubiji; 10-12-2011 at 08:57 AM..
# 4  
Old 10-12-2011
Do you mean "How do I read the parameters supplied to my routine?"
Code:
$hash_ref=get_data_as_hash($file);
...
sub get_data_as_hash{
   my  $file_handle = shift;
   my %nums;
   ...
   return \%nums;
}

# 5  
Old 10-12-2011
Hi,
yes like that but i need to create one module for the script as i posted. i will call that module in my main function, in my main function the file path is the input and it return hash value. so i need to create one module that calls in my main function.
regards,
veerubiji.

Last edited by veerubiji; 10-12-2011 at 11:38 AM..
# 6  
Old 10-12-2011
Ahh, now I get you, your module would look a bit like this...

Code:
#!/usr/bin/perl

package  My::XML::AsHash; # or whatever you want to call the package (remember to have the appropriate directory structure)

use warnings; 
use strict; 
use XML::LibXML::Reader; 
my $DEBUG=1; 
sub new{
   my($proto,%args)=@_;
   my $class= ref($proto)||proto; #Make inheritable
   my $self;    open( $self->{file}, '<', $args{'file'});
   my $reader = XML::LibXML::Reader->new( IO => $self->{file} ) 
      or die ("unable to open file");    # my $self->{nums};    
   while ($reader->nextElement( 'Data' ) ) {
        my $des = $reader->readOuterXml();
        $reader->nextElement( 'Number' );
         my $desnode = $reader->readInnerXml();
         $self->{nums}->{$desnode}= $des;
        print( " NUMBER: $desnode\n" ) if $DEBUG;
        print( " Datainfo: $des\n" ) if $DEBUG;
   }
   bless($self,$class);
}
exit 1;

It would be saved in for example /home/veerubiji/perl_libs/My/XML/AsHash.pm and would be called as follows
Code:
#!/usr/bin/perl
push @INC '/home/veerubiji/perl_libs/';
use My::XML::AsHash;
my $hash_of_xml=My::XML::AsHash->new(file=>'formal.xml');
for my $desnode (keys %{$hash_of_xml->{nums}}){
    print "$hash_of_xml->{nums}->{$desnode} is the value associated with $desnode\n";
}

This is a very rough and ready module however and I would STRONGLY recommend reading through perldoc perlboot and perldoc perltoot. A real world module should have an associated test suite, would provide accessors to the nums hash rather than exposing them directly, would have a perldoc section so that others could re-use your code and finally would be packaged so that you could install it on any of your users workstationsh2xs -XA -n My::XML::AsHash.
For someone who is at the beginning of their Perl coding you've jumped into the deep end, Have fun!

Last edited by Skrynesaver; 10-12-2011 at 12:58 PM..
This User Gave Thanks to Skrynesaver For This Post:
# 7  
Old 10-12-2011
Hi,
I have small doubts in the above module, i am reading "perlmod " i understand something. i am asking you,
can i use like this : package xmlhashtable as a module name.
can i use require function to call this module in my main section.
Code:
require My::XML::ashash

and also i am using strawberry Perl latest version so i need to install any anything to execute this program.
can you explain whats going with this comments.
Code:
my($proto,%args)=@_;
   my $class= ref($proto)||proto; #Make inheritable
   my $self;    open( $self->{file}, '<', $args{'file'});
   my $reader = XML::LibXML::Reader->new( IO => $self->{file} ) 
      or die ("unable to open file");    # my $self->{nums};

why we are opening file in this module ,we can directly open in the main function.
finally is there any another easy way to create module. i started to read your suggested tutorials.

thank you very much for reply.

regards,
veerubiji.

---------- Post updated at 11:08 PM ---------- Previous update was at 09:20 PM ----------

Hi,
after reading module i create like this is it ok
Code:
#!/usr/bin/perl
 
package Mymodule;
 
use warnings;
use strict;
use Exporter qw(import);
use Carp;
use XML::LibXML::Reader;
 
our @EXPORT_OK(myFunction);
 
#Reading XML with a pull parser
sub MyFunction {
    my $fh = shift; 
    my $reader = XML::LibXML::Reader->new( IO => $fh )
        or die ("unable to open file");
    my %nums;
    while ($reader->nextElement( 'Data' ) ) {
 
        my $des = $reader->readOuterXml();
 
        $reader->nextElement( 'Number' ); 
        my $desnode = $reader->readInnerXml(); 
 
        $nums{$desnode}= $des;
    }
    return %nums;
}
 
1;

thank you very much both modules working.
you helped me alot.

Last edited by veerubiji; 10-13-2011 at 06:02 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl programming issue

Dears, I want to print filename and count of each file in perl but failing to implement. `find $srcFolder -maxdepth 1 -type f -name "*$workDate*$fileExt" -exec sh -c ' && printf "$workDate|%s|%s\n" "$(wc -l<"$0")" *$workDate*$fileExt' {} \ >> /Sadique/filelog.out \\; 2> /dev/null`; ... (2 Replies)
Discussion started by: sadique.manzar
2 Replies

2. Shell Programming and Scripting

Perl programming help

I am trying to make a simple perl program that reads 20 characters upstream from the codon ATG in a given sequence. The following is what I have. I just dont know how to make the program read 20 characters upstream from the ATG codon. print "\nThis program will read 20 characters upstream... (1 Reply)
Discussion started by: patiencenpray
1 Replies

3. Shell Programming and Scripting

perl programming

how to link the linux files in perl on the local webpage ???? suppose we have some results and want to get them published on the local webpage of our internal site. how this can be done using HTML and perl together , so that the results are published directly on the webpage. thanks kullu (0 Replies)
Discussion started by: kullu
0 Replies

4. Programming

Programming help - Perl !

I am having a text file with Vivek 50 Ram 34 Hulk 45 Vivek 23 Ram 23 Vivek 55 Now I need a perl script to display the fields of 1st column & the 2nd column with summation (& avoid the duplicates). Vivek 128 Ram 57 hulk 45 Plz help me... (1 Reply)
Discussion started by: gameboy87
1 Replies

5. Shell Programming and Scripting

Perl programming error

Hi, everyone!! i am new to perl programming.. plz help me. #!C:/perl/bin use warnings; use strict; use Text::CSV_XS; my @rows = ""; my $row; my $count; my $fh; my @fields = ""; my $csv = Text::CSV_XS->new ({binary =>1}) or die "cannot use CSV:" .Text::CSV->error_diag (); open... (3 Replies)
Discussion started by: kvth
3 Replies

6. Shell Programming and Scripting

Perl Vs Shell Programming

Can someone please tell me what the big deal about perl is? i have been doing shell programming for quite a number of years and I have to say, there's very little if any thing that I can't do in shell programming. i just need to investigate how to do it. so, my question is, does deep... (1 Reply)
Discussion started by: SkySmart
1 Replies

7. UNIX for Dummies Questions & Answers

Is PERL a programming language?

I need a small and simple clarification... Can someone tell me whether PERL is a programming language or not. Also, can shell scripts also considered as programming language or not. Also, please tell me the exact difference between programming language and scripting. Please help.... (3 Replies)
Discussion started by: Anjan1
3 Replies

8. Programming

Perl Programming with Mapping

use strict; print "Enter last 4 digits of phone number:"; chomp(my $number=<>); die "Invalid number: '$number'\n" unless $number=~/^\d{4}$/; my @d=split(undef,$number); my %map={ 2=>"", 3=>"", 4=>"", 5=>"", 6=>"", 7=>"", 8=>"", 9=>"", }; my $r=$map{$d}.$map{$d}.$map{$d}.$map{$d};... (1 Reply)
Discussion started by: tturn33
1 Replies

9. Shell Programming and Scripting

Perl Programming for Splitting

Hi, I am extracting SQL queries into a file and the file is as follows ********************************************************* select BatchKey ,restartStatus ,batchContextBuffer ,batchPgmId ,StartKey , EndKey ,Mcbatchcontrol_ver from qsecminload.Mcbatchcontrol_t where RefId = :1 ... (5 Replies)
Discussion started by: sagarbsa
5 Replies

10. UNIX for Dummies Questions & Answers

PERL - DB programming

Hi friends, What are the possible ways to connect to DB2 database from Perl (on unix). I need to connect to DB2 and get records for further processing. Can you please suggest the best possible way. I heard about DBI/DBD, if you have some sample scripts please post them too. Thanks in advance. (3 Replies)
Discussion started by: satguyz
3 Replies
Login or Register to Ask a Question