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.



 
is there a diffrence between $_ (which i'm sure I've seen an error saying it has been depricated)

but as i was saying is $_ the whole built in array or just $_[0] or is the $_ different from the $_[array].

if a sub routine was passed 3 items eg.

Code:
&mysub("john","paul","gary");

in the sub the following would be true..
$_[0] = "john"
$_[1] = "paul"
$_[2] = "gary"

so what would $_ be equal to ?

the reference to the array?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
$string is a scalar.
@array is an array, a ordered list of scalars.
$array[1] is the second scalar in the @array list. (It is essentially just a scalar like $string).

$_ is also just a scalar.
@_ is also just an ordered list of scalars (used by perl to you pass lists to sub routines like your example).
$_[1] is the second scalar in the @_ list.


Think of the top scalars/arrays as user defined ones, think of the $_ and @_ ones as perls, or the system scalar and system array. Perl uses them for it's internal needs but happily allows the user to access them and manipulate them just like user defined scalars/lists.

I've not read that $_ is going to be deprecated but I don't try and keep up to date as much as I should either.
 
$_ and @_ are two different things. $_ is the default string variable, which the Perl is using at the present moment with the present func, and is accessible for the user. I personally am still newbie and never change that value, because many bad things could happen, and just peek at it. @_ is the default array, which Perl uses and $_[] is still a value in that array.
P.S. As I said I am just a newbie and this is the way I feel the things, so I hope somebody more experienced tell if I am right.

Corwin
 
It's quite ok to modify $_

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
If $_ was going to be deprecated there would probably be public lynchings...
 
hmmm - what is put into $_ by PERL then ?

also this is where I've seen errors using $_
Code:
split(/=/, $_);
which causes
Use of implicit split to @_ is deprecated
which is why i'm confused over $_ & @_

oh well i just stay clear of both unless as I showed in my example it's regarding a passed sub routine value.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top