in reply to Re: better cli processing
in thread better cli processing

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

Replies are listed 'Best First'.
Re: Re: Re: better cli processing
by BrowserUk (Patriarch) on Nov 03, 2002 at 01:17 UTC

    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...

        This behaviour on Windows is easy to explain: the interpreter is invoked by looking up what program to call for file type .pl. It is then invoked with the parameters from the registry entry. Whatever you put on the shebang line (or if there is one at all) has no effect.

        Why FreeBSD would behave this way also is puzzling, though..

        Makeshifts last the longest.