We help IT Professionals succeed at work.

Perl script

luyan
luyan asked
on
Medium Priority
820 Views
Last Modified: 2007-12-19
I am a newer of writing perl script. What is Getopts for? Can you explain the following script? Thanks!
&Getopts ( "d:s:i:f:r:h" ) ;
 if ( defined ( $opt_h ) )
{
    &print_help_msg ;
    exit ( 1 ) ;
}
Comment
Watch Question

Author

Commented:
I found "getopts" command are processes command-line arguments and checks for valid options. I  know what &Getopts ( "d:s:i:f:r:h" )  means now. Do you know any good online documents for learning perl and checking commands in perl scripting?
Thanks!
Top Expert 2009
Commented:
Online documentation for perl:
http://perldoc.perl.org

Tutorials at above site:
http://perldoc.perl.org/index-tutorials.html

To get documentation on modules:
http://search.cpan.org/

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Nice documentation. It's handy! Thanks!
Can you explain the following script when choosing the option "d"?     Thanks!
&Getopts ( "d:s:i:f:r:h" ) ;
if ( defined ( $opt_d ) )
{
    $Object[0][1] = $opt_d ;
    if ( defined $COMMONDBS{$Object[0][1]} ) {
       $Object[0][4] = $COMMONDBS{$Object[0][1]} ; }
    else {
       $Object[0][4] = $Object[0][1] ;
print $Object[0][4]; }
}
CERTIFIED EXPERT
Top Expert 2007

Commented:
All the perl documention should be on your computer

perldoc Getopt::Std

CERTIFIED EXPERT
Top Expert 2007
Commented:
In the following code:

&Getopts ( "d:s:i:f:r:h" ) ;
if ( defined ( $opt_d ) )
{
    $Object[0][1] = $opt_d ;
    if ( defined $COMMONDBS{$Object[0][1]} ) {
       $Object[0][4] = $COMMONDBS{$Object[0][1]} ; }
    else {
       $Object[0][4] = $Object[0][1] ;
print $Object[0][4]; }
}

if you invoke the script with

script -d foobar

then $Object[0][1] gets set to 'foobar'

Then if the hash $COMMONDBS{foobar} exists, then set $Object[0][4] to the value of the hash.

All seems like very convoluted code and rather cryptic variable names and structures.
Commented:
if you want to read -s and -e from commandline.
sub read_param
{
      my %opts;
      
      getopts('s:e:',\%opts);
      
      if($opts{'s'})
      {
            $s = $opts{'s'};
      }
      if($opts{'e'})
      {
            $e = $opts{'e'};
      }
      return;
}

Author

Commented:
Do you have any good idea on debugging perl script?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
perldoc perldebug
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.