Meena212 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, I have one requirement in which I pass a XML file as an input argument to my script. I don't want to read or process that file input in my script. Whereas I need to read that xml file in library and process the data inside. This data will be provided to script by library when a particular subroutine is called in script. Now How can I able to read the xml file command argument by script in my library? Thanks
  • Comment on How can we read input argument file to a script from its main library?

Replies are listed 'Best First'.
Re: How can we read input argument file to a script from its main library?
by Discipulus (Canon) on Mar 21, 2017 at 07:44 UTC
    Hello Meena212 and welcome to the monastery and to the wonderful world of Perl!

    Perl has many (well, many many) special variables: they are described in the perlvar man page. Specifically arguments passed in the command line are inserted into the special array @ARGV so that the first argument passed to your program will be $ARGV[0] .

    This is the basic. In addition you have (already installed) two core modules to handle argument passed via command line: Getopt::Std and Getopt::Long

    Then for the processing of the XML inside the file i suggest to use XML::Twig (anyway do NOT use XML::Simple !). The module has a dedicated website full of examples and tutorials.

    Use Super Search here at the monastery to spot other valid examples about XML parsing.

    I have some usefull links on my homenode about XML.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How can we read input argument file to a script from its main library?
by choroba (Cardinal) on Mar 21, 2017 at 08:43 UTC
    In the script, read the file name from $ARGV[0] or use a library as others have recommended. Then pass the file name as an argument to the library:
    package My::XML; sub parse_xml { my ($filename) = @_; ... }

    and call

    use My::XML; my $filename = $ARGV[0]; my $data = parse_xml($filename);

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: How can we read input argument file to a script from its main library?
by huck (Prior) on Mar 21, 2017 at 08:08 UTC

    This is a somewhat confusing question. the first argument to a perl script is in $ARGV[0]. so you can do this.

    use strict; use warnings; die 'I need an argument' unless ($ARGV[0]); # # capture name of file # my $name_of_file=$ARGV[0]; # # call a subroutine with the name of file # And capture the data it returns # my $data=do_something_else ($name_of_file); exit; sub do_something_else { my $filename=shift; print 'Name of file:'.$filename."\n"; # # read all of file at once into single variable # my $file_contents; { local $/=undef; open (my $infile,'<',$filename) or die 'cannot open'.$@; $file_contents=<$infile>; close ($infile); } print 'size of file:'.length($file_contents)."\n"; return $file_contents; } #do_something else
    Result:
    perl argin.pl argin.pl Name of file:argin.pl size of file:684

Re: How can we read input argument file to a script from its main library?
by Corion (Patriarch) on Mar 21, 2017 at 08:10 UTC

    See Getopt::Long and/or perlvar on @ARGV maybe?

    If that doesn't answer your question, can you maybe post a 10 line script that demonstrates the problem you have?

      Hi root, Thanks for your help. I have an idea of how handle command line arguments given to a script. Kindly go through the problem and background as below: Background : A script written to perform operation and all the depending subroutines for the script are in main library. Here no input arguments were accepted Problem : Now the input to file is an XML file. But I don't want to modify anything in local script. whereas when I call the script with xml file as a command line input, I should be able to read it directly in library. Is there a way to do this?

        Have you looked at Getopt::Long and @ARGV?

        What problems do you have applying the ideas discussed there to your program?

        Maybe you also want to look at perlsub to find how to pass parameters to subroutines?