Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

whats wrong ?

Status
Not open for further replies.

WebGoat

MIS
Jul 16, 2005
85
US
Code:
#!/usr/local/bin/perl-w
 use strict;
 use diagnostics;
                                                                                
# Program to open the password file, read it in,
# print it, and close it again.
                                                                                
my $file = '/etc/passwd';               # Name the file
open(INFO, $file);              # Open the file
my @lines = <INFO>;             # Read it into an array
close(INFO);                    # Close the file
#print @lines;                  # Print the array
                                                                                
foreach $morsel (@food)         # Visit each item in turn
                                # and call it $morsel
{
        print "$morsel\n";      # Print the item
        print "Yum yum\n";      # That was nice
}


output:

Global symbol "$morsel" requires explicit package name at test line 14.
Global symbol "@food" requires explicit package name at test line 14.
Global symbol "$morsel" requires explicit package name at test line 17.
Execution of test aborted due to compilation errors (#1)
Uncaught exception from user code:
Global symbol "$morsel" requires explicit package name at test line 14.
Global symbol "@food" requires explicit package name at test line 14.
Global symbol "$morsel" requires explicit package name at test line 17.
Execution of test aborted due to compilation errors.




whats wrong with the program ?

i followed the instruction from the tutorial. still its giving error . i am not able to understand this error message.



 
Crikey WebGoat!
You're working me hard today!
Your code fails because you mixed two scripts that use different variables.
If I clean is up, maybe you can see what is different and what you should have done.
Code:
#!/usr/local/bin/perl-w
 use strict;
 use diagnostics;
                                                                                
# Program to open the password file, read it in,
# print it, and close it again.
                                                                                
my $file = '/etc/passwd';               # Name the file
open(INFO, $file);              # Open the file
my @lines = <INFO>;             # Read it into an array
close(INFO);                    # Close the file
#print @lines;                  # Print the array
                                                                                
foreach [red]my[/red] $morsel ([red]@lines[/red])     # Visit each item in turn
                                # and call it $morsel
{
        print "$morsel\n";      # Print the item
        print "Yum yum\n";      # That was nice
}


Trojan.
 
You haven't declared the variables $morsel or @food. Since you're using strict, you must declare all variables before their use.

- Rieekan
 
superb!. its working now. sorry for the silly mistake. i have deleted the old file. and ceated a new one.

 
in this context i just have a small question

you gave me his code in my previous post
code was like below

Code:
while(<>) {
  # $_ contains each line from the file, one at a time
  # process the line here.
}

will you please how do you read line by lne with a while loop ?

i am reading with a for loop but yours was a while loop + line by line ,..thats very interesting. will you please
fill up your code . just want to see the difference.
thanks for the time
 
Take a look at this then.
Note that you should "chomp" the line after reading it to remove the trailing linefeed.
Read up on "chomp" and you'll see what I mean.
I've kept this code as similar as possible to make understanding it easier.
Code:
#!/usr/local/bin/perl-w
 use strict;
 use diagnostics;

# Program to open the password file, read it in,
# print it, and close it again.

my $file = '/etc/passwd';               # Name the file
open(INFO, $file);              # Open the file

while(<INFO>)                   # Read each line from file to $_ in turn
{
        print "$_\n";           # Print the line
        print "Yum yum\n";      # That was nice
}
close(INFO);                    # Close the file


Trojan.
 
this works fine. you have explained very nicely. thanks

i also examined
with
Code:
print $_;  // just prints variable
print "\n"; // just prints the newline

and also

Code:
print $_."\n";  // i read this the rule concatecation of sting

and i am comfortable with these two syntax.
i was not feeling comfortable with your syntax. you are using the double quote to print a variable !.
anyway, that too works fine.


question is
what is this $_ variable. you did not declare it. and go for printing !! is it a in-bulit variable of perl ? is it a keyword kind of things ? reserved variable ?
 
i have one more smallest question.

as i am learning perl , can i use perl in windows machine ?

right now, i am writing codes in linux machine.

is perl only exclusive for linux machine ?

does it work in windows 2000, windows XP machine ?
 
Lots of questions!
$_ is one of many special variables that perl uses if you don't specify a variable. It makes the syntax much shorter and quicker to type for many things and so is useful to learn about. Yes, it is a form of reserved variable.
Yes, you can use perl in windoze. You can download a windoze version from Yes, it works in windoze 2000 and XP.
In short, YES!
;-)



