use Getopt::Std; and use strict;

Hi everyone,
I have a problem using Getopt::Std. I depend on use strict for all my 
code, and when I use Getopt::Std all the variables it creates (to 
hold the command line option values) are flagged by strict because 
they have not been scoped. I get errors like this:

Line 70:  Global symbol "$opt_m" requires explicit package name

If I don't use strict the program runs fine. Is there anything I can 
do about this? Does anyone use both together?

Thanks for the help.

-Hans
0
hehe
5/11/2002 1:30:16 AM
perl.beginners 29344 articles. 3 followers. Follow

6 Replies
682 Views

Similar Articles

[PageSpeed] 3

Yes you can say

our $opt_m;

or 

use vars qw($opt_m);

at the top of your program (depending on perl version).
----- Original Message ----- 
From: "Hans Holtan" <hehe@nature.berkeley.edu>
To: <beginners@perl.org>
Sent: Friday, May 10, 2002 9:30 PM
Subject: use Getopt::Std; and use strict;


> Hi everyone,
> I have a problem using Getopt::Std. I depend on use strict for all my 
> code, and when I use Getopt::Std all the variables it creates (to 
> hold the command line option values) are flagged by strict because 
> they have not been scoped. I get errors like this:
> 
> Line 70:  Global symbol "$opt_m" requires explicit package name
> 
> If I don't use strict the program runs fine. Is there anything I can 
> do about this? Does anyone use both together?
> 
> Thanks for the help.
> 
> -Hans
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> 

0
thgibbs
5/11/2002 2:07:21 AM
Hans Holtan wrote:
> 
> Hi everyone,
> I have a problem using Getopt::Std. I depend on use strict for all my
> code, and when I use Getopt::Std all the variables it creates (to
> hold the command line option values) are flagged by strict because
> they have not been scoped. I get errors like this:
> 
> Line 70:  Global symbol "$opt_m" requires explicit package name
> 
> If I don't use strict the program runs fine. Is there anything I can
> do about this? Does anyone use both together?


Use a lexically scoped hash for the options:

use strict;
use Getopt::Std;

my %options;
getopt( 'ab:cde', \%options );



John
-- 
use Perl;
program
fulfillment
0
krahnj
5/11/2002 5:12:24 AM
On Friday, May 10, 2002, at 07:07 , Tanton Gibbs wrote:

> Yes you can say
>
> our $opt_m;
>
> or
>
> use vars qw($opt_m);
>
> at the top of your program (depending on perl version).

I've been preached the orthodoxy of the later - but
have never understood the distinction...

Yes, have read coping with scoping.

anyone have a human langugae explanation.


ciao
drieux

---

0
drieux
5/11/2002 3:43:13 PM
> > Yes you can say
> >
> > our $opt_m;
> >
> > or
> >
> > use vars qw($opt_m);
> >
> > at the top of your program (depending on perl version).
> 
> I've been preached the orthodoxy of the later - but
> have never understood the distinction...
> 
> Yes, have read coping with scoping.
> 
> anyone have a human language explanation.

if ($] < 5.600) {
    warn "our isn't implemented";
}

Note that 'use vars' is supposedly depreciated, so don't
use it if your script depends on 5.6 features.  Placing
'our' in a lexical scope probably makes it externally
visible until you leave the scope, 'use vars' imports
into your symbol table.  Hope this is right :)

Jonathan Paton

=====
---------------BEGIN GEEKCODE BLOCK-----------v3.12
GCS/E d+ s+: a20 C++(+++)>$ UHL++>+++ P+++ L++>++++
E- W++(-) N+ o? K- w--- !O M-- !V PS-- PE++ Y++ PGP
t@ 5-- X-- R- tv- b  DI+ D- G++ e h! !r--->++ !y---
----------------END GEEKCODE BLOCK-----------------
JAPH: print`perldoc perlembed`=~/(Ju.*)/,"\n"

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com
0
jonathanpaton
5/11/2002 5:00:27 PM
> > Yes you can say
> >
> > our $opt_m;
> >
> > or
> >
> > use vars qw($opt_m);
> >
> > at the top of your program (depending on perl version).
> 
> I've been preached the orthodoxy of the later - but
> have never understood the distinction...
> 
> Yes, have read coping with scoping.
> 
> anyone have a human language explanation.

if ($] < 5.600) {
    warn "our isn't implemented";
}

Note that 'use vars' is supposedly depreciated, so don't
use it if your script depends on 5.6 features.  Placing
'our' in a lexical scope probably makes it externally
visible until you leave the scope, 'use vars' imports
into your symbol table.  Hope this is right :)

Jonathan Paton

