From: Chad on
Give the following....
[cdalten(a)localhost oakland]$ more hello.pl
sub hello() {
print "hello \n";
}

hello();

I change the file permissions to read/write and then run the program..

[cdalten(a)localhost oakland]$ ls -al hello.pl
-rw------- 1 cdalten cdalten 48 Jul 12 15:11 hello.pl
[cdalten(a)localhost oakland]$ perl hello.pl
hello
[cdalten(a)localhost oakland]$

I'm assuming the perl interpreter reads in the hello.pl file. The
question is, how does it run hello(); if the actual function
definition is above the function itself. Does it just backtrack along
the text file and then re-read the actual function definition itself?
From: Uri Guttman on
>>>>> "C" == Chad <cdalten(a)gmail.com> writes:

C> sub hello() {
C> print "hello \n";
C> }

C> hello();

C> I'm assuming the perl interpreter reads in the hello.pl file. The
C> question is, how does it run hello(); if the actual function
C> definition is above the function itself. Does it just backtrack along
C> the text file and then re-read the actual function definition itself?

perl first compiles the source code to an internal form and then runs
it. it isn't a true interpreter like the shell. note that if you didn't
call hello with () and it was called before its definition it would barf
an error. the () tells perl that you are calling a sub (unless the name
is a builtin). without the () it will think it is a bareword which isn't
legal under strict.

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Ben Morrow on

Quoth Chad <cdalten(a)gmail.com>:
> Give the following....
> [cdalten(a)localhost oakland]$ more hello.pl
> sub hello() {
> print "hello \n";
> }
>
> hello();
>
> I change the file permissions to read/write and then run the program..
>
> [cdalten(a)localhost oakland]$ ls -al hello.pl
> -rw------- 1 cdalten cdalten 48 Jul 12 15:11 hello.pl
> [cdalten(a)localhost oakland]$ perl hello.pl
> hello
> [cdalten(a)localhost oakland]$
>
> I'm assuming the perl interpreter reads in the hello.pl file. The
> question is, how does it run hello(); if the actual function
> definition is above the function itself. Does it just backtrack along
> the text file and then re-read the actual function definition itself?

Perl is a compiled language. It reads through the program once,
compiling it into something a little like Java bytecode; then it
executes that bytecode.

[Of course, this isn't necessary for the example you provide. POSIX
shell is an entirely interpreted language, but it still supports
functions. What it *doesn't* support, which Perl does, is code like

hello();

sub hello { warn "hello!" }

where the call comes before the function definition.]

The key difference from Java, of course, is that the bytecode is never
written out into a separate file, but always executed straight away.
Having the compiler and the virtual-machine in the same program in this
way makes it possible to implement string-eval and other forms of code
generation without losing the benefits of an optimising compilation
stage.

Ben

From: Tad McClellan on
Chad <cdalten(a)gmail.com> wrote:

> Give the following....
> [cdalten(a)localhost oakland]$ more hello.pl
> sub hello() {
^^
^^

Prototypes in Perl should almost always be avoided. Use

sub hello {

until you have enough experience to know whether or not
you really do need to use a prototype.


> I'm assuming the perl interpreter reads in the hello.pl file.


Why assume?

Why not just read the standard docs about how Perl programs are run?

perldoc perlrun

After locating your program, Perl compiles the entire program to an
internal form.


>The
> question is, how does it run hello(); if the actual function
> definition is above the function itself.


Because is has compiled the entire program to an internal form.
(ie. it consults the internal form)


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.