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

My problem is that I am trying to use -F command line switch on the #! line but it does not look like it follows the docs in perlrun.

According to my copy of perlrun -F has this description

-Fpattern
      specifies the pattern to split on if -a is also in
      effect.  The pattern may be surrounded by "//", """",
      or "''", otherwise it will be put in single quotes.
If I put the code below in a file and run it like this perl test.pl
#!/usr/bin/perl -apnF"monkey" use Data::Dumper; print Data::Dumper->Dump([\@F]);
I get in perl version 5.6.1 on linux this error message

"use" not allowed in expression at test.pl line 2, at end of line
syntax error at test.pl line 2, near "use Data::Dumper"
Execution of test.pl aborted due to compilation errors.

When I got this error I changed the first line into this
#!/usr/bin/perl -apnF "monkey"

The code runs and if you type 'my monkey' @F does not have 2 elements in it as expected. Instead Data::Dumper prints out this

$VAR1 = [ 'my monkey ' ];
Then I changed the first line into this #!/usr/bin/perl -apn

and run the program like this
 perl -F"monkey" test.pl
This gave me this output from Data::Dumper.

$VAR1 = [ 'my ', ' ' ];
What am I doing wrong?
--
Pug

Replies are listed 'Best First'.
Re: Having an issue with -F
by hv (Prior) on Feb 19, 2003 at 23:46 UTC

    The first problem appears to be a bug in 5.6.1, already fixed in 5.8.0.

    I think the second problem is just more of the first - I don't think 5.6.1 is handling the -F on the #! line correctly.

    With 5.8.0, the #! version gives the same result as the command-line version.

    Note that you don't seem to be allowed to separate the argument to -F into a separate argument:

    #!/usr/bin/perl -apnF "monkey"
    failed even under 5.8.0.

    Hugo
      Thanks Hugo. I am not surpised that the case of
      #!/usr/bin/perl -apnF "monkey"
      does not work.. It doesn't work on the command line either.
      perl complians that it can't open perl script "monkey": No such file or directory.
      --
      Pug
Re: Having an issue with -F
by dragonchild (Archbishop) on Feb 19, 2003 at 23:31 UTC
    I don't know why the first few cases are messed up. The last case is correct, though. You asked it to split the string on the delimiter 'monkey'. That means the first part is "my " and the second part is "\n".

    Try splitting on the delimiter ' '. You also might want to chomp somewhere in there. (I don't remember if chomping is done for you with some cmd-line switch.) :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Sorry I thought I made it clear. The last case is exectly what I am looking for.
      --
      Pug
Re: Having an issue with -F
by Limbic~Region (Chancellor) on Feb 19, 2003 at 23:58 UTC
    Pug,
    As hv alluded to, the problem with using the -F on the shebang line is fixed in 5.8.0 (other problems aside).

    See the delta file for more information, but this is what you are looking for:

    The command-line options -s and -F are now recognized on the shebang (#!) line.

    Happy Hacking - L~R