=====
---------------BEGIN GEEKCODE BLOCK-----------v3.12
GCS/E d+ s+: a20 C++(+++)>$ UHL++>+++ P+++ L++>++++
E- W++(-) N+ o? K- w--- !O M-- !V PS-- PE++ Y++ PGP
t@ 5-- X-- R- tv- b  DI+ D- G++ e h! !r--->++ !y---
----------------END GEEKCODE BLOCK-----------------
JAPH: print`perldoc perlembed`=~/(Ju.*)/,"\n"

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com
0
jonathanpaton
5/11/2002 5:00:36 PM
> > Note that 'use vars' is supposedly depreciated, so don't
> > use it if your script depends on 5.6 features.  Placing
> > 'our' in a lexical scope probably makes it externally
> > visible until you leave the scope, 'use vars' imports
> > into your symbol table.  Hope this is right :)
> 
> That's not how I see it.

What part don't you understand?

"Note that 'use vars' is supposedly depreciated"

  - see "Programming Perl" page 861

"so don't use it if your script depends on 5.6 features."

Or better said as "To ensure your script/module works on
earlier versions 'use vars', but use 'our' if your project
won't run with less than 5.6.0 anyway."

> Both 'use vars' and 'our' are used to keep "use strict 'vars'"
> happy, by declaring a global variable and allowing one to use
> it without specifying the full package name.

And I use strictures to ensure my code is readable... so having
to 'use vars' or 'our' is part of that.  The difference is I'm
trying to keep myself/other coders happy, not strict :)

> The difference is that 'use vars' is not lexically scoped and
> affects the entire package, whereas 'our' takes the same scoping
> rules as 'my', but in contrast with 'my', refers to the same
> global variable.

I should have read the documentation for 'our', obviously.  The
difference between 'use vars' and 'our' are subtle!

> As far as I understand it, this has nothing to do with importing
> in the symbol table.

You've never seen the implementation of 'use vars' then :)  The
tail end of which is:

        *{"${callpack}::$sym"} =
          (  $ch eq "\$" ? \$   {"${callpack}::$sym"}
           : $ch eq "\@" ? \@   {"${callpack}::$sym"}
           : $ch eq "\%" ? \%   {"${callpack}::$sym"}
           : $ch eq "\*" ? \*   {"${callpack}::$sym"}
           : $ch eq "\&" ? \&   {"${callpack}::$sym"}
           : do {
                require Carp;
                Carp::croak("'$ch$sym' is not a valid variable name");
             });

Which I assure you has a LOT to do with importing into symbol tables.
This is the reason 'use vars' is package scoped.

Jonathan Paton

=====
---------------BEGIN GEEKCODE BLOCK-----------v3.12
GCS/E d+ s+: a20 C++(+++)>$ UHL++>+++ P+++ L++>++++
E- W++(-) N+ o? K- w--- !O M-- !V PS-- PE++ Y++ PGP
t@ 5-- X-- R- tv- b  DI+ D- G++ e h! !r--->++ !y---
----------------END GEEKCODE BLOCK-----------------
JAPH: print`perldoc perlembed`=~/(Ju.*)/,"\n"

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com
0
jonathanpaton
5/11/2002 6:10:41 PM
Reply:

Similar Artilces:

Why using use strict;
Hi all, I know why I should use "use strict;" but what happen if I use "use strict;" then if the code is OK, I delete this line? Shouldn't it work the same without this line if the code has no problems? Thanks. Teddy, orasnita@home.ro why would you want to do that Octavian? "use strict" is a sanity safety net. What your saying is like "I know why I should use safety belt whilst driving my car but what if I took it off?" - of course you can do it but beware of the consequences. joel -----Original Message----- From: ...

why to use 1)use CGI,2)use Strict ,3)use Carp in script
hi why we r using 1)use CGI, 2)use Strict, 3)use Carp in the begining of script sometimes. What is the use of it.Is the files CGI.pm,Strict.pm,Carp.pm default with PERL. Thnx __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com On Thu, 2004-11-04 at 09:31, supriya devburman wrote: > hi Hi > why we r using > 1)use CGI, We only use CGI when we want to use methods from that module (usually on web pages); see `perldoc CGI` for more information on what you...

Using Using
I have just completed reading a chapter in my book on performance. The books says to speed up performance on your web site you should use the using statement when opening connections. My question is how do you catch errors if you are using this as apposed to a try catch block.thanksBryan  Why would you use it as an alternative to a try/catch statement. Couldn't you just do something like  public void UsingSomeMethod() { try { SomeMethod(); } catch(SomeException e) { /// do something useful }}public void SomeMethod() { using (Something) { ...

