Newbie questions, migrating from c++

G

gg500

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

Sherm Pendley

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

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

A. Sinan Unur

(e-mail address removed) wrote in @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.
 
P

Paul Lalli

(e-mail address removed) 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
 
G

gg500

Sherm said:
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.
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.
 
G

gg500

A. Sinan Unur said:
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".
....

That is not how you access a hash element.
....
$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.
 
S

Sherm Pendley

It's not my intention to learn the libraries yet. I try to understand
how to do this right.

Using CGI.pm *is* the right way to parse form input.

sherm--
 
G

gg500

Paul said:
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.
 
G

gg500

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

Sherm Pendley

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

gg500

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

gg500

Sherm said:
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.
 
S

Sherm Pendley

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

Kåre Olai Lindbach

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...?
 
G

gg500

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

gg500

Kåre Olai Lindbach said:
$_ = ' 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.
 
A

A. Sinan Unur

(e-mail address removed) wrote in
A. Sinan Unur wrote:

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

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.

Indeed, that is not how you access a hash element. We can only comment on
what you wrote.
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 ...
You are again increasing the usenet noise by stupid writing

I am happy with my IQ.
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.
 
K

Kåre Olai Lindbach

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

Kåre Olai Lindbach

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

A. Sinan Unur

(e-mail address removed) wrote in
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
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top