A newbie question on pattern matching

D

DelphiDude

REAL newbie. I just got my book in the mail yesterday. And of course at
this point, I am not trying to produce anything, just understand stuff
that is a long way from Turbo Pascal and C++.

So far everything falls into place once I code it up and change it around
and play with it.

However, some questions on the following snippit (which doesn't work, by
the way).

#!/usr/bin/perl -w

testsub("testword");

sub testsub{
print $_[0]; # just as a test to see if it works.
if ($_[0] = /test/){
print "found it";
}
}

First off, as I understand it, a call to a sub with a value(s)
automatically puts it into a temporary array called $_[].

And it actually does work. I can print the value of $_[0] which correctly
comes out as "testword". But the match comparison errors out with...

Use of uninitialized value in pattern match (m//) at ./test2 line 7.

What value? The left side variable is ok. The /test/ is just a string
pattern as far as I can see.

My Perl book kind of glosses over this part and move on to much more
indepth pattern matching. Can anybody clue me in?

DD
 
J

Jürgen Exner

DelphiDude said:
REAL newbie. I just got my book in the mail yesterday. And of course
at this point, I am not trying to produce anything, just understand
stuff that is a long way from Turbo Pascal and C++.

So far everything falls into place once I code it up and change it
around and play with it.

However, some questions on the following snippit (which doesn't work,
by the way).

#!/usr/bin/perl -w

testsub("testword");

sub testsub{
print $_[0]; # just as a test to see if it works.
if ($_[0] = /test/){
print "found it";
}
}

First off, as I understand it, a call to a sub with a value(s)
automatically puts it into a temporary array called $_[].

Well, not temporary. It is the local variable for the arguments inside of
the sub.
And it actually does work. I can print the value of $_[0] which
correctly comes out as "testword". But the match comparison errors
out with...

Use of uninitialized value in pattern match (m//) at ./test2 line 7.

What value? The left side variable is ok. The /test/ is just a
string pattern as far as I can see.

My Perl book kind of glosses over this part and move on to much more
indepth pattern matching. Can anybody clue me in?

Actually quite simple if you know what to look for ;-)

Unless specified otherwise the match operator will try to match $_. But
there is no place in the whole script that defines a value for $_. Therefore
you are getting the uninitialized value error message.
The second mistake is that you are assigning the result of the match to
$_[0]. That does not make any sense at this place.

What you want instead is to bind the match operator to $_[0] such that is
will match $_[0] instead of $_.
And this is done by using the binding operator "=~" (without the quotes).

So just add the tilde to your code and it should work.

Two things to learn here:
- you need to write and read your Perl code carefully. Missing a single
character can change the meaning of your whole program. And Perl will not
always warn you!
- to look up the functionality of an operator use "perldoc perlop" (that
would have explained the "=" and the "=~" operators), to look up the
functionality of a function use "perldoc -f functionname". "perldoc -f m"
would have told you that m is the match operator and that you should check
it out in the perldoc perlop manpage again.

jue
 
J

Joe

DelphiDude said:
REAL newbie. I just got my book in the mail yesterday. And of course at
this point, I am not trying to produce anything, just understand stuff
that is a long way from Turbo Pascal and C++.

So far everything falls into place once I code it up and change it around
and play with it.

However, some questions on the following snippit (which doesn't work, by
the way).

#!/usr/bin/perl -w

testsub("testword");

sub testsub{
print $_[0]; # just as a test to see if it works.
if ($_[0] = /test/){
print "found it";
This is pretty nasty code man..


I would rewrite this..

#######################################
#!/usr/bin/perl -w

my $word = "testword";
print "WORD: \"$word\"\n";

if($word =~ /test)
{
print "FOUND WORD in \"$word\"\n";
}

exit;
#######################################

then once it works in std conext put it into subroutine

#######################################
#!/usr/bin/perl -w

my $word = "testword";
print "WORD: \"$word\"\n";

$find = findtest($word);
if($find eq "yes")
{ print "FOUND WORD in \"$word\"\n"; }
else
{ print "NO FOUND WORD in \"$word\"\n"; }

exit;

##### SUBS

sub findtest
{
my $word = shift(@_);
my $found = "no";
if($word =~ /test)
{
print "FOUND WORD in \"$word\"\n";
$found = "yes";
}
return $found;
}

#######################################


First off, as I understand it, a call to a sub with a value(s)
automatically puts it into a temporary array called $_[].

And it actually does work. I can print the value of $_[0] which correctly
comes out as "testword". But the match comparison errors out with...

Use of uninitialized value in pattern match (m//) at ./test2 line 7.

What value? The left side variable is ok. The /test/ is just a string
pattern as far as I can see.

My Perl book kind of glosses over this part and move on to much more
indepth pattern matching. Can anybody clue me in?

DD
 
J

Joe

Joe said:
DelphiDude said:
REAL newbie. I just got my book in the mail yesterday. And of course at
this point, I am not trying to produce anything, just understand stuff
that is a long way from Turbo Pascal and C++.

So far everything falls into place once I code it up and change it around
and play with it.

However, some questions on the following snippit (which doesn't work, by
the way).

#!/usr/bin/perl -w

testsub("testword");

sub testsub{
print $_[0]; # just as a test to see if it works.
if ($_[0] = /test/){
print "found it";
This is pretty nasty code man..


I would rewrite this..

#######################################
#!/usr/bin/perl -w

my $word = "testword";
print "WORD: \"$word\"\n";

if($word =~ /test)

Opps...if($word =~ /test/) # should be this
{
print "FOUND WORD in \"$word\"\n";
}

exit;
#######################################

then once it works in std conext put it into subroutine

#######################################
#!/usr/bin/perl -w

my $word = "testword";
print "WORD: \"$word\"\n";

$find = findtest($word);
if($find eq "yes")
{ print "FOUND WORD in \"$word\"\n"; }
else
{ print "NO FOUND WORD in \"$word\"\n"; }

exit;

##### SUBS

sub findtest
{
my $word = shift(@_);
my $found = "no";
if($word =~ /test)

Opps...if($word =~ /test/) # should be this
{
print "FOUND WORD in \"$word\"\n";
$found = "yes";
}
return $found;
}

#######################################


First off, as I understand it, a call to a sub with a value(s)
automatically puts it into a temporary array called $_[].

And it actually does work. I can print the value of $_[0] which correctly
comes out as "testword". But the match comparison errors out with...

Use of uninitialized value in pattern match (m//) at ./test2 line 7.

What value? The left side variable is ok. The /test/ is just a string
pattern as far as I can see.

My Perl book kind of glosses over this part and move on to much more
indepth pattern matching. Can anybody clue me in?

DD
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top