Is Perl breaking convention because it has no character arrays, just strings?

S

sln

I'm a stronger C++ programmer than Perl.

On another thread posted recently, someone was having a
problem with "sysread", passing in the correct buffer
variable. He assigned an intermediate variable to hold the
correct variable to pass in to sysread.

I was a little taken aback, not by this confusion, but by
the fact that Perl's "sysread" (and all the reads) take a
parameter that is a pure scalar variable.

This seemed kind of weird to me since to populate an external
buffer from within a user subroutine, a reference has to be passed in.

Apparently, this doesen't apply to Perl's builtin functions.

I started thinking about C conventions.
In C, there is a concept of "char a[10];", a character array, which
isin't present in Perl, the language, constuct's.
Conceptually, in C, the core language, it has always been pass by value
or pass by reference (or pointer).
In C, this applies to language core lib calls AND user defined function
calls (I say 'C' but "C++" is no different).

I understand that the smallest data unit is SCALAR, however what I can't
understand this distinction:

In C, you can't pass the address of a character to a function (system or user)
that isn't in a character array. char a[10]; function(a,,,) is really
function(&a[0],,,), the "address of a" is passed. Where char a; function(a,,,,),
the "value in a" is passed.
On the other hand, passing a reference of "a" to a function is assignable
within the function.

So what I find dificult to concieve of is why, or how, Perl can take a seeming
"pass by value" in any other function call, and turn it into an assignable
address (or reference) in a Perl function call without having to actually
pass in a reference to the buffer.

My only guess is that Perl parses and interprets its own call's different
than subroutine calls.

Thats my best guess, and its just a guess.

sln
("Don't taze me bro!")
 
X

xhoster

I'm a stronger C++ programmer than Perl.

On another thread posted recently, someone was having a
problem with "sysread", passing in the correct buffer
variable. He assigned an intermediate variable to hold the
correct variable to pass in to sysread.

I was a little taken aback, not by this confusion, but by
the fact that Perl's "sysread" (and all the reads) take a
parameter that is a pure scalar variable.

This seemed kind of weird to me since to populate an external
buffer from within a user subroutine, a reference has to be passed in.

Apparently, this doesen't apply to Perl's builtin functions.

Nor does it apply to user-defined functions.

my $x="foo";
foo($x);
print $x;
sub foo{ $_[0]="bar"};
__END__
bar

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
J

Joost Diepenmaat

I'm a stronger C++ programmer than Perl.

On another thread posted recently, someone was having a
problem with "sysread", passing in the correct buffer
variable. He assigned an intermediate variable to hold the
correct variable to pass in to sysread.

I was a little taken aback, not by this confusion, but by
the fact that Perl's "sysread" (and all the reads) take a
parameter that is a pure scalar variable.

That's just syntactic sugar.
This seemed kind of weird to me since to populate an external
buffer from within a user subroutine, a reference has to be passed in.

Yeah. really, don't go down that road - just look at this as one of
the particularities that perl *just* *has*. If you don't believe me:

http://www.perl.com/pub/a/language/misc/fmproto.html
Apparently, this doesen't apply to Perl's builtin functions.

sysread *is* is builtin function. if you really want to: read up on
prototypes in perl.
I started thinking about C conventions.

Please don't go there.
In C, there is a concept of "char a[10];", a character array, which
isin't present in Perl, the language, constuct's.
Conceptually, in C, the core language, it has always been pass by value
or pass by reference (or pointer).
In C, this applies to language core lib calls AND user defined function
calls (I say 'C' but "C++" is no different).

I understand that the smallest data unit is SCALAR, however what I can't
understand this distinction:

In C, you can't pass the address of a character to a function (system or user)
that isn't in a character array. char a[10]; function(a,,,) is really
function(&a[0],,,), the "address of a" is passed. Where char a; function(a,,,,),
the "value in a" is passed.
On the other hand, passing a reference of "a" to a function is assignable
within the function.

So what I find dificult to concieve of is why, or how, Perl can take a seeming
"pass by value" in any other function call, and turn it into an assignable
address (or reference) in a Perl function call without having to actually
pass in a reference to the buffer.

My only guess is that Perl parses and interprets its own call's different
than subroutine calls.

Basically forget about C style prototypes when dealing with
perl. Perl's prototype deal with coercion, C's prototypes do not.

http://www.perlmonks.org/?node_id=447298
 
S

Sherm Pendley

So what I find dificult to concieve of is why, or how, Perl can take
a seeming "pass by value" in any other function call, and turn it
into an assignable address (or reference) in a Perl function call
without having to actually pass in a reference to the buffer.

My only guess is that Perl parses and interprets its own call's
different than subroutine calls.

You can make any subroutine (but not a method) act the same way by
supplying a prototype. Suppose you wanted to declare your own
equivalent to the built-in push() function:

sub mypush (\@@) {
my $arr = shift;
push @$arr, @_;
}

You'd call it like this:

mypush @foo, 'bar', 'baz';

Note that you don't need to take the reference to @foo - that's what
the \ in the prototype does. It means that argument will be passed by
reference, and Perl will automatically create the reference.

