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

Discussion in 'Perl Misc' started by sln@netherlands.com, Aug 25, 2008.

  1. Guest

    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!")
    , Aug 25, 2008
    #1
    1. Advertising

  2. Guest

    wrote:
    > 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.
    , Aug 25, 2008
    #2
    1. Advertising

  3. writes:

    > 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


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Aug 25, 2008
    #3
  4. writes:

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

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Sherm Pendley, Aug 25, 2008
    #4
  5. Guest

    On Sun, 24 Aug 2008 20:38:06 -0400, Sherm Pendley <> wrote:

    > writes:
    >
    >> 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--


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

    sln
    , Aug 25, 2008
    #5
  6. Guest

    On 25 Aug 2008 00:17:03 GMT, wrote:

    > wrote:
    >> 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
    , Aug 25, 2008
    #6
  7. Guest

    On 25 Aug 2008 00:17:03 GMT, wrote:

    > wrote:
    >> 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
    , Aug 25, 2008
    #7
  8. Guest

    On Mon, 25 Aug 2008 02:28:58 +0200, Joost Diepenmaat <> wrote:

    > writes:
    >
    >> 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
    , Aug 25, 2008
    #8
  9. Guest

    On Mon, 25 Aug 2008 01:21:18 GMT, wrote:

    >On Mon, 25 Aug 2008 02:28:58 +0200, Joost Diepenmaat <> wrote:
    >
    >> writes:
    >>
    >>> 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
    , Aug 25, 2008
    #9
  10. Guest

    wrote:
    > On 25 Aug 2008 00:17:03 GMT, wrote:
    > > wrote:
    > >>
    > >> 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.
    , Aug 25, 2008
    #10
  11. Re: Is Perl breaking convention because it has no character arrays,just strings?

    wrote:
    > On 25 Aug 2008 00:17:03 GMT, wrote:
    >> 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
    Michael Carman, Aug 25, 2008
    #11
  12. wrote:
    >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
    Jürgen Exner, Aug 25, 2008
    #12
    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. Amy
    Replies:
    0
    Views:
    501
  2. James
    Replies:
    2
    Views:
    9,077
    Jacek Dziedzic
    Nov 3, 2004
  3. Christian Blackburn
    Replies:
    8
    Views:
    440
    Christian Blackburn
    Jun 21, 2006
  4. Mike
    Replies:
    2
    Views:
    189
  5. Lew
    Replies:
    0
    Views:
    416
Loading...

Share This Page