Reference syntax

T

tim_milstead

Is this:

my $arg_ref = \@_;

the same as this:

my ($arg_ref) = @_;

What is the correct way of reading the latter in English, pseudo code?

Thanks in advance.

Tim.
 
T

Tim Southerwood

Is this:

my $arg_ref = \@_;

the same as this:

my ($arg_ref) = @_;

What is the correct way of reading the latter in English, pseudo code?

Thanks in advance.

Tim.

No, they aren't the same.

The first makes $arg_ref contain a reference to \@_, so that
$_[0] equiv $arg_ref->[0]
$_[1] equiv $arg_ref->[1]
etc.

The second statement causes $arg_ref to hold a copy of the first element of
@_ so that

$arg_ref "contains a copy of" $_[0] and could have been written:

$arg_ref = $_[0];

More generally, one might do:

($a, $b, $c, @w) = @_;

which means:

$a = $_[0];
$b = $_[1];
$c = $_[2];
@w = rest of @_

HTH

Tim
 
A

anno4000

Is this:

my $arg_ref = \@_;

the same as this:

my ($arg_ref) = @_;

No. The first assigns a reference to @_ to $arg_ref. The second
assigns the first element of @_ ($_[0]) to $arg_ref. Entirely
different.

You may have been thinking of

my $arg_ref;
@$arg_ref = @_;

That has superficially the same effect as your first statement, but
it creates an anonymous copy of @_ and stores *that* in $arg_ref,
not a reference to @_,
What is the correct way of reading the latter in English, pseudo code?

Huh?

Anno
 
T

tim_milstead

- snip -

That makes sense. I only ask because page 182 of perl best practices
recommends named arguments are parsed like this:

sub padded {
 
T

tim_milstead

On 20 Jun, 14:43, (e-mail address removed) wrote:

Sorry sent before finishing:


That makes sense. I only ask because page 182 of perl best practices
recommends named arguments are parsed like this:

sub padded {
my ($arg_ref) = @_;

instead of:

sub padded {
my %arg = @_;

or even what I would have though:

my $arg_ref = \@_;

because non matched pairs causes a compile time error on the first and
a run time error on the second.
I'm not sure why the first is better than the third?

Tim
 
J

Jürgen Exner

On 20 Jun, 14:43, (e-mail address removed) wrote:
That makes sense.

What makes sense? Please quote appropriate context, such that people know
what you are responding to.
I only ask because page 182 of perl best practices
recommends named arguments are parsed like this:

sub padded {
my ($arg_ref) = @_;

Personally I would prefer
my $arg_ref = shift;
because your method becomes a pain when dealing with more than one
parameter.
instead of:

sub padded {
my %arg = @_;

But those are totally different animals.
In the first sub you would be passing a reference (to e.g. an array or
hash), while in the second example you are passing the hash itself.
or even what I would have though:

my $arg_ref = \@_;

Well, that's yet another beast that has nothing to do with either of the
first examples. Of the top of my head I cannot think of a useful example
when you would want to create a reference to the parameter list.
because non matched pairs causes a compile time error on the first and
Really?

a run time error on the second.

Sure. When assigning a hash like that then it will only work if you got
key/value pairs.
I'm not sure why the first is better than the third?

They are different animals that do different things.

jue
 
T

Tad McClellan

Jürgen Exner said:
(e-mail address removed) wrote:

Personally I would prefer
my $arg_ref = shift;
because your method becomes a pain when dealing with more than one
parameter.


my($arg1, $arg2, $arg3, $arg4, $arg5) = @_;

vs

my $arg1 = shift;
my $arg2 = shift;
my $arg3 = shift;
my $arg4 = shift;
my $arg5 = shift;


And your position is that the 1st is more painful than the 2nd?

Personally, I'd say repeatedly typing the same thing is a pain...
 
T

Tad McClellan

On 20 Jun, 14:43, (e-mail address removed) wrote:
sub padded {
my ($arg_ref) = @_;

instead of:

sub padded {
my %arg = @_;

or even what I would have though:

my $arg_ref = \@_;

because non matched pairs causes a compile time error on the first and
a run time error on the second.


Neither of them cause errors.

(warnings are not errors)

I'm not sure why the first is better than the third?


I'm not sure why a whisker is better than a table.
 
J

Jürgen Exner

Tad said:
my($arg1, $arg2, $arg3, $arg4, $arg5) = @_;

vs

my $arg1 = shift;
my $arg2 = shift;
my $arg3 = shift;
my $arg4 = shift;
my $arg5 = shift;


And your position is that the 1st is more painful than the 2nd?

Personally, I'd say repeatedly typing the same thing is a pain...

Well, ok, you got a point :)
I was thinking along different lines but of course you are right.

jue
 
B

Brad Baxter

Is this:

my $arg_ref = \@_;

Store a reference to the @_ array in the lexical scalar $arg_ref.
the same as this:

my ($arg_ref) = @_;

Store the value of the first element in the @_ array in the lexical
scalar $arg_ref.
What is the correct way of reading the latter in English, pseudo code?

I suppose the exercise might be clarifying.
 
M

Mario D'Alessio

Tad McClellan said:
my($arg1, $arg2, $arg3, $arg4, $arg5) = @_;

vs

my $arg1 = shift;
my $arg2 = shift;
my $arg3 = shift;
my $arg4 = shift;
my $arg5 = shift;


And your position is that the 1st is more painful than the 2nd?

Personally, I'd say repeatedly typing the same thing is a pain...

I like to do this:

my(
$arg1,
$arg2,
...
$argn,
) = @_;

This way, I can easily add/remove args, and code counting tools
will track a line change (added, deleted, modified) per arg.

Mario
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top