Professional Web Applications Themes

Flag for script - PERL Beginners

Hello All, I'm trying to create a script with where you can put a flag to the script or not fx run it like: script -h or just script something like: if ( $ARGV[0] =~ /-h/ ) { print "You have chosen the very special ability!\n"; } else { print "you have not chosen the very special ability!\n"; } but when I run this one without any flag i get an error message: "use of initialized value in pattern match (m//)" Do I have to do it differently to get rid of the error? Thanks, Jakob...

  1. #1

    Default Flag for script

    Hello All,

    I'm trying to create a script with where you can put a flag to the script or
    not
    fx run it like:

    script -h
    or just
    script

    something like:

    if ( $ARGV[0] =~ /-h/ ) {
    print "You have chosen the very special ability!\n";
    } else {
    print "you have not chosen the very special ability!\n";
    }

    but when I run this one without any flag i get an error message:
    "use of initialized value in pattern match (m//)"

    Do I have to do it differently to get rid of the error?

    Thanks,
    Jakob


    Jakob Guest

  2. #2

    Default Re: Flag for script

    In a message dated 4/15/2004 3:19:04 PM Eastern Daylight Time, dk
    writes: 
    [Rest snipped]

    It's best not to deal with ARGV directly if you want to do the fancy stuff.
    Look into Getopt::Std or Getopt::Long.

    perldoc Getopt::Long
    perldoc Getopt::Std

    -will
    (the above message is double rot13 encoded for security reasons)

    Most Useful Perl Modules
    -strict
    -warnings
    -Devel::DProf
    -Benchmark
    -B::Dep
    -Data::Dumper
    -Clone (a Godsend)
    -Perl::Tidy
    -Beautifier
    WilliamGunther@aol.com Guest

  3. #3

    Default Re: Flag for script

    On Apr 15, 2004, at 2:10 PM, Jakob Kofoed wrote:
     

    Use:

    if (ARGV and $ARGV[0] =~ /-h/) {
     

    Better would be to use the standard module to handle this for you.
    It's called Getopt::Std.

    James

    James Guest

  4. #4

    Default RE: Flag for script

    Thanks for all your replies - I have something to work with!

    Cheers,
    Jakob


    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>




    Jakob Guest

  5. #5

    Default Re: Flag for script



    Jakob Kofoed wrote:
     

    Try
    if(defined $ARGV[0]) {
    if($ARGV[0] eq '-h') { print "Welcome to dash h\n"; }
    else { print "What did you choose?\n"; }
    } else {
    print "you have not chosen the very special ability!\n";
    }

    or:

    my $switch = $ARGV[0] || '';
    now it is initialized :)
    and you can do what you did before just use $switch instead of $ARGV[0]

    HTH

    Lee.M - JupiterHost.Net 
    Jupiterhost.Net Guest

Similar Threads

  1. flag or not to flag?
    By LeeD in forum Macromedia Director Lingo
    Replies: 2
    Last Post: January 14th, 01:00 AM
  2. Flag wave
    By Gianpiero Colagiacomo in forum Macromedia Director 3D
    Replies: 7
    Last Post: September 29th, 11:55 AM
  3. read-only flag
    By kielhd in forum PERL Miscellaneous
    Replies: 2
    Last Post: September 23rd, 11:14 AM
  4. Florida Flag==
    By David in forum Photography
    Replies: 0
    Last Post: July 16th, 11:23 AM
  5. Florida Flag..
    By GRC in forum Photography
    Replies: 0
    Last Post: July 7th, 08:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139