Perl : Getopts and Strict -How to solve

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : Getopts and Strict -How to solve
# 1  
Old 11-25-2013
Perl : Getopts and Strict -How to solve

How do I get past the error when using strict and GetOpts ?

use strict;
use Getopt::Std; 
# Process the command line options 
die "Usage: $0 -r <router> -u <username> -p <password> -e <enable password>\n" if (@ARGV < 6); 
exit if (!getopts('r:u:p:e:')); 
my $unam=$opt_u; 
my $psswd=$opt_p; 
my $epsswd=$opt_e; 
my $dev=$opt_r; 

print "$unam $psswd $epsswd $dev "

/home/popeye> topt -r -u popeye -p thesailorman
Global symbol "$opt_u" requires explicit package name at topt line 10.
Global symbol "$opt_p" requires explicit package name at topt line 11.
Global symbol "$opt_e" requires explicit package name at topt line 12.
Global symbol "$opt_r" requires explicit package name at topt line 13.
Execution of topt aborted due to compilation errors.
Thanks in advance
# 2  
Old 11-25-2013
You need to declare the variables using use vars for it to work:
use vars qw($opt_u $opt_p ...);

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 11-25-2013

On my system:
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
perl 5.10.0

We find:
       vars - Perl pragma to predeclare global variable names (obsolete)

-- perldoc vars
whereas from perldoc Getopt::Std
       Note that, if your code is running under the recommended "use strict
       'vars'" pragma, you will need to declare these package variables with

           our($opt_x, $opt_y);

The former may work, I have often used the latter.

Learning to use perldoc can make one less dependent on others.

Good luck .. cheers, drl

Last edited by drl; 11-25-2013 at 09:15 AM..
# 4  
Old 11-25-2013
Yes. Thanks to all. I found it after searching the web. Thanks again !!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Ssh disable strict checking

What are the different ways to disable ssh strict checking? I've seen this mentioned a few times but it doesn't seem to be working. $ ssh -o 'StrictHostKeyChecking no' admin@hostname Is there a file somewhere in /etc that I could... (4 Replies)
Discussion started by: cokedude
4 Replies

2. Shell Programming and Scripting

Help! Can't locate after Cygwin update

I installed gcc4 today using setup.exe from cygwin. However, I cannot run any of my perl program after that. For example, Run@Run-THINK /home $ perl Can't locate in @INC (@INC contains: /usr/lib/perl5/5.10/i686-cygwin / usr/lib/perl5/5.10... (0 Replies)
Discussion started by: littledeer
0 Replies

3. Shell Programming and Scripting

getopts function in Perl

Hi All I have searches getopts function in Perl a lot, but yet i didn't cleared with it. First I want to know what is the meaning of getopts('t:c:', \%options); and please explain getopts function in an easy way.. (4 Replies)
Discussion started by: parthmittal2007
4 Replies

4. Shell Programming and Scripting

New PERL guru's help on

I opened and found some not understandable stuff, please let me know if you have any Idea on the same. 1) Line 23 => $bits |= (what is $= here how it affect the statement) 2) Line 36 => $^H (what is that I haven't found any statement on this in google) 3) Line 41 =>... (3 Replies)
Discussion started by: jatanig
3 Replies

5. Shell Programming and Scripting

Perl strict

What is the sake of using `use strict;` in perl codes? and how can avoid it?.TnX. (8 Replies)
Discussion started by: Zaxon
8 Replies

6. Shell Programming and Scripting

Strict Argument

Im trying to write a bash script that has an if statment that when the user enters ONLY that exact argument, will echo what follows that conditon. For example: for file in $1 do if then Var1=$(cat hello | egrep "that pattern" | awk '{ print $NF }') cat $Var1 fi done Basically,... (3 Replies)
Discussion started by: oxoxo
3 Replies

7. UNIX for Dummies Questions & Answers

Can somebody solve this

I have to find the files older than 200 days from a path and copy them to some other directory with the current date stamp attached to it. i have written like follows: #!/bin/ksh DSTAMP=$(date +"%y%m%d%H%M") rm $CA_OUT_PATH/ftp_logs/temp touch $CA_OUT_PATH/ftp_logs/temp chmod 777... (13 Replies)
Discussion started by: sreenusola
13 Replies

8. Programming

does any one know how to solve?

Hello experts, Here is my code.I can create the database.But I also want it to see standard output.Please see the blocked code.If i use them they show me weired symbols. #include <stdio.h> #include <stdlib.h> struct date { int month; int day; int year; }; struct empRec{... (14 Replies)
Discussion started by: mlhazan
14 Replies

9. Shell Programming and Scripting

How to solve this

I have to write an script for.. CUST: 123 trans: some contents CUST: 1234 trans: some contents Now wat i have to do is this: CUST:123 akash trans: some contents CUST:1234 akash1 trans: I have been able to add... (3 Replies)
Discussion started by: akashag22
3 Replies
Login or Register to Ask a Question