Beginner need help on perl issues

P

Petterson Mikael

Hi,

What is the meaning of

my ($common,$local) = @_; in sub?

sub insert_or_update
{
my ($common,$local) = @_;

.....

}

Also I have a regular expression looking like the following ( see
below).
I does it on a file containing key=value.
Can anyone explain what is done?

foreach (@common_lines)
{
if (($common_key,$common_value) = /^\s*([^=#]+?)\s*=\s*(.*)$/)
{
$common_map{"$common_key"} = "$common_value";
}
}

All hints appreciated.

BR

//Mikael
 
G

Gunnar Hjalmarsson

Petterson said:
What is the meaning of

my ($common,$local) = @_; in sub?

sub insert_or_update
{
my ($common,$local) = @_;

If you pass arguments to that sub by calling it like:

insert_or_update('arg1', 'arg2');

those arguments will become elements in the @_ array. I leave it to
you to guess the rest. ;-)

Suggested reading:

http://www.perldoc.com/perl5.8.4/pod/perlintro.html

http://www.perldoc.com/perl5.8.4/pod/perlsub.html
Also I have a regular expression looking like the following ( see
below).
I does it on a file containing key=value.
Can anyone explain what is done?

foreach (@common_lines)
{
if (($common_key,$common_value) = /^\s*([^=#]+?)\s*=\s*(.*)$/)
{
$common_map{"$common_key"} = "$common_value";
}
}

It populates the hash %common_map with the key/value pairs.

Note that throwing out random questions like those, without showing
that you have made own efforts to find the answers, is not a
reasonable way to learn basic Perl. If you want to learn Perl, this is
a suitable starting point:

http://learn.perl.org/

And before posting to this newsgroup again, please study the posting
guidelines:

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Good luck!
 
G

Greg Bacon

: What is the meaning of
:
: my ($common,$local) = @_; in sub?

Try it out. Consider the following code:

@_ = ('apples', 'oranges', 'bananas');

my ($common,$local) = @_;

print "common = $common\n",
"local = $local\n";

It produces the following output:

common = apples
local = oranges

As you can see, it copies the first two items off the @_ array.

Read the perldata manpage. This is documented in the section on
lists.

: Also I have a regular expression looking like the following ( see
: below). I does it on a file containing key=value.
: Can anyone explain what is done?
:
: foreach (@common_lines)
: {
: if (($common_key,$common_value) = /^\s*([^=#]+?)\s*=\s*(.*)$/)
: {
: $common_map{"$common_key"} = "$common_value";
: }
: }

Piece by piece:

^ - "At the beginning of the line..."

\s* - "...optional whitespace followed by..."

( - "...(and remember what matches here)..."

[^=#]+? - "...match one or more chars that aren't = or #..."

) - "...return this value..."

\s* - "...followed by optional whitespace..."

= - "...followed by an equals sign..."

\s* - "...followed by optional whitespace..."

( - "...(and remember what matches here)...

.* - "...whatever's left on the line..."

) - "...also return this value if we match..."

$ - "...followed by end of line."

On a match, the regular expression match operator, i.e., /.../,
returns the portions of the input text -- in this case, each element of
@common_lines in turn -- that matched the parenthesized subexpressions,
i.e., ([^=#]+?) and (.*) in this case.

Hope this helps,
Greg
 
E

Eric Schwartz

Please put the subject of your post in the Subject: of your post.
Think of it this way: suppose you were searching for answers to your
questions in some newsgroup archive like groups.google.com. Would you
be able to figure out that this post was relevant, just by looking at
the Subject: line?
What is the meaning of

my ($common,$local) = @_; in sub?

Read 'perlintro' (with 'perldoc perlintro'). You can probably stop
doing everything else you're doing with Perl until you finish that,
it's that basic.
Also I have a regular expression looking like the following ( see
below).
I does it on a file containing key=value.
Can anyone explain what is done?

You'll want to read 'perlretut' (for "Perl Regex Tutorial") with
'perldoc perlretut'. This one's pretty simple, so I'll help you
decode it, but you'll really want to read perlretut
foreach (@common_lines)
{
if (($common_key,$common_value) = /^\s*([^=#]+?)\s*=\s*(.*)$/)

Let's write it out the long way:

if (my ($common_key,$common_value) = /^\s*

This says there is optional whitespace at the beginning of the line. \s
matches any white space character, and * means there are zero or more of them.
If you wanted to guarantee there was at least one whitespace character here,
then you'd use + instead of *
([^=#]+?)

Followed by a number of characters that are not = or #. The parens put
these matched characters into $1 (because it's the first set of parens)

\s*
Followed by optional whitespace

=

a literal = character

\s*

more optional whitespace

(.*)

and capture everything after the whitespace into $2 (since it's the

second set of parens
$/x))

and $ matches the end of the string. I added /x because /x says to
ignore whitespace in the regex, and you could just rip out my comments
and have an exact equivalent to the regex you quoted.

Now, the bits on the left hand side, as written, are:

($common_key, $common_value) = /...regex.../;

You should always put 'use warnings;' and 'use strict;' at the top of
all your Perl programs. This requires you to declare your variables,
so you'd write instead,

my ($common_key, $common_value) = /...regex.../;

The fact that the variables $common_key and $common_value are in
parentheses means that they are in list context. Perl does different
things sometimes depending on if an operator like // is in scalar
context or list context.

Now, I don't want to give away all the fun, and besides you could
probably stand to learn the practice of looking up the docs on your
own, so why don't you search perlretut for what happens when // is
evaluated in list context, and let us know what you find out? You
should be able to find the documentation easily, but if you can't
understand it, post back, and we'll help you work through it.

-=Eric
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top