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

Please can you tell me what the hyphen before an argument means in the following code.

$dbcontext = Bio::DB::SimpleDBContext->new( -database => 'biosql' -user => 'xxxx', -pass => 'xxxx', -dbname => 'biosql', -host => 'localhost', -port => 3306, # optional -driver => 'mysql', );

many thanks

Replies are listed 'Best First'.
Re: beginner - hyphen before a parameter
by NetWallah (Canon) on Oct 18, 2010 at 19:18 UTC
    It means nothing.

    It is just one of many conventions in the way you specify parameters.

    Specifically, perl "auto-quotes" the name when used at the left side of the "=>" operator, so the name essentially is "-host", "-user" etc..., and perl does not try to understand the contents.

    How a particular name is interpreted is left to the recipient of the parameters.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: beginner - hyphen before a parameter
by DStaal (Chaplain) on Oct 18, 2010 at 20:04 UTC

    Nothing... Except it identifies the 'name' of the parameter.

    Perl does not have named parameters. It passes the entire set of parameters to the subroutine as a list. (Elements of which can be references to other structures, of course.) So the code shown only looks like you are passing a hash. You are actually passing a list.

    Therefore, the subroutine has to turn that list into something useful. In the above, they likely are trying to be nice, and letting you present the arguments in any order, like a hash. But to do so, they need to know which order they are actually in. A simple way is to create a set of reserved words, look for them in the parameter list, and say that the next item in the list is the argument for that reserved word. And, of course, you want a reserved word that will never be used, is recognizable as such, and if possible allows expansion using the same pattern, in case you need more arguments later.

    Putting a hyphen in front of a word satisfies all of the above, fairly simply, and in a way that anyone familiar with a Unix-style commandline will understand.

    So, it's just a convention, but a useful one.

Re: beginner - hyphen before a parameter
by TomDLux (Vicar) on Oct 18, 2010 at 20:16 UTC

    When you run a command line program, the simplest way to provide arguments is to simply have the values on the command line. For example, you might have a program named 'sum, which would add together all the numbers passed in: sum 1 2 3 4 would return '10'.

    That's great for simple arguments. If you want to calculate something about a vehicle, you might want to pass in the number of wheels, the vehicle weight and horsepower. Suddenly the order becomes significant, you have to put them in the right order. So a good idea is to give the arguments names, especially if the convertible attributes only apply to cars, the wings attributes only to airplanes, and the saddlebags only to motorcycles. So you might have a miles-per-gallon program, mpg, which is invoked: mpg --weight 500 --wheels 2 --cc 1100.

    Similarly, subroutines are normally passed arguments by position: area( 4, 5). The modern trend is either to pass in a single hash reference containing named arguments, or to have separate named arguments, as above. The hash ref equivalent would look like below; some programs that accept on accept the other as well.

    $dbcontext = Bio::DB::SimpleDBContext->new( { -database => 'biosql' -user => 'xxxx', -pass => 'xxxx', -dbname => 'biosql', -host => 'localhost' +, -port => 3306, # +optional -driver => 'mysql', } );

    The arror '=>' is called a 'fat comma', because it acts as a comma separator, only with better visibility, but also stringifies the preceeding bareword term.

    I was goign to say there is no reason to have hyphens on the labels, but I did think of one. If the labels were optional, someone might invoke the costructor with labelled arguments, in whichever order they like, or they could use ordered arguments, in a single, specified order. So the presence of the hyphens clarifies their intent. You might process it like this ...

    # in Bio::DB::SimpleDBContext sub new { my $class = shift; my %args; if ( '-' eq substr 0,1, $_[0] ) { die( "Odd number of named arguments.\n" ) if @_ % 2; while ( @_ ) { my $tag = shift @_; my $val = shift @_; $args{qw/database, user, pass, dbname, host, port, driver/} = + $val; } } else { my $args{arg1, arg2} = @_; }

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: beginner - hyphen before a parameter
by ikegami (Patriarch) on Oct 18, 2010 at 23:51 UTC
    It's not before the parameter, it's part of the parameter. The author simply chose to name his parameters with a leading "-", perhaps since it bears a resemblance to command-line options.
Re: beginner - hyphen before a parameter
by raybies (Chaplain) on Oct 18, 2010 at 20:20 UTC
    The dash is used on unix commandlines, but it's just part of the string. The => operator is preceded by a raw string, used as a hash key => value pair. Within perl it has no actual significance, and is not a sigil, though it might be convenient to stuff onto a commandline...