A doubt

M

mani

Hi,

I see this code :

while(<STDIN>){

if(/^From: /){

if(/merlyn/){
print "Email from merlyn\n";
}
last;
}
if(/^$/){

last;
}
}

what i cant understand is this :

I know that whatever is read from STDIN is read into $_ I thought if
we are looking for something which has From: we should look for this
pattern in $_
but i am seeing that the $_ never appers in the first if control
statement.
Is it understood that we are refering to $_ everywhere if not
specified in a control statement ???


thanks
\
Mani
 
J

Jürgen Exner

mani said:
while(<STDIN>){
if(/^From: /){ [...]
what i cant understand is this :

I know that whatever is read from STDIN is read into $_ I thought if
we are looking for something which has From: we should look for this
pattern in $_
but i am seeing that the $_ never appers in the first if control
statement.
Is it understood that we are refering to $_ everywhere if not
specified in a control statement ???

No, that assumption would be wrong. You are refering to $_ if the man page
of the involved function says so.
You just need to understand what functions and operators are involved.

"/^From:/" is the short form of "m(/^From:/)" (sorry, this one you just have
to know).
Now, checking out the documentation of "m" using "perldoc -f m" tells us:

m// The match operator. See the perlop manpage.

Ok, let's check the perlop man page, "perldoc perlop":

m/PATTERN/cgimosx
/PATTERN/cgimosx
Searches a string for a pattern match, and in scalar context
returns true if it succeeds, false if it fails. If no string is
specified via the "=~" or "!~" operator, the $_ string is
searched.

If "/" is the delimiter then the initial "m" is optional.

jue
 
J

John W. Krahn

mani said:
Hi,

I see this code :

while(<STDIN>){

if(/^From: /){

if(/merlyn/){
print "Email from merlyn\n";
}
last;
}
if(/^$/){

last;
}
}

what i cant understand is this :

I know that whatever is read from STDIN is read into $_ I thought if
we are looking for something which has From: we should look for this
pattern in $_ but i am seeing that the $_ never appers in the first
if control statement.
Is it understood that we are refering to $_ everywhere if not
specified in a control statement ???

Yes, if you add $_ to the program it would look like this:

LOOP: while ( defined( $_ = <STDIN> ) ) {
if ( $_ =~ /^From: / ) {
if ( $_ =~ /merlyn/ ) {
print "Email from merlyn\n";
}
last LOOP;
}
if ( $_ =~ /^$/ ) {
last LOOP;
}
}

The use of $_ is (usually) optional for most operators/functions.


John
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top