Re: Perl on a Mac

Discussion in 'Perl Misc' started by Jürgen Exner, Apr 22, 2012.

  1. Torsten Jørgensen <> wrote:
    >Does it understand #!/usr/bin/perl.


    No, it doesn't. It doesn't on any OS because the shebang line is a
    function of the shell, not of Perl. Perl doesn't care about the shebang
    line(*).

    >Can the Terminal run shell scripts.


    Why not?

    *: actually there are hacks for some OSs where Perl will parse arguments
    from the shebang line because the command interpreter on those OSs does
    not recognize the shebang line.

    jue
    Jürgen Exner, Apr 22, 2012
    #1
    1. Advertising

  2. Jürgen Exner

    Tim McDaniel Guest

    In article <>,
    J_rgen Exner <> wrote:
    >Torsten J_rgensen <> wrote:
    >>Does it understand #!/usr/bin/perl.

    >
    >No, it doesn't. It doesn't on any OS because the shebang line is a
    >function of the shell, not of Perl.


    That happens not to be the case. In UNIX and UNIX derivatives, the
    system detects when the first two characters of the file are "#!".
    "man 2 execve" on Ubuntu (one version back), where "2" means it's a
    system call:

    execve() executes the program pointed to by filename. filename
    must be either a binary executable, or a script starting with a
    line of the form:

    #! interpreter [optional-arg]

    For details of the latter case, see "Interpreter scripts" below.

    <http://kernel.org/doc/man-pages/online/pages/man2/execve.2.html> has
    a Linux man page, including implementations details and how different
    systems differ in their implementations of it.

    Below is an example program showing a binary (100, compiled from
    100.c) executing a script (100.cat) without any shell being involved.

    In the 1980s, if you were using csh and a file began with "#" (not
    "#!), then csh would interpret it as a csh script instead of calling
    the kernel. That is no longer the case on current Linux or NetBSD
    5.1.2, the two systems I can check conveniently. There, the correct
    interpreter (/bin/cat, if I re-use the example script below) gets
    called, though I can't easily test to see whether csh caused it or the
    kernel.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    $ cat 100.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    int main(int argc, char *argv[])
    {
    char *newargv[] = { "100.cat", (char *) NULL };
    char *newenviron[] = { (char *) NULL };

    execve("100.cat", newargv, newenviron);
    perror("execve"); /* execve() only returns on error */
    exit(EXIT_FAILURE);
    }
    $ cat 100.cat
    #! /bin/cat
    Hello, world!
    $ cc -o 100 100.c
    $ chmod u+x 100.cat
    $ ./100
    #! /bin/cat
    Hello, world!
    Tim McDaniel, Apr 22, 2012
    #2
    1. Advertising

  3. Jürgen Exner

    Dr.Ruud Guest

    On 2012-04-22 06:08, Torsten Jørgensen wrote:

    > And by the way, keep your inuendo to yourself, it is insulting.


    A big lesson that many usenet users still need to learn, again and
    again, is that you are what you read.
    If you feel that something you read, annoys you, then realize that you
    were already annoyed before you started reading it.

    In am not interested in people that show behavior like Torsten just did,
    with the exception of close family. For usenet that is easy to express,
    I just create a filter and he's being plonked. I don't actually have a
    plonk sound for every plonk that my filters do, I prefer the silence.

    --
    Ruud
    Dr.Ruud, Apr 22, 2012
    #3
  4. Torsten Jørgensen <> wrote:
    >On 2012-04-22 03:51:59 +0000, Jürgen Exner said:
    >
    >> Torsten Jørgensen <> wrote:
    >>> Does it understand #!/usr/bin/perl.

    >>
    >> No, it doesn't. It doesn't on any OS because the shebang line is a
    >> function of the shell, not of Perl. Perl doesn't care about the shebang
    >> line(*).

    >
    >I know, I meant the shell...


    Ok.
    Sorry, I can only read what you are writing, not what you are thinking.

    >>> Can the Terminal run shell scripts.

    >>
    >> Why not?

    >
    >Except that it comes out with -bash: seek.pl: command not found
    >when I try to run this:
    >
    >#!/usr/bin/perl
    >print "Hello World";


    Then I would guess there is no executable program named perl at
    /usr/bin. Maybe it is not installed? Maybe it is in a different
    location? Maybe access rights are incorrect?

    >> *: actually there are hacks for some OSs where Perl will parse arguments
    >> from the shebang line because the command interpreter on those OSs does
    >> not recognize the shebang line.

    >
    >But OSX Lion does...


    That's fine. I was merely mentioning that on certain OSs there are
    exceptions to the standard behaviour of Perl.

    >And by the way, keep your inuendo to yourself, it is insulting.


    -v, please?

    jue
    Jürgen Exner, Apr 22, 2012
    #4
  5. Torsten Jørgensen <> wrote:
    > On 2012-04-22 03:51:59 +0000, Jürgen Exner said:
    > > Torsten Jørgensen <> wrote:
    > >> Does it understand #!/usr/bin/perl.

    > >
    > > No, it doesn't. It doesn't on any OS because the shebang line is a
    > > function of the shell, not of Perl. Perl doesn't care about the shebang
    > > line(*).


    > I know, I meant the shell...


    > >> Can the Terminal run shell scripts.

    > >
    > > Why not?


    > Except that it comes out with -bash: seek.pl: command not found
    > when I try to run this:


    I've no experience with Macs but I heard that their OS is based
    on BSD. And if that's the case you may have to put "./" in front
    of the scripts name when you try to run it from the shell, i.e.
    use

    ../seek.pl

    since the current directory isn't necessarily in the paths the
    shell looks for programs in. And, of course, to get it to work,
    you also need the "execute" flag set for the script, i.e. you
    may have to set the execute permission with a command like

    chmod +x seek.pl

    Without the execute flag being set the "#!" won't do anything.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Apr 22, 2012
    #5
  6. Jürgen Exner

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Perl always parses arguments from the #! line, on all OSs. Even on OSs
    BM> which understand #!, the kernel always passes the whole of the rest of
    BM> that line (up to a fixed limit) as one argument, and on some OSs that
    BM> limit was rather small. Perl goes to some trouble to sort out the
    BM> resulting mess so you can apparently put several arguments on the #!
    BM> line and it will Just Work.

    perl gets the #! just by reading the source file it is passed. it
    doesn't need any help from the kernel to see it all.

    uri
    Uri Guttman, Apr 22, 2012
    #6
  7. Jürgen Exner

    Tim McDaniel Guest

    In article <>,
    J_rgen Exner <> wrote:
    >Torsten J_rgensen <> wrote:
    >>Except that it comes out with -bash: seek.pl: command not found
    >>when I try to run this:
    >>
    >>#!/usr/bin/perl
    >>print "Hello World";

    >
    >Then I would guess there is no executable program named perl at
    >/usr/bin. Maybe it is not installed? Maybe it is in a different
    >location? Maybe access rights are incorrect?


    "type perl" or "which perl" may say where it is. For example, on my
    ISP,

    $ type -a perl
    perl is /usr/local/bin/perl

    But other systems may differ.

    --
    Tim McDaniel,
    Tim McDaniel, Apr 22, 2012
    #7
  8. Jürgen Exner

    Tim McDaniel Guest

    In article <>,
    Ben Morrow <> wrote:
    >
    >Quoth :
    >> In article <>,
    >> J_rgen Exner <> wrote:
    >> >Torsten J_rgensen <> wrote:
    >> >>Does it understand #!/usr/bin/perl.
    >> >
    >> >No, it doesn't. It doesn't on any OS because the shebang line is a
    >> >function of the shell, not of Perl.

    >>
    >> That happens not to be the case. In UNIX and UNIX derivatives,

    >
    >The kernel implementation of #! is a BSD thing, though I believe it
    >made its way into all the SysVs eventually.


    Regardless of the history, I have not heard of any extant current UNIX
    derivative that doesn't have it. Linux certainly does.

    And to quibble: technically no, but effectively yes.

    http://www.in-ulm.de/~mascheck/various/shebang/
    The #! magic, details about the shebang/hash-bang mechanism on
    various Unix flavours
    2011-12-27
    has a history that says it was done at Bell Labs after Version 7,
    announced by Dennis Richie, but BSD 4 (not BSD 2) was the first wide
    distribution to pick it up. "Among the public releases from Bell
    Labs, #! was not added until SVR4 ('88) according to a TUHS list
    discussion. System III and SVR1 definitely had not implemented it,
    yet."

    It links to http://homepages.cwi.nl/~aeb/std/hashexclam.html
    #! - the Unix truth as far as I know it.
    Andries Brouwer,
    v1.2, 2002-12-05
    , which gives more details about changes and implementations.

    --
    Tim McDaniel,
    Tim McDaniel, Apr 22, 2012
    #8
  9. Jürgen Exner

    Tim McDaniel Guest

    In article <>,
    Ben Morrow <> wrote:
    >Even on OSs which understand #!, the kernel always passes the whole
    >of the rest of that line (up to a fixed limit) as one argument


    http://homepages.cwi.nl/~aeb/std/hashexclam-1.html#ss1.3 says that
    there's lots of variations on that, though I find it confusing because
    he uses the present tense to refer to long-obsolete UNIX variations.

    - Most systems do that.
    Some split on whitespace and pass multiple arguments.
    Some split on whitespace and ignore all but the first.
    One line of development treated # as a comment character, but later
    yanked it out.
    - Systems have various limits, but "BSD/OS 4.2 does not truncate".
    - Some delete trailing whitespace, some do not.
    - Some systems (Linux) keep trailing \r as part of the argument.
    Others (Solaris) strip it too.
    - Systems differ on what arg0 that they will pass.
    - Some systems do not allow a script to be on the #! line.

    In short, to go by that, you can portably depend on 0 or 1 argument
    if there's no whitespace in it or after it, no \r, and less than 80
    bytes on modern systems.

    --
    Tim McDaniel,
    Tim McDaniel, Apr 22, 2012
    #9
  10. Jürgen Exner

    Hans Mulder Guest

    On 22/04/12 19:27:38, Uri Guttman wrote:
    >>>>>> "BM" == Ben Morrow <> writes:

    >
    > BM> Perl always parses arguments from the #! line, on all OSs. Even on OSs
    > BM> which understand #!, the kernel always passes the whole of the rest of
    > BM> that line (up to a fixed limit) as one argument, and on some OSs that
    > BM> limit was rather small. Perl goes to some trouble to sort out the
    > BM> resulting mess so you can apparently put several arguments on the #!
    > BM> line and it will Just Work.


    > perl gets the #! just by reading the source file it is passed. it
    > doesn't need any help from the kernel to see it all.


    One exception to this, is the '-T' switch. If you let the kernel process
    the shebang line, then it will pass the -T on the command line and perl
    will run in taint mode. It perl only finds the -T when it parses the
    first line of the source file, then it's too late and perl will abort:

    $ cat demo.pl
    #!/usr/bin/perl -T
    print "Hello, world\n";
    $ ./demo.pl
    Hello, world
    $ perl demo.pl
    "-T" is on the #! line, it must also be used on the command line at
    demo.pl line 1.
    $ perl -T demo.pl
    Hello, world
    $


    -- HansM
    Hans Mulder, Apr 22, 2012
    #10
  11. Hacker <> wrote:
    >On 2012-04-22 11:18:42 +0000, Jürgen Exner said:
    >> Torsten Jørgensen <> wrote:


    >> Sorry, I can only read what you are writing, not what you are thinking.

    >
    >Piss off.


    Thank you for confirming that "Torsten Jørgensen" had to change his ID
    after only 3 postings. That must be close to a new record.

    jue
    Jürgen Exner, Apr 24, 2012
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,767
  2. Roman =?ISO-8859-15?Q?Bl=F6th?=

    Senseless rendering: Mac.Mozilla != Mac.Netscape6.01 ?!?!

    Roman =?ISO-8859-15?Q?Bl=F6th?=, Jul 2, 2003, in forum: HTML
    Replies:
    1
    Views:
    988
    Steve Pugh
    Jul 2, 2003
  3. mangm
    Replies:
    2
    Views:
    731
    mangm
    Dec 1, 2005
  4. K Richard Pixley
    Replies:
    3
    Views:
    332
    Ned Deily
    Jan 3, 2012
  5. Jake Wiley
    Replies:
    14
    Views:
    283
Loading...

Share This Page