Help needed replacing deprecated syntax

P

Page

I received the following message:

C:\Program Files\Apache Group\Apache2\cgi-bin\corr>perl -wc corrdetail.pl
Using an array as a reference is deprecated at corrdetail.pl line 106 (#1)
(D deprecated) You tried to use an array as a reference, as in
< @foo-[23] >> or < @$ref-[99] >>. Versions of perl <= 5.6.1 used to
allow this syntax, but shouldn't have. It is now deprecated, and will be
removed in a future version.

corrdetail.pl syntax OK

Here is the offending line:
my $msg = @_->[0];

What is the current proper syntax for such a statement?

Thanks.
 
B

Ben Morrow

Quoth (e-mail address removed) (Page):
I received the following message:

C:\Program Files\Apache Group\Apache2\cgi-bin\corr>perl -wc corrdetail.pl
Using an array as a reference is deprecated at corrdetail.pl line 106 (#1)
(D deprecated) You tried to use an array as a reference, as in
< @foo-[23] >> or < @$ref-[99] >>. Versions of perl <= 5.6.1 used to
allow this syntax, but shouldn't have. It is now deprecated, and will be
removed in a future version.

corrdetail.pl syntax OK

Here is the offending line:
my $msg = @_->[0];

What is the current proper syntax for such a statement?

That depends on what you meant it to do. If you wanted the first item in
@_, you meant $_[0] (perlsyn). If you wanted to deref an array ref in $_
then you meant $_->[0] (perlref). If you wanted something else you will
have to tell us what that was.

Ben
 
P

Page

Ben Morrow said:
Quoth (e-mail address removed) (Page):
I received the following message:

C:\Program Files\Apache Group\Apache2\cgi-bin\corr>perl -wc corrdetail.pl
Using an array as a reference is deprecated at corrdetail.pl line 106 (#1)
(D deprecated) You tried to use an array as a reference, as in
< @foo-[23] >> or < @$ref-[99] >>. Versions of perl <= 5.6.1 used to
allow this syntax, but shouldn't have. It is now deprecated, and will be
removed in a future version.

corrdetail.pl syntax OK

Here is the offending line:
my $msg = @_->[0];

What is the current proper syntax for such a statement?

That depends on what you meant it to do. If you wanted the first item in
@_, you meant $_[0] (perlsyn). If you wanted to deref an array ref in $_
then you meant $_->[0] (perlref). If you wanted something else you will
have to tell us what that was.

Ben

Ok. It's the first line of a function that may or may not have a
string value parameter passed to it. Basically I'm just setting $msg
to that parameter, then I check to see if it has a value like this:
if ($msg ne "") { ... do stuff... }

Which is the correct syntax (of your two above) for this or is there a
better way to do this?
 
J

Jürgen Exner

Page said:
Ok. It's the first line of a function that may or may not have a
string value parameter passed to it. Basically I'm just setting $msg
to that parameter, >
Which is the correct syntax (of your two above) for this or is there a
better way to do this?

my $msg = shift; #removes the item from the parameter list
or
my $msg = @_[0]; #does not remove the item from the parameter list
then I check to see if it has a value like this:
if ($msg ne "") { ... do stuff... }

There is a difference between an empty string and an undefined string.

jue
 
U

Uri Guttman

JE> my $msg = shift; #removes the item from the parameter list
JE> or
JE> my $msg = @_[0]; #does not remove the item from the parameter list

still wrong. it should be $_[0].

but what is wrong about the classic my ($msg) = @_ ; ?

uri
 
T

Tad McClellan

Jürgen Exner said:
my $msg = @_[0]; #does not remove the item from the parameter list


You should always enable warnings when developing Perl code!

heh.
 
G

Gunnar Hjalmarsson

Page said:
Ok. It's the first line of a function that may or may not have a
string value parameter passed to it. Basically I'm just setting
$msg to that parameter, then I check to see if it has a value like
this: if ($msg ne "") { ... do stuff... }

That test is inappropriate, since $msg will be undef if nothing is
passed to the function. Maybe you mean:

if (defined $msg) { ... do stuff... }

By having warnings enabled, you catch that kind of mistake.
 
G

Gunnar Hjalmarsson

Page said:
Ok. It's the first line of a function that may or may not have a
string value parameter passed to it. Basically I'm just setting
$msg to that parameter, then I check to see if it has a value like
this: if ($msg ne "") { ... do stuff... }

That test is inappropriate, since $msg will be undef if nothing is
passed to the function. Maybe you mean:

if (defined $msg) { ... do stuff... }

By having warnings enabled, you catch that kind of mistake.
 
G

Gunnar Hjalmarsson

Page said:
Ben said:
Quoth (e-mail address removed) (Page):
Here is the offending line:
my $msg = @_->[0];

What is the current proper syntax for such a statement?

That depends on what you meant it to do. If you wanted the first
item in @_, you meant $_[0] (perlsyn). If you wanted to deref an
array ref in $_ then you meant $_->[0] (perlref). If you wanted
something else you will have to tell us what that was.

Ok. It's the first line of a function that may or may not have a
string value parameter passed to it.

In that case you should learn about subroutines in Perl instead of
just guessing.

perldoc perlsub

The @_ array is explained there, and you also find a lot of
illustrative examples.
Basically I'm just setting $msg to that parameter, then I check to
see if it has a value like this:
if ($msg ne "") { ... do stuff... }

That test is inappropriate, since $msg will be undef if nothing is
passed to the function. Maybe you mean:

if (defined $msg) { ... do stuff... }
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top