Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

$dbh could not be passed to function unless another variable is also passed

by tukusejssirs (Beadle)
on Aug 08, 2019 at 12:50 UTC ( #11104165=perlquestion: print w/replies, xml ) Need Help??

tukusejssirs has asked for the wisdom of the Perl Monks concerning the following question:

I a Perl newbie. I want to pass $dbh to a function and it works, but only when $dbh is passed along with another variable; then it failes with Can't call method "disconnect" without a package or object reference at [script_name] line 27.

I have uploaded a MWE to here.

Can anyone tell me why? Why does it not work in both cases?

Replies are listed 'Best First'.
Re: $dbh could not be passed to function unless another variable is also passed
by hippo (Chancellor) on Aug 08, 2019 at 13:02 UTC

    Your pastebinned code includes this line:

        my $dbh = @_;   # Case #2

    This almost certainly doesn't do what you want as it enforces scalar context. Use list context instead:

    my ($dbh) = @_; # Case #2

    See the Context tutorial for more on this. In future, please include your code in the question (enclosed withing <code> ... </code> tags) rather than a transient off-site link.

      Indeed it works. Thank you. But can you ten me, why my $dbh = $_[0] does not work?

        That also works fine. SSCCE:

        $ cat close.pl #!/usr/bin/env perl use strict; use warnings; use DBI; my $dbh = DBI->connect ('dbi:SQLite:dbname=foo', '', ''); close_db ($dbh); sub close_db { my $dbh = $_[0]; $dbh->disconnect or die "Could not disconnect: $DBI::errstr\n"; } $ perl close.pl $
Re: $dbh could not be passed to function unless another variable is also passed
by jcb (Parson) on Aug 09, 2019 at 01:41 UTC

    The idiom as I understand for extracting function arguments into lexicals is: my $var = shift;

    Sample code:

    use strict; use warnings; sub quux { my $foo = shift; my $bar = shift; my $baz = shift; print "FOO:$foo BAR:$bar BAZ:$baz\n"; } quux(1, 2, 'three'); quux(one => 2, 3); # illustrate "fat comma"

    Sample output:

    FOO:1 BAR:2 BAZ:three FOO:one BAR:2 BAZ:3

    Generally, you should use one of these idioms near the very beginning of each sub to bring your arguments into lexicals. There is usually little difference between assigning @_ to a list of variables and using shift, but sometimes it can matter, particularly if you have additional arguments to process. You can also use splice on @_ to extract a group of arguments into a list of variables or an array, but this is a more advanced feature. Directly accessing elements of @_ is best limited to very short subs, with a comment listing the arguments nearby.

    The "fat comma" is another neat Perl feature. You can think of it as quoting the previous word (there are some fairly complex rules for what gets quoted, but in most contexts it Just Works) and acting exactly like a normal comma in every other way. You will see it often in keyword arguments in many Perl modules. See the "Comma Operator" section in perlop for details.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11104165]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2021-05-16 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (151 votes). Check out past polls.

    Notices?