When to use ( and when to use ((?
I'm just not getting it. When do you use ( in a statement, when do you need to use ((, and is the space after either or both mandatory? For example, I coded this statement: if (( $_ eq $bad_guys_ip )) { more_stuff_here }; Why the two (( in an if? And why does it seem to not work when I miss the space after the ((? Thanx! Mark me as... /Corn-fused|Dense/ > I'm just not getting it. >=20 > When do you use ( in a statement, when do you need to use ((, and is > the space after either or both mandatory? >=20 > For example, I coded this sta...

PROBLEM WITH THE USES OF USE STRICT
Hello Team, Can someone please tell me why the below code does not work correctly? Here is the problem: Once I run the program and type in a couple of wrong answers and then type in the correct answer I still get the wrong response (e.g "No that is wrong! Try again.") But if I type in the correct answer first I get the correct response. I also should add that if I don't use "use strict" and rename my variable without "my". The code then works as it should. regards, Jaimee #!/usr/bin/perl -w use strict; print (&q...

use Strict and using module
Hello, Purpose: I am trying to understand the reason for the following error. Error: "Bareword "NetCDF::READ" not allowed while "strict subs" in use..." Actions already taken: 1. Searched Google --> Perl Bareword not allowed while "strict subs" Most of the hits I have looked at deal with just an OPEN Perl command in a subroutine. I am trying to understand why the module call NetCDF::READ results in similar behavior. Not understanding the parallel between the OPEN explanations and how to apply it to NetCDF::READ. 2. Searched http:...

How to use the arguments to use() in the package being used
Howdy,=20 The subject says it all believe it or not :) What I'm trying to figure out is how to pass an argument=20 (pragma I believe is the proper term) to use() and do=20 sonethign in the package based on it. I've looked at CGI.pm source but can't seem to track it down.=20 (Similar idea as to CGIs -oldstyle_urls -newstyel_urls) http://search.cpan.org/~lds/CGI.pm-3.04/CGI.pm#PRAGMAS What I'd like to do is something like this: # for old time's sake we'll just use our favorite module use Foo::Monkey qw(:Foo :Bar -doamazingthings); #then in Foo::Monkey...

RE: Why do we even HAVE to... (was: Why using use strict;)
because it would reek havoc on all the perl one liners. And deter many beginners that are touching perl for the first time. And would annoy those who have to write a quick 5 line script in 3 seconds that forget a my for $line. > -----Original Message----- > From: Jake [mailto:jwebster@olemiss.edu] > Sent: Wednesday, June 05, 2002 8:56 AM > To: beginners-cgi@perl.org > Subject: Why do we even HAVE to... (was: Why using use strict;) > > > So, in post after post after post I see the comment "always > use strict"... > > I have seen thr...

Pragmas use strict and use warnings
------=_Part_1657_33307736.1193755179713 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi Whats the exact purpose of use strict and use warnings Pragmas in the Perl Programming Language. I know use warnings turns on all useful warnings. Thanks and Regards Kaushal ------=_Part_1657_33307736.1193755179713-- On 10/30/07, Kaushal Shriyan <kaushalshriyan@gmail.com> wrote: snip > Whats the exact purpose of use strict snip The strict pragma has three effects (unless modified by arguments): 1. most var...

Using Getopt::Std
Hi, i am using the Getopt::Std package in my code. use Getopt::Std; getopts('s:'); $a = $opt_s this is how i run my program from command line " > my_program.pl -s pattern now if "pattern" is something like abcd[12] how do i make my program accept the index 12 as a part of the string for option -s ? i would appreciate if anybody can help me on this. __________________________________ Do you Yahoo!? Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes http://hotjobs.sweepstakes.yahoo.com/signingbonus > Hi, >...

FreeAndNil to use or not to use
After reading these debates about FreeAndNil I separated the people into two categories: The first one says: "You should have a perfect design which prohibits errors from happening. So, if you have a perfect design then FreeAndNil is not necessary so you should not use it. If we see FreeAndNil in your code then it means that your design is not perfect." The second one says: "Yes, we agree that we should have a perfect design which prohibits errors from happening and we do our best in order to create a perfect design. However our practice says that it is better ...

To use or not to use Delphi
Sadly, it seems to me that there is a sort of race between the two threads, for and against using Delphi in new projects, with more or less the same users posting in both threads. Arguments are fiercely debated in both camps. Borland had their own vision. As a community, now that Delphi has changed ownership I believe we should try to be more consistent, more clear, and more articulate in what we expect from Embercadero in terms of Delphi. We can contribute to keeping Delphi alive and moving in the right direction. "Laurent Cocea" schrieb: > Sadly, it seems to me that there ...

I used to use Firefox until...
Name: Dan Product: Firefox Summary: I used to use Firefox until... Comments: Firefox takes forever to load now, even the absolute newest version. It has become clunky and everyone I know has moved on to another browser. I prefer Chrome. I suggest adding some sort of configuration optimization tool as well as toolbar/add-on install/hijack prevention. Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.53 Safari/525.19 From URL: http://hendrix.mozilla.org/ Note to readers: Hendrix gives no expectation of a re...

How to use net use?
Please can any one tell me how to use the dos command net use properly? I have read the help but can't seem to get it to work. Keep getting sytax error message. Thanks in advance. "Beowulf" <who-knows@no-one.com> wrote in message news:9krutk$puu$1@news.grc.com... > Please can any one tell me how to use the dos command net use properly? > > I have read the help but can't seem to get it to work. Keep getting sytax > error message. what are you trying to do? There are about 50 options to "net use" that do a ton of different thin...

Web resources about - use Getopt::Std; and use strict; - perl.beginners

Resources last updated: 12/21/2015 9:25:06 PM