Trojan.
 
many thanks sir,

can i ask one more very simplest question ?
you made a comment...
# Read each line from file to $_ in turn

but where did you assign the line to $_ . you did not assign it.
how does it get the value ?

i am feeling uncomfortable with this.
will you please tell me whats going on here ?
thank you
 
[red]<INFO>[/red] reads a record from the file and stores that line in [red]$_[/red] automatically.


Trojan.
 
Code:
<INFO> reads a record from the file and stores that line in $_ [b]automatically[/b].

OK . good.

is it exclusive for only for Files ?

had it not been a file , but a simple array which contanis some data, will the same occured ?

in fact, i am worried about in which place this works and in which place this does not work .

i guess , in all reading this may not work..right ?
 
Yes, [red]<>[/red] is exclusive to files and streams (such as STDIN).
Automatically storing the result in [red]$_[/red] is not specific to files or <>. There are many other functions that process $_ in one way or another by default.


Trojan.
 
Automatically storing the result in $_ is not specific to files or <>. There are many other functions that process $_ in one way or another by default.

Sir, This is a very difficult statement to understand.

i got more confused when i read the following tutorial


The $_ special variable


We could use a conditional as

if ($sentence =~ /under/)
{
print "We're talking about rugby\n";
}

which would print out a message if we had either of the following

$sentence = "Up and under";
$sentence = "Best winkles in Sunderland";

But it's often much easier if we assign the sentence to the special variable $_ which is of course a scalar. If we do this then we can avoid using the match and non-match operators and the above can be written simply as

if (/under/)
{
print "We're talking about rugby\n";
}

The $_ variable is the default for many Perl operations and tends to be used very heavily.





i am really very very much confused about the behaviour of $_ .

i dont understand how it works . because evrythings seems invisble .

do you mean for any assignment , $_ always gets one copy of the value automatically iresspective of the assignmemnt(i.e whether its file, addition,subtraction, whatever $_ always there and gets a freec opy of the variable invisibly

i am feeling its toughest concept sir.


Will you please unravel the myth of $_ variable ?

Thanks for your time.
 
This is a matter of learning which functions/operators use $_ as a default and which do not.
It it possibly one of perls strengths in that it allows very succinct code but also one of perls weakenesses (at least for a beginner) in that you have to learn which functions/operators do use $_ and which do not. Also you need to learn how they use $_.
I suggest you get some books for beginners or google around for beginners guides to perl.



Trojan.
 
>but also one of perls weakenesses
Sir,yes,i also term it as a weakness. because it is hiding the fact!

anyway,
you have to learn which functions/operators do use $_ and which do not

so you mean there are some functions which uses this $_ variable.

but in your previous example which you posted (while loop code) there is no function nor operator is no it ? the line has been copied into $_ variable SILENTLY without any visible assignment.

the language is hiding the fact. very bad thing

my head is burning.
 
No, there is and operator. It's the <> operator.
That always assigns the result of a file read to $_ unless you specify differently as in "$x = <>;".
It's not bad that it hides these things, but it does make it more difficult for beginners like yourself.


Trojan.
 
No, there is and operator. It's the <> operator.
That always assigns the result of a file read to $_
got some peace at least.

Thank you
 
The use of $_ is only a "weakness" if you do not understand how it works. You can turn it to your advantage easily. Don't get hung up on it though. If you never want to use $_ you don't have to. But once you learn how to use it you will love it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top