Have a look at the "Prototypes" section in "perldoc perlsub" for
details.

sherm--
 
S

sln

You can make any subroutine (but not a method) act the same way by
supplying a prototype. Suppose you wanted to declare your own
equivalent to the built-in push() function:

sub mypush (\@@) {
my $arr = shift;
push @$arr, @_;
}

You'd call it like this:

mypush @foo, 'bar', 'baz';

Note that you don't need to take the reference to @foo - that's what
the \ in the prototype does. It means that argument will be passed by
reference, and Perl will automatically create the reference.

Have a look at the "Prototypes" section in "perldoc perlsub" for
details.

sherm--

Hey thanks, I'm going to look into prototypes soon.
I did asume a prototype, like in your example, worked that way.

sln
 
S

sln

I'm a stronger C++ programmer than Perl.

On another thread posted recently, someone was having a
problem with "sysread", passing in the correct buffer
variable. He assigned an intermediate variable to hold the
correct variable to pass in to sysread.

I was a little taken aback, not by this confusion, but by
the fact that Perl's "sysread" (and all the reads) take a
parameter that is a pure scalar variable.

This seemed kind of weird to me since to populate an external
buffer from within a user subroutine, a reference has to be passed in.

Apparently, this doesen't apply to Perl's builtin functions.

Nor does it apply to user-defined functions.

my $x="foo";
foo($x);
print $x;
sub foo{ $_[0]="bar"};
__END__
bar

Xho

Interresting.
I didn't realize every value passed in was a reference.
What happens when you pass in a reference?

sln
 
S

sln

I'm a stronger C++ programmer than Perl.

On another thread posted recently, someone was having a
problem with "sysread", passing in the correct buffer
variable. He assigned an intermediate variable to hold the
correct variable to pass in to sysread.

I was a little taken aback, not by this confusion, but by
the fact that Perl's "sysread" (and all the reads) take a
parameter that is a pure scalar variable.

This seemed kind of weird to me since to populate an external
buffer from within a user subroutine, a reference has to be passed in.

Apparently, this doesen't apply to Perl's builtin functions.

Nor does it apply to user-defined functions.

my $x="foo";
foo($x);
print $x;
sub foo{ $_[0]="bar"};
__END__
bar

Xho

Oh, ok, $_[0] is an alias for $x.
When is it a reference?
Does the alias turn into a value after it is asigned?
Kindof spooky isn't it?

sln
 
S

sln

I'm a stronger C++ programmer than Perl.

On another thread posted recently, someone was having a
problem with "sysread", passing in the correct buffer
variable. He assigned an intermediate variable to hold the
correct variable to pass in to sysread.

I was a little taken aback, not by this confusion, but by
the fact that Perl's "sysread" (and all the reads) take a
parameter that is a pure scalar variable.

That's just syntactic sugar.
This seemed kind of weird to me since to populate an external
buffer from within a user subroutine, a reference has to be passed in.

Yeah. really, don't go down that road - just look at this as one of
the particularities that perl *just* *has*. If you don't believe me:

http://www.perl.com/pub/a/language/misc/fmproto.html
Apparently, this doesen't apply to Perl's builtin functions.

sysread *is* is builtin function. if you really want to: read up on
prototypes in perl.
I started thinking about C conventions.

Please don't go there.
In C, there is a concept of "char a[10];", a character array, which
isin't present in Perl, the language, constuct's.
Conceptually, in C, the core language, it has always been pass by value
or pass by reference (or pointer).
In C, this applies to language core lib calls AND user defined function
calls (I say 'C' but "C++" is no different).

I understand that the smallest data unit is SCALAR, however what I can't
understand this distinction:

In C, you can't pass the address of a character to a function (system or user)
that isn't in a character array. char a[10]; function(a,,,) is really
function(&a[0],,,), the "address of a" is passed. Where char a; function(a,,,,),
the "value in a" is passed.
On the other hand, passing a reference of "a" to a function is assignable
within the function.

So what I find dificult to concieve of is why, or how, Perl can take a seeming
"pass by value" in any other function call, and turn it into an assignable
address (or reference) in a Perl function call without having to actually
pass in a reference to the buffer.

My only guess is that Perl parses and interprets its own call's different
than subroutine calls.

Basically forget about C style prototypes when dealing with
perl. Perl's prototype deal with coercion, C's prototypes do not.

http://www.perlmonks.org/?node_id=447298

Well, Sherm Pedly has enlightened me on prototypes, so I guess the
paradox stands in neutral for now. But for the life of me, I can't find
any Perl prototypes for built-ins. Didn't see clear documentation that
indeed sysread is prototyped to take a reference.

The funny part is there was no mention of a prototype that will take a
reference.

Thanks!

sln
 
S

sln

I'm a stronger C++ programmer than Perl.

On another thread posted recently, someone was having a
problem with "sysread", passing in the correct buffer
variable. He assigned an intermediate variable to hold the
correct variable to pass in to sysread.

