Check syntax question...

  • Follow


I am going through another persons code. I turned on "strict" and
"warnings" and I am not sure what to do about this warning:

"Scalar value @_[0] better written as $_[0] at update_patch.pl line
939."

Should I change it to reflect what is suggested? Why did it suggest
that? I am just a wee learner of Perl.

Robert
0
Reply bobx (95) 6/21/2004 4:52:47 PM

On Mon, 21 Jun 2004, Bob wrote:

> I am going through another persons code. I turned on "strict" and
> "warnings" and I am not sure what to do about this warning:
>
> "Scalar value @_[0] better written as $_[0] at update_patch.pl line
> 939."
>
> Should I change it to reflect what is suggested?

Almost certainly yes.  We'd have to see the code to be 100% certain, but
the chances of it not being better changed are very unlikely.

> Why did it suggest that?

In Perl, $ is used to mean scalar, which is a single value.  @ is used to
mean array, a collection of values.  The syntax of @array[2,3,4] for
example, means "the set containing the third, fourth, and fifth elements
of @array".  The syntax @array[2] means "the set containing the third
element of @array".  $array[2], on the other hand, means simply "the third
element of @array".  There is almost never a reason to use a collection
containing one element where all you actually need is that one element.
That is what that warning is about.

> I am just a wee learner of Perl.

Read up! :-)   Take a look at
perldoc perlsyn
for the syntax of Perl, and
perldoc perldiag
for a description and explanation of all the warnings you may encounter.


Paul Lali
0
Reply Paul 6/21/2004 5:02:18 PM


Bob <bobx@linuxmail.org> wrote:
> I am going through another persons code. I turned on "strict" and
> "warnings" and I am not sure what to do about this warning:
> 
> "Scalar value @_[0] better written as $_[0] at update_patch.pl line
> 939."
> 
> Should I change it to reflect what is suggested? 


Yes.


> Why did it suggest
> that? 


Because sometimes it makes a difference.


> I am just a wee learner of Perl.


Your Question is Asked Frequently:

  What is the difference between $array[1] and @array[1]?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas
0
Reply Tad 6/21/2004 11:21:33 PM

2 Replies
66 Views

(page loaded in 0.064 seconds)

Similiar Articles:













7/28/2012 11:55:14 PM


Reply: