Newbie questions, migrating from c++

Discussion in 'Perl Misc' started by gg500@lycos.com, Dec 12, 2004.

  1. Guest

    Hello,

    Few things puzzles me in perl. I'm trying to use warnings and strict
    and run with perl -w, but I get warnings which I do not understand. For
    example I have to use "use Fcntl qw:)DEFAULT :seek);" in order to get
    no warnings using SEEK_SET etc, this phrase I copied got from net while
    not understanding what it means. Could you describe this statement?

    Is there some other useful things which I can set to make warnings as
    explanatory and abundant as possible? I mean something equal to gcc
    -Wall -pedantic.

    If I write "split /\./, $ENV(REMOTE_ADDR)" I get warning about using
    undefined variable, so without knowing how exactly I should proceed I
    have written abundant amount of lines like "defined $ENV{REMOTE_ADDR} ?
    $ENV{REMOTE_ADDR} : """. Obviously I do not understand the
    fundamentals, so could you please explain what I do wrong since I get
    myself into this situation?

    It must have taken me 10 hours to try to get information how to refer
    single byte (char) in "string/array", like in C: char a[5]; return
    a[2]; without realizing that's not how things are done in perl. Is this
    correct? So if I'm to do something with chars in string (or perl
    variable that is) I am to split the variable to @array first? Does this
    not impose performance problems in general?

    I'm also a bit confused with calling functions or "sub routines".
    Writing "&subroutine();" works but I do not understand what exactly is
    the difference when omitting "()" and/or "&"...?

    Also do I need to flock UN_LOCK before closing the file, or does close
    handle the unlocking automatically?
    , Dec 12, 2004
    #1
    1. Advertising

  2. Lots of different questions here - I'll answer a few of the ones I have
    answers for off the top of my head.

    wrote:

    > Few things puzzles me in perl. I'm trying to use warnings and strict
    > and run with perl -w, but I get warnings which I do not understand. For


    Yes, but you're halfway there - you're enabling strict and warnings, and
    making an effort to understand their output. That's a good habit to
    have, and it's far too rare.

    > example I have to use "use Fcntl qw:)DEFAULT :seek);" in order to get
    > no warnings using SEEK_SET etc, this phrase I copied got from net while
    > not understanding what it means. Could you describe this statement?


    Which part of it?

    qw() is a convenient way to declare an array by splitting some text on
    white space - the above is equivalent to "use Fcntl (':DEFAULT', ':seek');

    See "Quote and Quote-like Operators", in:

    perldoc perlop

    When you pass an array as the second argument to a use(), you're asking
    the Exporter module to export symbols from the module you're using into
    the current module's name space. If you didn't do that, you'd have to
    fully-qualify the package names of the functions you call, instead of
    being able to simply call them with the function name alone.

    There's more to it than that - for example, the ':DEFAULT' above is
    actually exporting a group of symbols, not a single symbol by that name.
    But that's the general idea of it.

    See also:

    perldoc -f use
    perldoc Exporter

    > Is there some other useful things which I can set to make warnings as
    > explanatory and abundant as possible? I mean something equal to gcc
    > -Wall -pedantic.


    use diagnostics;

    See also:

    perldoc diagnostics

    > If I write "split /\./, $ENV(REMOTE_ADDR)"


    Don't do that. Use CGI.pm instead.

    > I'm also a bit confused with calling functions or "sub routines".
    > Writing "&subroutine();" works but I do not understand what exactly is
    > the difference when omitting "()" and/or "&"...?


    See:

    perldoc perlsub

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Dec 12, 2004
    #2
    1. Advertising

  3. wrote in news:1102858622.622328.166600
    @c13g2000cwb.googlegroups.com:

    > Few things puzzles me in perl. I'm trying to use warnings and strict
    > and run with perl -w,


    use warnings;

    is preferable these days.

    > but I get warnings which I do not understand.


    Then this would be a great time to read the posting guidelines for this
    group and note

    use diagnostics;

    > For example I have to use "use Fcntl qw:)DEFAULT :seek);" in order
    > to get no warnings using SEEK_SET etc, this phrase I copied got
    > from net while not understanding what it means. Could you describe
    > this statement?


    I am not in the habit of explaining random statements from "the net".

    If

    use strict;

    is in effect, symbols need to be declared before being used. The Fcntl
    module declares and defines those symbols. the use statement above then
    imports them into your scripts namespace.

    > Is there some other useful things which I can set to make warnings as
    > explanatory and abundant as possible? I mean something equal to gcc
    > -Wall -pedantic.


    use diagnostics;

    > If I write "split /\./, $ENV(REMOTE_ADDR)" I get warning about using


    That is not how you access a hash element.

    > undefined variable, so without knowing how exactly I should proceed I
    > have written abundant amount of lines like "defined $ENV{REMOTE_ADDR} ?
    > $ENV{REMOTE_ADDR} : """.


    What is """ supposed to do?

    > It must have taken me 10 hours to try to get information how to refer
    > single byte (char) in "string/array", like in C: char a[5]; return
    > a[2]; without realizing that's not how things are done in perl. Is this
    > correct? So if I'm to do something with chars in string (or perl
    > variable that is) I am to split the variable to @array first? Does this
    > not impose performance problems in general?


    Please give an example where you think it is necessary to pretend a Perl
    scalar is a C char array, then we can comment on whether that is the
    right way in that particular case.

    > I'm also a bit confused with calling functions or "sub routines".
    > Writing "&subroutine();" works but I do not understand what exactly is
    > the difference when omitting "()" and/or "&"...?


    &subroutine() has specific effects which are explained in

    perldoc perlsub

    It is not a good idea to use this method of subroutine invocation unless
    you know these effects and specifically need them.

    You can invoke your subroutine without the parantheses under certain
    circumstances. For example:


    #! perl

    use strict;
    use warnings;

    sub no_need {
    print "Don't need no stinking parantheses to call me\n"
    }

    no_need;
    need();

    sub need {
    no_need;
    }

    __END__

    Again, please read the documentation first and ask for clarifications.

    > Also do I need to flock UN_LOCK before closing the file


    No.


    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
    A. Sinan Unur, Dec 12, 2004
    #3
  4. Paul Lalli Guest

    wrote:

    I'll comment on the one question Sherm did not...

    > It must have taken me 10 hours to try to get information how to refer
    > single byte (char) in "string/array", like in C: char a[5]; return
    > a[2]; without realizing that's not how things are done in perl. Is this
    > correct?


    Yes, in Perl, a string is a scalar datatype. A string is not created
    nor stored as an array of characters as it is in C++. This also means
    that there is no internal difference between:
    $str = 'a';
    and
    $str = 'hello world.';

    The only difference is that one string contains one character, while the
    other contains 12.

    > So if I'm to do something with chars in string (or perl
    > variable that is) I am to split the variable to @array first? Does this
    > not impose performance problems in general?


    If you really *need* to operate on one single character from a string,
    then yes, you generally split the string into an array of characters.
    And yes, this is probably inefficient.

    *However*, Perl contains so many features for dealing with strings as a
    whole (regular expressions, and the l-value return value of substr, for
    example), that is almost never necessary to operate on a specific
    character. If you have a situation in which you find yourself wanting
    to modify/read/delete single characters from an existing string, it's
    likely that you're either ignoring or unaware of a Perl feature that is
    better suited to your task. If you have one of these situations
    happening right now, feel free to post an example to this group, and
    someone will probably be able to show you a better way.

    Paul Lalli
    Paul Lalli, Dec 12, 2004
    #4
  5. Guest

    > Sherm Pendley wrote:
    > When you pass an array as the second argument to a use(), you're

    asking the Exporter module to export symbols from the module you're
    using into the
    > ...See also:
    > perldoc Exporter


    Thanks, this was helpful.

    >> If I write "split /\./,$ENV(REMOTE_ADDR)"

    > Don't do that. Use CGI.pm instead.


    It's not my intention to learn the libraries yet. I try to understand
    how to do this right.
    , Dec 12, 2004
    #5
  6. Guest

    A. Sinan Unur wrote:
    > Then this would be a great time to read the posting guidelines for

    this group and note

    Why bother because no matter how newbies put their questions, there are
    people like you who will point out things out of context what the
    poster got wrong. Typical usenet replies.

    > I am not in the habit of explaining random statements from "the net".


    ....

    >> If I write "split /\./, $ENV(REMOTE_ADDR)" I get warning about using

    > That is not how you access a hash element.


    ....

    >> have written abundant amount of lines like "defined

    $ENV{REMOTE_ADDR} ? $ENV{REMOTE_ADDR} : """.
    > What is """ supposed to do?


    You know perfectly well there was quotes around the whole piece of
    code. You are again increasing the usenet noise by stupid writing like
    this, which does not answer the question or do anything good for the
    group.

    > Please give an example where you think it is necessary to pretend a

    Perl scalar is a C char array, then we can comment on whether that is
    the right way in that particular case.

    Again, that has little to do with the question of confirming is there
    way to do this in perl. The answer was confirmed by other posters.
    Thanks for help, although 50% of your post was just plain arrogance.
    , Dec 12, 2004
    #6
  7. Sherm Pendley, Dec 12, 2004
    #7
  8. Guest

    Paul Lalli wrote:
    >> So if I'm to do something with chars in string (or perl
    >> variable that is) I am to split the variable to @array first? Does

    this
    >> not impose performance problems in general?

    >If you really *need* to operate on one single character from a string,


    > then yes, you generally split the string into an array of characters.


    > And yes, this is probably inefficient.
    > *However*, Perl contains so many features for dealing with strings as

    a
    > whole (regular expressions, and the l-value return value of substr,

    for
    > example), that is almost never necessary to operate on a specific
    > character. If you have a situation in which you find yourself wanting


    > to modify/read/delete single characters from an existing string, it's


    > likely that you're either ignoring or unaware of a Perl feature that

    is
    > better suited to your task. If you have one of these situations
    > happening right now, feel free to post an example to this group, and
    > someone will probably be able to show you a better way.


    Thanks for the reply. You are right that such single character
    modifications aren't probably necessary if one knows perl. I presumed
    this but it's difficult to master the regex and whole tools, and when I
    try to accomplish something for different ways and spent time reading
    references to do something simple, I end up trying to modify single
    characters. I originally got stuck while trying to the number of
    whitespaces in the beginning of string. Now I can think (out of my
    head) at least one way to do it, which is something like this:
    $i=0;while(/^ /){++$i;} and how i holds the number of spaces, but I
    didn't figure this back then.

    To give you an example what I still don't know how to do. I could think
    of some kind of simple keyed rotation (in C):
    for(i=0,j=strlen(s);i<j;++i)s=(s+key[i%keylen])%m;
    Note that I do not need exactly solution for the above, that is just an
    example of similar situations which I found myself in. I know this is
    mainly because I just have not used to the powerful features which are
    the core of perl and try to solve things in a low level way.
    , Dec 12, 2004
    #8
  9. Guest

    I do not understand. Are you saying that the situation is so unique
    that there is no other situation where I need to split entry from
    hashed array?
    , Dec 12, 2004
    #9
  10. wrote:

    > A. Sinan Unur wrote:
    >
    >>Then this would be a great time to read the posting guidelines for

    >
    > Why bother


    So you don't make a fool of yourself in public. So you don't find
    yourself in the killfiles of those who are best qualified to answer your
    questions.

    If nothing else, out of basic courtesy.

    > Typical usenet replies.


    If you consistently get nasty replies to your usenet posts, perhaps you
    should be asking yourself what you're doing to attract them.

    >>Please give an example where you think it is necessary to pretend a
    >>Perl scalar is a C char array, then we can comment on whether that is
    >>the right way in that particular case.

    >
    > Again, that has little to do with the question of confirming is there
    > way to do this in perl.


    Nonsense. While it's *possible* write Perl that emulates C's "char at a
    time" approach to string processing, it's rarely necessary. More often
    than not, there's an easier way to do the same job that treats the
    string as a single unit.

    For example, consider converting a string to uppercase. In C you'd loop
    through the array and examine in character in turn, converting it as
    necessary. In Perl you'd simply use the built-in uc() function.

    A. Sinan didn't give you noise, he gave you good advice. Arrogance is
    what you're giving him back, by flaming him in return for his help.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Dec 12, 2004
    #10
  11. Guest

    Just to correct myself, my space counting code does not work. But I
    tried this and it did work:
    $i=1;while (/^ {$i}/) {++$i;}--$i;
    But this didn't work:
    $i=0;while (/^ {$i+1}/) {++$i;}
    which needless to say I do not understand.
    , Dec 12, 2004
    #11
  12. Guest

    Sherm Pendley wrote:
    > A. Sinan didn't give you noise, he gave you good advice. Arrogance is

    what you're giving him back, by flaming him in return for his help.

    If you honestly didn't recognize his reply as arrogant then I suggest
    you take another look.
    , Dec 12, 2004
    #12
  13. wrote:

    > I do not understand. Are you saying that the situation is so unique
    > that there is no other situation where I need to split entry from
    > hashed array?


    Don't be absurd, I made no such gross over-generalization.

    In your example, you tried to read the environment variable REMOTE_ADDR.
    That implies that you're writing a CGI script and you want the remote
    host address. In that case, the normal way to get it is to call the
    remote_host() method in CGI.pm.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Dec 12, 2004
    #13
  14. On 12 Dec 2004 09:53:33 -0800, wrote:

    >Just to correct myself, my space counting code does not work. But I
    >tried this and it did work:
    >$i=1;while (/^ {$i}/) {++$i;}--$i;
    >But this didn't work:
    >$i=0;while (/^ {$i+1}/) {++$i;}
    >which needless to say I do not understand.


    use strict;
    use warnings:

    $_ = ' string with spaces first';
    s/^\s+//;
    print "$_\n";

    If you want to migrate from C++ to Perl, why don't you start reading
    up/learing those thing that are done more easily in Perl than C++
    first? Like (string-) variables and regexes and similar...?

    --
    mvh/Regards Kåre Olai Lindbach
    (News: Remove '_delete_' and '.invalid')
    (HTML-written email from unknown will be discarded)
    Kåre Olai Lindbach, Dec 12, 2004
    #14
  15. Guest

    Right, I plan to make full use of CGI.pm, this is not the point. If we
    take a look at the origin of this case I was referring to warnings got
    by undefined arguments perl says are undefined and I have to add these
    defined() conditions which seem like overhead. I asked about these
    warnings, and I'm not at all asking about some specific variable in my
    example.
    , Dec 12, 2004
    #15
  16. Guest

    Kåre Olai Lindbach wrote:
    > $_ = ' string with spaces first';
    > s/^\s+//;
    > print "$_\n";


    But the code you propose cuts the spaces from the beginning of the line
    and does not solve the problem. I was posing a problem which I needed
    to know how many spaces there were.

    > If you want to migrate from C++ to Perl, why don't you start reading
    > up/learing those thing that are done more easily in Perl than C++

    first? Like (string-) variables and regexes and similar...?

    A good suggestion, but unfortunately the lack of patience leads to
    wanting to learn all at once.
    , Dec 12, 2004
    #16
  17. wrote in
    news::

    > A. Sinan Unur wrote:


    >> Then this would be a great time to read the posting guidelines for
    >> this group and note

    >
    > Why bother because no matter how newbies put their questions,


    I am not sure what you are saying here. The answer to "Why bother reading
    the guidelines?" is that they mention the diagnostics package and assorted
    niceties involved in posting here:

    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html#posting_t
    o_comp.lang.perl.misc (http://tinyurl.com/466lp if you have problems
    opening that).

    >> I am not in the habit of explaining random statements from "the net".


    Exactly. There are good Perl books and Perl comes with excellent
    documentation which you would do well to read. It is natural that you would
    still have questions after having read the docs but then you would be in a
    position to ask better questions.

    >>> If I write "split /\./, $ENV(REMOTE_ADDR)" I get warning about using


    >> That is not how you access a hash element.


    Indeed, that is not how you access a hash element. We can only comment on
    what you wrote.

    >>> have written abundant amount of lines like "defined
    >>> $ENV{REMOTE_ADDR} ? $ENV{REMOTE_ADDR} : """.


    >> What is """ supposed to do?

    >
    > You know perfectly well there was quotes around the whole piece of
    > code.


    No, I don't know perfectly well. I did not see the " mark before the
    defined there. If you want to minimize errors such as this one, you might
    want to help your readers by formatting your post better:

    <example>
    have written abundant amount of lines like

    defined $ENV{REMOTE_ADDR} ? $ENV{REMOTE_ADDR} : "";

    Obviously I do not understand ...
    </example>

    > You are again increasing the usenet noise by stupid writing


    I am happy with my IQ.

    >> Please give an example where you think it is necessary to pretend a
    >> Perl scalar is a C char array, then we can comment on whether that is
    >> the right way in that particular case.

    >
    > Again, that has little to do with the question of confirming is there
    > way to do this in perl.


    It has everything to do with it. Just recently, I found myself doing it in
    response to another post (see http://tinyurl.com/5m6wt).

    > The answer was confirmed by other posters.


    Which answer?

    Sinan.
    A. Sinan Unur, Dec 12, 2004
    #17
  18. On 12 Dec 2004 10:14:47 -0800, wrote:

    >K? Olai Lindbach wrote:
    >> $_ = ' string with spaces first';
    >> s/^\s+//;
    >> print "$_\n";

    >
    >But the code you propose cuts the spaces from the beginning of the line
    >and does not solve the problem. I was posing a problem which I needed
    >to know how many spaces there were.


    Sorry, I got so upset with earlier questions in this thread that I
    didn't get this question/problem correctly... ;-)

    use strict;

    $_ = ' string with spaces first';

    if(/^(\s+)/) {
    print length($1),"\n";
    }

    or using a decent variable name:

    $atext = ' string with spaces first';

    if($atext =~ /^(\s+)/) {
    print length($1),"\n";
    }



    This is not the most concise way, I believe.

    --
    mvh/Regards Kåre Olai Lindbach
    (News: Remove '_delete_' and '.invalid')
    (HTML-written email from unknown will be discarded)
    Kåre Olai Lindbach, Dec 12, 2004
    #18
  19. On Sun, 12 Dec 2004 19:25:46 +0100, Kåre Olai Lindbach
    <> wrote:

    >use strict;


    [snipp]

    >or using a decent variable name:
    >
    >$atext = ' string with spaces first';
    >
    >if($atext =~ /^(\s+)/) {
    > print length($1),"\n";
    >}


    This last one wount compile without a 'my'. Where to put it is left as
    an exercise...

    --
    mvh/Regards Kåre Olai Lindbach
    (News: Remove '_delete_' and '.invalid')
    (HTML-written email from unknown will be discarded)
    Kåre Olai Lindbach, Dec 12, 2004
    #19
  20. wrote in
    news::

    > Just to correct myself, my space counting code does not work. But I
    > tried this and it did work:
    > $i=1;while (/^ {$i}/) {++$i;}--$i;
    > But this didn't work:
    > $i=0;while (/^ {$i+1}/) {++$i;}
    > which needless to say I do not understand.


    I am not even sure what you mean by "work" here. On the other hand, you
    could find a better way by actually reading

    perldoc perlop

    and thinking about this a little instead of attempting write C in Perl.

    use strict;
    use warnings;

    my $s = ' sdfj sdlkfj sdkfj sdkfj sdfk lk';
    my $i = 0;

    # Writing C in Perl can be fun
    ++$i while substr($s, $i, 1) eq ' ';
    print "$i\n";


    # One way to do it with a regex

    my ($spaces) = ($s =~ /^( *)/);
    print length($spaces), "\n";

    Sinan
    A. Sinan Unur, Dec 12, 2004
    #20
    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. =?Utf-8?B?Sko=?=

    Migrating 1.1 to 2.0 Questions

    =?Utf-8?B?Sko=?=, Jan 4, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    304
    =?Utf-8?B?Sko=?=
    Jan 4, 2005
  2. =?Utf-8?B?QW5kcmVhcyBIb2ZtYW5u?=

    Newbie question: migrating "global variables" from ASP to ASP.NET

    =?Utf-8?B?QW5kcmVhcyBIb2ZtYW5u?=, Apr 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    668
    =?Utf-8?B?QW5kcmVhcyBIb2ZtYW5u?=
    Apr 18, 2005
  3. Replies:
    1
    Views:
    1,785
    Marco De Paoli
    Feb 12, 2007
  4. vj
    Replies:
    7
    Views:
    349
    Robert Kern
    Mar 14, 2007
  5. Replies:
    56
    Views:
    688
    alex23
    Dec 27, 2012
Loading...

Share This Page