I was a little taken aback, not by this confusion, but by
the fact that Perl's "sysread" (and all the reads) take a
parameter that is a pure scalar variable.

That's just syntactic sugar.
This seemed kind of weird to me since to populate an external
buffer from within a user subroutine, a reference has to be passed in.

Yeah. really, don't go down that road - just look at this as one of
the particularities that perl *just* *has*. If you don't believe me:

http://www.perl.com/pub/a/language/misc/fmproto.html
Apparently, this doesen't apply to Perl's builtin functions.

sysread *is* is builtin function. if you really want to: read up on
prototypes in perl.
I started thinking about C conventions.

Please don't go there.
In C, there is a concept of "char a[10];", a character array, which
isin't present in Perl, the language, constuct's.
Conceptually, in C, the core language, it has always been pass by value
or pass by reference (or pointer).
In C, this applies to language core lib calls AND user defined function
calls (I say 'C' but "C++" is no different).

I understand that the smallest data unit is SCALAR, however what I can't
understand this distinction:

In C, you can't pass the address of a character to a function (system or user)
that isn't in a character array. char a[10]; function(a,,,) is really
function(&a[0],,,), the "address of a" is passed. Where char a; function(a,,,,),
the "value in a" is passed.
On the other hand, passing a reference of "a" to a function is assignable
within the function.

So what I find dificult to concieve of is why, or how, Perl can take a seeming
"pass by value" in any other function call, and turn it into an assignable
address (or reference) in a Perl function call without having to actually
pass in a reference to the buffer.

My only guess is that Perl parses and interprets its own call's different
than subroutine calls.

Basically forget about C style prototypes when dealing with
perl. Perl's prototype deal with coercion, C's prototypes do not.

http://www.perlmonks.org/?node_id=447298

Well, Sherm Pedly has enlightened me on prototypes, so I guess the
paradox stands in neutral for now. But for the life of me, I can't find
any Perl prototypes for built-ins. Didn't see clear documentation that
indeed sysread is prototyped to take a reference.

The funny part is there was no mention of a prototype that will take a
reference.
I mean to coerce a reference........
Thanks!

sln

sln
 
X

xhoster

This seemed kind of weird to me since to populate an external
buffer from within a user subroutine, a reference has to be passed in.

Apparently, this doesen't apply to Perl's builtin functions.

Nor does it apply to user-defined functions.

my $x="foo";
foo($x);
print $x;
sub foo{ $_[0]="bar"};
__END__
bar

Xho

Oh, ok, $_[0] is an alias for $x.
When is it a reference?

If $x contains a reference, then $_[0] contains a reference.

If you take a reference to $_[0], then you get a reference
to $x.
Does the alias turn into a value after it is asigned?

That doesn't make much sense to me. If you do
my $foo=$_[0];

Then $foo holds a value, just as it would if $x were in scope and you did
my $foo=$x;

$_[0] remains an alias for $x. Assigning *from* it hasn't changed what it
is.
Kindof spooky isn't it?

Not to me. Not anymore, anyway.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
M

Michael Carman

my $x="foo";
foo($x);
print $x;
sub foo{ $_[0]="bar"};
__END__
bar

Oh, ok, $_[0] is an alias for $x.
Correct.

When is it a reference?

In this example, never. As others have shown, you can use prototypes to
get implicit pass-by-reference behavior.
Does the alias turn into a value after it is asigned?

To get pass-by-value semantics you need make a copy of the data in @_

my $x = shift;
my ($y, @z) = @_;

This is the standard idiom in Perl, so in practice most programmers get
pass-by-value behavior even if they don't understand why. The aliasing
is awkward enough to use that you're unlikely to do so accidentally.
Kindof spooky isn't it?

It's a little odd at first but it's consistent with Perl's behavior in
other contexts (like iterating over an array).

-mjc
 
J

Jürgen Exner

I started thinking about C conventions.
In C, there is a concept of "char a[10];", a character array, which

More precisely it's nothing but a chunk of memory with all the
associated shortcomings and problems.
I understand that the smallest data unit is SCALAR, however what I can't
understand this distinction:

In C, you can't pass the address of a character to a function (system or user)
that isn't in a character array. char a[10]; function(a,,,) is really
function(&a[0],,,), the "address of a" is passed. Where char a; function(a,,,,),
the "value in a" is passed.
On the other hand, passing a reference of "a" to a function is assignable
within the function.

Well, C is a pretty low-level programming language with _A_LOT_ of
quirks and issues, many of them caused by being too close to assembler
or the attempt to give the programmer too detailed control.
So what I find dificult to concieve of is why, or how, Perl can take a seeming
"pass by value" in any other function call, and turn it into an assignable
address (or reference) in a Perl function call without having to actually
pass in a reference to the buffer.

My only guess is that Perl parses and interprets its own call's different
than subroutine calls.

Are you talking about C subroutine calls? Or course they are different!
Perl has and maintains its own data structures.
I suggest you take some classes in compiler design. Many high-level
compilers use C as the target language and typically it is quite some
effort to map logical data structures into C's primitive concept of
memory chunks.

jue
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top