in reply to better cli processing

It may be worth pointing out that if you can live with putting = signs between your switches and their values:

script -f=file -s=size -t=time

instead if what you showed, you don't need to use a module or do any parsing yourself. Perl has this functionality built-in. Just add -s to your shebang under *nix or to the ftype definition under win32 and Perl will parse the values for you.

It has its limitations, but also its benefits, and its as cheap as it gets.

See perlrun for details.


Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

Replies are listed 'Best First'.
Re: Re: better cli processing
by AlCapone (Novice) on Nov 03, 2002 at 00:59 UTC
    ok, I have this
    #!/usr/bin/perl -s print $xyz if $xyz;
    which is shameless cut'n'paste from perldoc perlrun. Now, I'd *think* it works, but it doesn't. I dont get nothing whatsoever, no matter if I pass -xyz=abc or leave it out. The Almighty Manual Book says:

    The following program prints "1" if the program is invoked with a -xyz switch, and "abc" if it is invoked with -xyz=abc.

    But it doesn't. I tried on freebsd 4.4, linux 2.4.18. I even tested on winxp and nothing. And hey, if it doesn't work on _windows_ there have got to be something wrong with it! ;p

      Update2: I just noticed that you aren't printing a newline ("\n" or $/ (by default)) on the end of your print line. Is it possible that the line is getting buffered and not output somehow?

      Using this code

      #!perl -sw print $xyz,$/ if $xyz; __END__

      gives

      C:\test>210020 -xyz 1 C:\test>210020 -xyz=fred fred C:\test>

      This is on NT4/AS. NOTE though, the shebang line in my code is advisory only. The actual work being done by the ftype definition which I have set up like this

      C:\test>assoc .pl .pl=perl_script C:\test>ftype perl_script perl_script=e:\perl\bin\perl.exe -sw "%1" %* C:\test>

      I haven't used Perl under linux so I'm only guessing here. Have you tried doing

      /usr/bin/perl -s yourscript -xyz on your shell command line?

      I'm speculating that your shell is not respecting options supplied on the shebang line.


      There is more to this as well, once you get this to work in your environment(s), but the rest of this post is irrelevant if you can't get your test script to run.

      If you want to use -s in conjunction with strict, you need to take some extra step as the variables set my -s are globals which aren't use strict; complient.

      The fix is to this is to use vars qw/$xyz $file/; I'll dig out a short script showing you how I use the -s. Not that I necessarially do it correctly, but it works for me:)

      Update: Added a better example of using -s


      Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
        Hey, you were right! My winxp and freebsd don't care about -ws in shebang, so instead I had to "perl -ws file.pl -s=15" and it worked like charm! Thanks a lot, now I have to figure out why in the world it didn't work in shebang and how often crap like that happens, as I'm probably going to distribute my little app and I want to be sure it works...