Your skill will accomplishwhat the force of many cannot PerlMonks

### Re: Passing a database connection

by doob (Pilgrim)
 on Nov 22, 2005 at 21:35 UTC ( #510919=note: print w/replies, xml ) Need Help??

in reply to Passing a database connection

Hey! Shouldn't my \$dbh; sub new { \$dbh = @_; } actually be my \$dbh; sub new { \$dbh = @_[0]; } ? Try it! It might work! This is because when passing arguments, the @_ has to have an index, starting at 0. If you would have two arguments, one would be @_[0](this is equal to your first argument) and another would be @_1(this is equal to your second, which you luckily don't have).

Replies are listed 'Best First'.
Re^2: Passing a database connection
by ikegami (Pope) on Nov 22, 2005 at 21:37 UTC
Actually
\$dbh = \$_[0];
or
(\$dbh) = @_;
or
\$dbh = shift(@_);
or
\$dbh = shift;
but yes.
Re^2: Passing a database connection
by dragonchild (Archbishop) on Nov 23, 2005 at 02:23 UTC
It shouldn't be @_[0] because that's the arrayslice consisting of the 0th element. If you turn warnings on, it will tell you about this. Arrayslicing is really cool. Try the following snippet:
```my @x = 0 .. 9;
print "@x[2..4]\n";
print "@x[3,5,7]\n";
my @y = 4..6;
print "@x[@y]\n";
@x[7..9] = @y;
print "@x\n";

The reason why you don't want to use the arrayslice form when you want to extract a single element is, while it has been special-cased to work in the case of my \$x = @x[2]; (it should assign 1 and not 2), it won't work in this case:

```sub foo {
wantarray ? 3 : 5;
}

my @x;
@x[2] = foo();
my \$x = foo();
print "\$x[2] <-> \$x\n";
@x[2] is list context, not scalar context.

My criteria for good software:
1. Does it work?
2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
I see what you are saying. I made a mistake. It should've been \$_[0], no?

=d(o_o)b=

Create A New User
Node Status?
node history
Node Type: note [id://510919]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2018-07-22 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (454 votes). Check out past polls.

Notices?