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

Is there work around if you are using use strict and you also want to use switchs with your program?

Replies are listed 'Best First'.
Re: use strict and -s
by japhy (Canon) on Mar 12, 2001 at 23:57 UTC
    Sure. -s makes GLOBAL variables, so declare them.
    #!/usr/bin/perl -ws use strict; use vars qw( $size $shape ); print "A $shape of size $size\n";
    Now you can say: object -size=10 -shape=square.

    japhy -- Perl and Regex Hacker
      Or just give them their full names...:

      #!/usr/local/bin/perl use strict; $::e && $main::f && print "wabbit";
      for example.
Re: use strict and -s
by TGI (Parson) on Mar 12, 2001 at 23:55 UTC
    Use Getopt::Long or Getopt::Std. They're part of the standard library. I didn't even ken the -s option, until today, but I've used Getopt::Std with great success. Here's a snippet that I use to look for people who need help using a tool.

    use strict; use Getopt::Std; my $file; usage() unless ($ARGV[0]); my %opts; getopts ('h?',\%opts); usage() if (exists($opts{h})); usage() if (exists($opts{'?'}));

    This is a sweet module, use it.
    TGI says moo

Re: use strict and -s
by mirod (Canon) on Mar 13, 2001 at 00:13 UTC

    And if you really don't know which variables might be initialized:

    perl toto -var=2

    where toto is:

    #!/usr/bin/perl -ws use strict; { # you can use -s defined variables here no strict 'vars'; print $var; }

    Just note that generally -s and use strict; don't go well together. -s should really be used only for throw-away scripts, if not just deprecated, while use strict would be used for everything else. If you use strict you should also use "clean" alternative to -s such as one of the various Getopt::* modules.

Re: use strict and -s
by davorg (Chancellor) on Mar 14, 2001 at 14:58 UTC
Re: use strict and -s
by Tuna (Friar) on Mar 12, 2001 at 23:57 UTC
    Why would you need a "workaround"????
    #!/usr/local/bin/perl -w use strict; die "Usage: $0 <yyyymmdd> [1 7177 201 201 -a]\n This program will do XYZ, based upon your number. Use -a (this is your + switch) to choose all\n" unless @ARGV ==2; if ($ARGV[1] eq "-a") { print "Hey, I'm a switch!\n"; } # and so on........
    Obviously, not anything useful above, other than to make a point regarding "strict" and switches. See perlman:lib:strict for more info on use strict;

    Update: Clearly, I misunderstood the question. I have requested that the editor remove this reply, as it is irrelevant.